RTC v4.0.1+ Java API: how to load a workspace using Java API
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
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
Indeed ILoadRuleFactory class has a static factory to read files.
ILoadRuleFactory.loadRuleFactory.getLoadRule(connection, input, progress)
but it requires a IWorkspaceConnection. How do I get from an ITeamRepository an IWorkspaceConnection ?
I mean I saw I can:
IWorkspaceManager wm = SCMPlatform.getWorkspaceManager(repo);
IWorkspaceConnection workspace = wm.createWorkspace(repo.loggedInContributor(), "Moti-Dependencies", "branch for the POC proof2", monitor);
but I'm afraid this code will create a new workspace . I want to load an existing workspace.
(assuming I want to load an existing workspace which was predefined in Concert Team client)
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);
You already wrote the code to get those. Is there something else that you're missing?
the search result is :
java.util.List<IWorkspaceHandle> workspaceList = wm.findWorkspaces(criteria, maxSize, monitor);
how can I get the IWorkspaceConnection from an IWorkspaceHandle ?
is IWorkspaceManager.createWorkspace really creates a new entity in the repository - or is it only a memory representation of an existing workspace ?
IWorkspaceManager#getWorkspaceConnection(IWorkspaceHandle, IProgressMonitor) would get you your connection.
The other call would create a new repository workspace, which isn't what you want.
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
and the loadrule XML file content is:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!--Built from the sandbox "/home/mordechi/dev/auto-dev-install" and the workspace "moti-master6.0-Workspace"-->
<!--Generated: 2013-07-04 16.08.45-->
<scm:sourceControlLoadRule eclipseProjectOptions="import" version="1" xmlns:scm="http://com.ibm.team.scm">
<parentLoadRule>
<component name="core"/>
<parentFolder repositoryPath="/" />
</parentLoadRule>
<parentLoadRule>
<component name="common"/>
<parentFolder repositoryPath="/"/>
</parentLoadRule>
</scm:sourceControlLoadRule>
As an IBMer i find it sad there are not enough samples available online.
The whole Java API area is missing many samples. Indeed you can download the JavaDoc but there is nothing like a good old code to explain simple/common tasks.
It can save hours/days for beginners in the RTC area.
Comments
Evan Hughes
JAZZ DEVELOPER Aug 20 '13, 11:04 a.m.The filesystem operations aren't supported API yet. We are formalizing them, but we don't have an expected date for their release. In the meantime you are encouraged to use the commandline client.