RTC v4.0.1+ Java API: how to load a workspace using Java API
Hi
I find my self struggling with a simple task:
given an RTC repository and a ready made workspace I want to download its content.
getting the repository instance was easy (there are online samples to do login):
ITeamRepository repository = TeamPlatform.getTeamRepositoryService().getTeamRepository(rtcServerURL);
... then code here perform a login...
then get an IWorkspaceConnection using
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repository)
IWorkspaceSearchCriteria criteria = IWorkspaceSearchCriteria.FACTORY.newInstance();
criteria.setExactName(rtcWorkspace);
criteria.setKind(IWorkspaceSearchCriteria.WORKSPACES);
... and then run a findWorkspace...
or maybe simply run :
ISharingManager sharingManager = FileSystemCore.getSharingManager();
File workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
PathLocation pathlocation = new PathLocation(workspaceRoot.getAbsolutePath());
ILocation sandBoxLocation = pathlocation.getCanonicalForm();
ISandbox sandbox = sharingManager.getSandbox(sandBoxLocation, false);
IRelativeLocation relativeLocation = new RelativeLocation(new String[0]);
Collection<ILoadRule> paramCollection = null;
ILoadOperation loadOp = IOperationFactory.instance.getLoadOperation(p);
loadOp.requestLoad(ISandbox paramISandbox, IRelativeLocation paramIRelativeLocation, Collection<ILoadRule> paramCollection, IProgressMonitor paramIProgressMonitor)
however, I'm not sure what to put in the params for the simplest load command.
can anyone help here ?
I find my self struggling with a simple task:
given an RTC repository and a ready made workspace I want to download its content.
getting the repository instance was easy (there are online samples to do login):
ITeamRepository repository = TeamPlatform.getTeamRepositoryService().getTeamRepository(rtcServerURL);
... then code here perform a login...
then get an IWorkspaceConnection using
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repository)
IWorkspaceSearchCriteria criteria = IWorkspaceSearchCriteria.FACTORY.newInstance();
criteria.setExactName(rtcWorkspace);
criteria.setKind(IWorkspaceSearchCriteria.WORKSPACES);
... and then run a findWorkspace...
or maybe simply run :
ISharingManager sharingManager = FileSystemCore.getSharingManager();
File workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
PathLocation pathlocation = new PathLocation(workspaceRoot.getAbsolutePath());
ILocation sandBoxLocation = pathlocation.getCanonicalForm();
ISandbox sandbox = sharingManager.getSandbox(sandBoxLocation, false);
IRelativeLocation relativeLocation = new RelativeLocation(new String[0]);
Collection<ILoadRule> paramCollection = null;
ILoadOperation loadOp = IOperationFactory.instance.getLoadOperation(p);
loadOp.requestLoad(ISandbox paramISandbox, IRelativeLocation paramIRelativeLocation, Collection<ILoadRule> paramCollection, IProgressMonitor paramIProgressMonitor)
however, I'm not sure what to put in the params for the simplest load command.
can anyone help here ?
4 answers
You seem to have figured out the sandbox for the operation.
The relative location is where you want the content loaded in your sandbox. You can give it null if you want to load in the sandbox root.
ILoadRuleFactory will provide you with a way to create your load rule object from the file to pass to the operation.
The relative location is where you want the content loaded in your sandbox. You can give it null if you want to load in the sandbox root.
ILoadRuleFactory will provide you with a way to create your load rule object from the file to pass to the operation.
Comments
here is what I did to load an existing workspace (need to attach the XML load rule file too)
public void loginAndLoad(IProgressMonitor monitor)
{
WorkspaceJob activation = new WorkspaceJob("Login to RTC Server ") {
@Override
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
try {
repo = login(monitor);
if (repo != null) {
IWorkspaceConnection wrkspace = getRepositoryWorkspace(repo,monitor);
loadWorkspace(wrkspace,monitor);
}
if (repo != null) {
return Status.OK_STATUS;
}
} catch (TeamRepositoryException ex) {
ex.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return Status.CANCEL_STATUS;
}
};
activation.setRule(ResourcesPlugin.getWorkspace().getRoot());
activation.schedule();
}
ITeamRepository login(IProgressMonitor monitor) throws TeamRepositoryException {
if (!TeamPlatform.isStarted()) {
TeamPlatform.startup();
}
ITeamRepository repository = TeamPlatform.getTeamRepositoryService().getTeamRepository(rtcServerURL);
repository.registerLoginHandler(new ITeamRepository.ILoginHandler() {
public ILoginInfo challenge(ITeamRepository repository) {
return new ILoginInfo() {
public String getUserId() {
return rtcUserName;
}
public String getPassword() {
return rtcPassword;
}
};
}
});
monitor.subTask("Contacting " + repository.getRepositoryURI() + "...");
repository.login(monitor);
System.out.println(repository.getState());
monitor.subTask("Connected");
return repository;
}
IWorkspaceConnection getRepositoryWorkspace(ITeamRepository repo, IProgressMonitor monitor)
throws TeamRepositoryException, ItemNotFoundException {
IWorkspaceConnection connection = null;
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
int maxSize = 4;
monitor.subTask("searching for desired workspace");
IWorkspaceSearchCriteria criteria = IWorkspaceSearchCriteria.FACTORY.newInstance();
//criteria.setPartialName("some word to search...");
criteria.setExactName(rtcWorkspace);
criteria.setKind(IWorkspaceSearchCriteria.WORKSPACES);
java.util.List<IWorkspaceHandle> workspaceList = wm.findWorkspaces(criteria, maxSize, monitor);
System.out.println("found " + workspaceList.size() + " matching workspaces");
if (workspaceList.size() == 1) {
IWorkspaceHandle handle = workspaceList.get(0);
connection = wm.getWorkspaceConnection(handle, monitor);
} else if (workspaceList.size() > 1){
System.out.println("search creteria was to common. serveral workspaces found:");
for (IWorkspaceHandle handle :workspaceList ) {
connection = wm.getWorkspaceConnection(handle, monitor);
String desc = connection.getDescription();
System.out.println("desc=" + desc + " name="+connection.getName());
}
} else {
//result search is empty.
System.out.println("search failed to find workspace named:"+rtcWorkspace);
}
return connection;
}
@SuppressWarnings("restriction")
void loadWorkspace(IWorkspaceConnection workspaceConnection,IProgressMonitor monitor) throws FileSystemException, TeamRepositoryException, FileNotFoundException
{
if (workspaceConnection == null) {
return;
}
ISharingManager sharingManager = FileSystemCore.getSharingManager();
File workspaceRoot = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
PathLocation pathlocation = new PathLocation(workspaceRoot.getAbsolutePath());
ILocation sandBoxLocation = pathlocation.getCanonicalForm();
ISandbox sandbox = sharingManager.getSandbox(sandBoxLocation, false);
LoadDilemmaHandler p = LoadDilemmaHandler.getDefault();
monitor.subTask("searching for load rules file");
//parameters for load request
String loadRulesAsXML = "loadrules4.0.2.xml"; //this is the file that was generated from Eclipse Jazz RTC tool (Preferences --> Team -->
InputStream ins = DevInstallerActivator.getDefault().getPluginResource(loadRulesAsXML);
if (ins == null) {
ins = DevInstallerActivator.getDefault().getClass().getResourceAsStream(loadRulesAsXML);
}
if (ins != null) {
Reader xmlReader = new InputStreamReader(ins) ;
ILoadRule2 rule = ILoadRuleFactory.loadRuleFactory.getLoadRule(workspaceConnection, xmlReader, monitor);
ILoadOperation loadoperator = rule.getLoadOp(sandbox, p, monitor);
monitor.subTask("loading files from RTC server...");
loadoperator.run(monitor);
} else {
throw new FileNotFoundException(loadRulesAsXML);
}
}
Comments
Evan Hughes
JAZZ DEVELOPER Aug 20 '13, 11:04 a.m.