It's all about the answers!

Ask a question

RTC v4.0.1+ Java API: how to load a workspace using Java API


mordechi taitelman (46147) | asked Jul 03 '13, 11:28 a.m.
edited Jul 17 '13, 4:04 a.m.
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 ?

Comments
Evan Hughes commented Aug 20 '13, 11:04 a.m.
JAZZ DEVELOPER

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. 


Supported API calls are those with javadoc in the 'doc' directory of the SDK. 

4 answers



permanent link
mordechi taitelman (46147) | answered Jul 04 '13, 3:10 a.m.
actually my post is duplicate of https://jazz.net/forum/questions/114708/load-workspace-using-api

permanent link
Tim Mok (6.6k38) | answered Jul 04 '13, 9:31 a.m.
JAZZ DEVELOPER
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.

Comments
mordechi taitelman commented Jul 04 '13, 10:46 a.m. | edited Jul 04 '13, 10:50 a.m.

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)


Tim Mok commented Jul 04 '13, 11:04 a.m.
JAZZ DEVELOPER
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?


mordechi taitelman commented Jul 04 '13, 11:07 a.m.

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 ?


Tim Mok commented Jul 04 '13, 11:10 a.m.
JAZZ DEVELOPER

IWorkspaceManager#getWorkspaceConnection(IWorkspaceHandle, IProgressMonitor) would get you your connection.

The other call would create a new repository workspace, which isn't what you want.


permanent link
mordechi taitelman (46147) | answered Jul 07 '13, 7:24 a.m.
edited Jul 07 '13, 7:26 a.m.
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
mordechi taitelman commented Jul 07 '13, 7:28 a.m.

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>


mordechi taitelman commented Jul 17 '13, 4:07 a.m.

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.


permanent link
Stefano Antoniazzi (1701711) | answered May 11 '16, 9:59 a.m.
 Here you may find a trace:

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.