It's all about the answers!

Ask a question

Problems accesing files content in a server-side plugin


Lorena Cortés (2654) | asked Nov 03 '11, 3:31 p.m.
Hi there,

I'm trying to get the content from a file (line per line) in a server-side plugin, but a get the next error:

java.lang.ClassCastException: com.ibm.team.filesystem.common.internal.impl.FileItemImpl incompatible with com.ibm.team.filesystem.common.IFileItem

I made this in a client-side code and this worked succesfull
**
IFileItem file = (IFileItem) versionable.getFullState();
IFileContent content = file.getContent();
IFileContentManager contentManager = FileSystemCore.getContentManager(repo);
**

This is my server-side code
-----------------------
IScmService scmService = getService(IScmService.class);

while (changeSetIterator.hasNext()) {

IChangeSet changeSet = (IChangeSet) changeSetIterator.next();
for (IChange change : (List<IChange>) changeSet.changes()) {

IVersionableHandle versionableHandle = change.afterState();
IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
IFilteItem file = (IFileItem)item;

}
}
--------------------------

sorry for my english

thanks in advance

9 answers



permanent link
Mehmet Ali Aydın (6133) | answered Nov 04 '11, 3:20 a.m.
You should check if it's a file or folder as sample code below.


IVersionable item = iconfig.fetchPartialItem(vhandle, Arrays.asList(properties), monitor);
IFolderHandle parent=null;

if(item instanceof IFolder)
{
path = "\\"+((IFolder)item).getName();
parent = ((IFolder)item).getParent();
}
else if(item instanceof IFileItem)
{
path = "\\"+((IFileItem)item).getName();
parent = ((IFileItem)item).getParent();
}




Hi there,

I'm trying to get the content from a file (line per line) in a server-side plugin, but a get the next error:

java.lang.ClassCastException: com.ibm.team.filesystem.common.internal.impl.FileItemImpl incompatible with com.ibm.team.filesystem.common.IFileItem

I made this in a client-side code and this worked succesfull
**
IFileItem file = (IFileItem) versionable.getFullState();
IFileContent content = file.getContent();
IFileContentManager contentManager = FileSystemCore.getContentManager(repo);
**

This is my server-side code
-----------------------
IScmService scmService = getService(IScmService.class);

while (changeSetIterator.hasNext()) {

IChangeSet changeSet = (IChangeSet) changeSetIterator.next();
for (IChange change : (List<IChange>) changeSet.changes()) {

IVersionableHandle versionableHandle = change.afterState();
IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
IFilteItem file = (IFileItem)item;

}
}
--------------------------

sorry for my english

thanks in advance

permanent link
Lorena Cortés (2654) | answered Nov 04 '11, 6:13 a.m.
In fact, it is a test file that i made: consulta.sql

I don't have an IConfiguration object in a server side plugin....right???



You should check if it's a file or folder as sample code below.


IVersionable item = iconfig.fetchPartialItem(vhandle, Arrays.asList(properties), monitor);
IFolderHandle parent=null;

if(item instanceof IFolder)
{
path = "\\"+((IFolder)item).getName();
parent = ((IFolder)item).getParent();
}
else if(item instanceof IFileItem)
{
path = "\\"+((IFileItem)item).getName();
parent = ((IFileItem)item).getParent();
}




Hi there,

I'm trying to get the content from a file (line per line) in a server-side plugin, but a get the next error:

java.lang.ClassCastException: com.ibm.team.filesystem.common.internal.impl.FileItemImpl incompatible with com.ibm.team.filesystem.common.IFileItem

I made this in a client-side code and this worked succesfull
**
IFileItem file = (IFileItem) versionable.getFullState();
IFileContent content = file.getContent();
IFileContentManager contentManager = FileSystemCore.getContentManager(repo);
**

This is my server-side code
-----------------------
IScmService scmService = getService(IScmService.class);

while (changeSetIterator.hasNext()) {

IChangeSet changeSet = (IChangeSet) changeSetIterator.next();
for (IChange change : (List<IChange>) changeSet.changes()) {

IVersionableHandle versionableHandle = change.afterState();
IVersionable item = (IVersionable) scmService.fetchState(versionableHandle, null, null);
IFilteItem file = (IFileItem)item;

}
}
--------------------------

sorry for my english

thanks in advance

permanent link
sam detweiler (12.4k6182201) | answered Nov 04 '11, 12:01 p.m.
In my server plugin I do (I am only reading the first 255 bytes of the file)


// output stream handle
OutputStream os=null;
// get the content manager
IContentManager contentManager= ((ITeamRepository) attachment.getOrigin()).contentManager();
os= new ByteArrayOutputStream(256);
// read a little file data
contentManager.retrieveContent(attachment.getContent(), os, monitor);


Sam

permanent link
Lorena Cortés (2654) | answered Nov 04 '11, 3:31 p.m.
ok, but then how can I get ITeamRepository and attachment ???

in my code I'm not available to get ITeamRepository. Instead of ITeamRepository I get IRepositoryItemService:

IRepositoryItemService itemService = getService(IRepositoryItemService.class);

Here is the code (I copied it)
===========================================
public class DeliverParticipant extends AbstractService implements
IOperationParticipant {

@SuppressWarnings({ "unchecked", "null" })
public void run(AdvisableOperation arg0, IProcessConfigurationElement arg1,
IParticipantInfoCollector arg2, IProgressMonitor arg3)
throws TeamRepositoryException {

Object operationData = arg0.getOperationData();
if (!(operationData instanceof DeliverOperationData)) {
return;
}

IScmService scmService = getService(IScmService.class);
IRepositoryItemService itemService = getService(IRepositoryItemService.class);

DeliverOperationData deliverData = (DeliverOperationData) operationData;

// Foundation baselines (when stream is created)
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++");
IWorkspace destWorkspace = deliverData.getDestWorkspace();
System.out.println("Stream Name: " + destWorkspace.getName());
ComponentEntry[] entries = scmService.getComponentEntries(destWorkspace, null);
for (int i = 0; i < entries.length; i++) {
ComponentEntry componentEntry = entries;
IComponent c = (IComponent)itemService.fetchItem(componentEntry.getComponent(), null);
IBaselineHandle baseline = componentEntry.getBasis();
IBaseline b = (IBaseline) itemService.fetchItem(baseline, null);
System.out.println(c.getName() + " => " + b.getId() + " : " + b.getName());
}

// The owner and creator (team area and process for the stream)
if(destWorkspace.isStream()) {
IAuditable owner = (IAuditable) itemService.fetchItem(destWorkspace.getOwner(), null);
if(owner instanceof IProcessArea) {
System.out.println("Owner: " + ((IProcessArea)owner).getName());
}
}

List<IChangeSetHandle> changeSetHandles = deliverData.getChangeSetHandles();
IItemServiceIterator changeSetIterator = ItemServiceIteratorFactory.INSTANCE.createFetchItemsIterator(itemService, changeSetHandles.toArray(new IItemHandle), IRepositoryItemService.COMPLETE);

while (changeSetIterator.hasNext()) {
IChangeSet changeSet = (IChangeSet) changeSetIterator.next();
if (changeSet == null) {
// don't have read access to the change set
continue;
}
for (IChange change : (List<IChange>) changeSet.changes()) {
if (((change.kind() & IChange.MODIFY) == IChange.MODIFY)) {
extractChangeInformation(scmService, itemService, deliverData, changeSet, change);
}
}
}
}

private void extractChangeInformation(IScmService scm, IRepositoryItemService repo, DeliverOperationData data, IChangeSet cs, IChange change) throws TeamRepositoryException {
String comment = cs.getComment();
String creator = ((IContributor)repo.fetchItem(cs.getAuthor(), null)).getEmailAddress();
String date = cs.getLastChangeDate().toString();


IServerSideVersionedContentService contentService = getService(IServerSideVersionedContentService.class);
ServiceConfigurationProvider cfgs = ServiceConfigurationProvider.FACTORY.create(data.getDestWorkspace(), cs.getComponent());
IVersionableHandle afterStateHandle = change.afterState();
IVersionable afterState = scm.fetchState(afterStateHandle, null, null);
IVersionableHandle beforeStateHandle = change.beforeState();
IVersionable beforeState = scm.fetchState(beforeStateHandle, null, null);
if (afterState instanceof IFileItem) {
IFileItem file = (IFileItem) afterState;
IAncestorReport[] reports = scm.configurationDetermineAncestorsInHistory(cfgs, new IVersionableHandle[] {file}, null, null);

System.out.println("=======================================================");
System.out.println(getPath(reports));
System.out.println(comment);
System.out.println(creator);
System.out.println(date);
//System.err.println(Location.itemLocation(cs, getRepositoryURL()).toAbsoluteUri());
printContents("Before file", (IFileItemHandle) beforeState, ((IFileItem)beforeState).getContent(), contentService);
printContents("After file", (IFileItemHandle) afterState, ((IFileItem)afterState).getContent(), contentService);
}else if (afterState instanceof IFolder){
System.out.println("es carpeta");
}else if (afterState instanceof FileItemImpl){
System.out.println("es un FileItemImpl");
}else{
System.out.println("no es nada");
}

}

private String getPath(IAncestorReport report) {
StringBuilder sb = new StringBuilder();
Iterator i = report.getNameItemPairs().iterator();
if (i.hasNext()) {
i.next(); // discard the pair representing the root folder, since
// it carries no name
while (i.hasNext()) {
INameItemPair pair = (INameItemPair) i.next();
sb.append("/"); //$NON-NLS-1$
sb.append(pair.getName());
}
}
return sb.toString();
}

private void printContents(String prefix, IFileItemHandle file, IFileContent content, com.ibm.team.scm.service.IServerSideVersionedContentService contentService) throws TeamRepositoryException {
System.err.println(prefix + ":");
ByteArrayOutputStream outputStream = null;
try {
outputStream = new ByteArrayOutputStream();
contentService.fetchContentTrusted(file, content.getHash(), System.err);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// Try to close the stream. If it fails, do nothing.
}
}
}
}
=================================================


in extractChangeInformation() method, the line:

"if (afterState instanceof IFileItem) " is false because afterState is an instance of FileItemImpl

thanks

permanent link
Lorena Cortés (2654) | answered Nov 24 '11, 6:44 a.m.
Could you please put the complete code????





In my server plugin I do (I am only reading the first 255 bytes of the file)


// output stream handle
OutputStream os=null;
// get the content manager
IContentManager contentManager= ((ITeamRepository) attachment.getOrigin()).contentManager();
os= new ByteArrayOutputStream(256);
// read a little file data
contentManager.retrieveContent(attachment.getContent(), os, monitor);


Sam

permanent link
sam detweiler (12.4k6182201) | answered Nov 24 '11, 10:31 a.m.
the AuditableCommon object in the SaveParameters has a retrieveContent method

void retrieveContent(IContent content, OutputStream outStream, IProgressMonitor monitor) throws TeamRepositoryException;

and I can retrieve that thru the OperationAdvisor parameters

IAuditableCommon iac = ((ISaveParameter) data).getSaveOperationParameter().getAuditableCommon();

Sam

permanent link
Pancha Gyaneswari Yelika (45811) | answered Feb 11 '13, 7:49 a.m.
 Hi,

I am writing a server-side operation advisor on work item save.

On saving the Work Item I am trying to retrieve the list of file changed for the work item. I am able to get the list file names from the changeset. But I need to get the full path of the files. 

In the above code
"ServiceConfigurationProvider cfgs = ServiceConfigurationProvider.FACTORY.create(data.getDestWorkspace(), cs.getComponent()); " the data object is an instance of DeliverOperationData. 

Since I have the Work Item save operation, the data is an instance of ISaveParameter. 
The question here is how do I get the data.getDestWorkspace() from Work Item or IsaveParameter. Or is there any other approach to get the ServiceConfigurationProvider  instance.


public void run(AdvisableOperation operation,
IProcessConfigurationElement advisorConfiguration,
IAdvisorInfoCollector collector, IProgressMonitor monitor)
throws TeamRepositoryException {

Object data = operation.getOperationData();
if (data instanceof ISaveParameter) {
.......
....
while (changeSetIterator.hasNext()) {
IChangeSet changeSet = (IChangeSet) changeSetIterator.next();

for (Object obj : changeSet.changes()) {
IChange change = (IChange) obj;

if (change.kind() != IChange.DELETE) {
IVersionableHandle after = change.afterState();
/*ServiceConfigurationProvider configProvider = ServiceConfigurationProvider.FACTORY.create(
data.getDestWorkspace(), changeSet.getComponent());*/
IVersionable item = (IVersionable) scmService.fetchState(
after, null, null);
/*IAncestorReport reports[] = scmService.configurationLocateAncestors(
configProvider, new IVersionableHandle[] {after}, null, null*/);
changedFilesAndFolders.add(item.getName());
}
}
          }

Please help. 

Thanks 


permanent link
Saritha Yadav (134) | answered Jul 01 '15, 2:57 a.m.
We are using DeliverOperationData to access changeset But the code is breaking.

if (data instanceof ISaveParameter) {

DeliverOperationData data = (DeliverOperationData) operation.getOperationData();

}

can you please suggest on this.

permanent link
Saritha Yadav (134) | answered Jul 01 '15, 2:58 a.m.
Since I have the Work Item save operation, the data is an instance of ISaveParameter. 
The question here is how do I get the data.getDestWorkspace() from Work Item or IsaveParameter. Or is there any other approach to get the ServiceConfigurationProvider  instance.

Is there any update on this question??

Your answer


Register or to post your answer.