Jazz Forum Welcome to the Jazz Community Forum Connect and collaborate with IBM Engineering experts and users

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 ?

0 votes

Comments

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
actually my post is duplicate of https://jazz.net/forum/questions/114708/load-workspace-using-api

0 votes


Permanent link
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.

0 votes

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.


Permanent link
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);
        }
       
    }

0 votes

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.


Permanent link
 Here you may find a trace:

0 votes

Your answer

Register or log in 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.

Search context
Follow this question

By Email: 

Once you sign in you will be able to subscribe for any updates here.

By RSS:

Answers
Answers and Comments
Question details
× 10,935
× 457
× 89
× 57
× 51

Question asked: Jul 03 '13, 11:28 a.m.

Question was seen: 12,996 times

Last updated: May 11 '16, 9:59 a.m.

Confirmation Cancel Confirm