Prevent changes on certain folders - by certain users
3 answers
Comments
Hi Michele,
My Code looks like this: - The problem that i have is FileItem - can be used only in clinet plugin i think and hence am unable to retrive the item details - folders and files here. Please suggest an alternative for server-side plugin
DeliverOperationData data = (DeliverOperationData) operationData;
List<IChangeSetHandle> changeSetHandles = data.getChangeSetHandles();
IRepositoryItemService itemService = getService(IRepositoryItemService.class);
IScmService scmService = getService(IScmService.class);
IItemServiceIterator changeSetIterator = ItemServiceIteratorFactory.INSTANCE.createFetchItemsIterator(itemService, changeSetHandles.toArray(new IItemHandle[changeSetHandles.size()]), IRepositoryItemService.COMPLETE);
while (changeSetIterator.hasNext()) {
IChangeSet changeSet = (IChangeSet) changeSetIterator.next();
if (changeSet == null) {
continue;
}
for (IChange change : (List<IChange>) changeSet.changes()) {
if (change.kind() == IChange.ADD || change.kind() == IChange.MODIFY || change.kind() == IChange.DELETE)
{
IVersionableHandle versionableHandle = change.beforeState();
ServiceConfigurationProvider configProvider = ServiceConfigurationProvider.FACTORY.create(
data.getDestWorkspace(), changeSet.getComponent());
IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
if(item instanceof IFolder)
{
String path = "\"+((IFolder)item).getName();
IVersionableHandle parent = ((IFolder)item).getParent();
System.out.println(" Folder names is" + path);
System.out.println(" Folder parent names is" + parent);
}
else if(item instanceof IFileItem)
{
String path = "\"+((IFileItem)item).getName();
IVersionableHandle parent = ((IFileItem)item).getParent();
System.out.println(" File names is" + path);
System.out.println(" File parent names is" + parent);
}
IAncestorReport reports[] = scmService.configurationLocateAncestors(
configProvider, new IVersionableHandle[] {versionableHandle}, null, null);
IAdvisorInfo info = collector.createProblemInfo(
"Deliver of deleted files is prohibited",
"The file " + toFullPath(item, reports[0].getNameItemPairs()) + " is not allowed to be deleted.",
"error");
collector.addInfo(info);
}
}
}
}
}
private String toFullPath(IVersionable versionable, List<INameItemPair> segments) {
StringBuffer path = new StringBuffer();
if(segments.isEmpty()) {
return versionable.getName();
}
for (INameItemPair nameItemPair : segments) {
if(path.length() != 0) {
path.append(Path.SEPARATOR);
}
// ignore the root folder which doesn't have a name
if(nameItemPair.getName() != null)
path.append(nameItemPair.getName());
}
return path.toString();
}
}
Thanks.
I use both com.ibm.team.filesystem.common.IFileItem and com.ibm.team.scm.common.IFolderHandle on my server side script. I don't know of any limitation between client and server in this case.
Hi Michele,
Then could it be a problem with my pre-requisites? Because i got errors when using these methods in my server side plugin.
Could you put your associated plugin.xml code here?
Thanks.
Where do you have the errors? And which exceptions do you have?
Sorry, made it an advisor and i think thats the reason...I will modify it and check.
For stream name once you've the IWorkspaceHandle (from data.getDestWorkspace()) you can easily get the IWorkspace object (using IRepositoryItemService) and then use .getName() method.
IScmDeltaSource deltaSource = (IScmDeltaSource) operationData;
IScmItemService scmItemService = getService(IScmItemService.class);
for (IChangeHistoryModificationDelta mod : deltaSource.getDeltas(IChangeHistoryModificationDelta.class)) {
IUpdateReport updateReport = mod.getUpdateReport();
for (IComponentHandle comp : updateReport.getAffectedComponents()) {
Collection<IItemUpdateReport> updatesForComponent = updateReport.getUpdatesForComponent(comp);
//...</pre>
I have implemented this plugin and it works fine for modify/delete as there is a before and an after state. sample code that i used to get the path is as below:
for (IChange change : (List<IChange>) changeSet.changes()) {
ServiceConfigurationProvider configProvider = ServiceConfigurationProvider.FACTORY.create(data.getDestWorkspace(), changeSet.getComponent());
IVersionableHandle versionableHandle = change.beforeState();
IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
IAncestorReport reports[] = scmService.configurationLocateAncestors(
configProvider, new IVersionableHandle[] {versionableHandle}, null, null);
String Path = toFullPath(item, reports[0].getNameItemPairs());
but in case of add operation, the value of versionableHandle is null and so the above code does not work. Could you please suggest how this can be achieved for an add operation?
M
Comments
The handle is null because of the configuration provider you've selected. In case of an add operation the element is not already present in the target stream so you have to use data.getSourceWorkspace() in order to have the object.
Thanks Michelle. Could you please provide a sample code for the add operation?
Versionable handle looks to be retrieving a null value (or) is it the problem only because of config provider?
Your snippet seems to be ok, in case of add operation (you can obtain change kind by IChange.getKind() and compare it with IChange.ADD) you have to select a different config provider.
Hi Michele,
The plugin works fine now. But at times, it fails to run giving the following error:
http-9443-Processor44] WARN com.ibm.team.filesystem - Unhandled Exception
java.io.IOException: Stream closed
And when i restart the system(local machine - where my JTS resides), it starts working again. there is no problem with plugin activation, but it fails at times - i can say after 2 or 3 runs.
Please advise.
Thanks.
Comments
David Olsen
JAZZ DEVELOPER Jun 21 '12, 11:45 a.m.Are you trying to limit read access or write access to those folders? I am guessing write access, that you want to prevent users from delivering changes to those folders. But your question isn't clear about that.
VK L
Jun 22 '12, 11:36 a.m.Hi David,
I am trying to restrict write access to the folders.
VK L
Jul 02 '12, 10:00 a.m.My requirement is that i should restrict add operations on a particular path for certain users.
Thanks.