- viewer.setContentProvider(new DistributionsContentProvider());
- }
-
- public void setNodeRepository(Repository repository) {
- this.nodeRepository = repository;
- }
-
- public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
- this.repositoryFactory = repositoryFactory;
- }
-
- public void setKeyring(Keyring keyring) {
- this.keyring = keyring;
- }
-
- /*
- * INTERNAL CLASSES
- */
- /** Content provider */
- private class DistributionsContentProvider implements ITreeContentProvider {
- Session nodeSession;
- List<RepoElem> repositories = new ArrayList<RepoElem>();
-
- public Object[] getElements(Object input) {
- Repository nodeRepository = (Repository) input;
- try {
- if (nodeSession != null)
- dispose();
- nodeSession = nodeRepository.login();
-
- String reposPath = UserJcrUtils.getUserHome(nodeSession)
- .getPath() + RepoConstants.REPOSITORIES_BASE_PATH;
- NodeIterator repos = nodeSession.getNode(reposPath).getNodes();
- while (repos.hasNext()) {
- Node repoNode = repos.nextNode();
- if (repoNode.isNodeType(ArgeoTypes.ARGEO_REMOTE_REPOSITORY)) {
- repositories.add(new RepoElem(repoNode));
- }
- }
- } catch (RepositoryException e) {
- throw new SlcException("Cannot get base elements", e);
- }
- return repositories.toArray();
- }
-
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-
- }
-
- public Object[] getChildren(Object parentElement) {
- if (parentElement instanceof RepoElem) {
- return ((RepoElem) parentElement).getChildren();
- } else if (parentElement instanceof DistributionElem) {
- return ((DistributionElem) parentElement).getChildren();
- }
- return null;
- }
-
- public Object getParent(Object element) {
- // TODO register repo elem in distirbution elem?
- return null;
- }
-
- public boolean hasChildren(Object element) {
- if (element instanceof RepoElem) {
- return true;
- } else if (element instanceof DistributionElem) {
- return false;
- }
- return false;
- }
-
- public void dispose() {
- for (RepoElem repoElem : repositories)
- repoElem.dispose();
- repositories = new ArrayList<RepoElem>();
- JcrUtils.logoutQuietly(nodeSession);
- }
-
- }
-
- /** A software repository */
- private class RepoElem {
- private Node repoNode;
-
- private Repository repository;
- private Credentials credentials;
-
- public RepoElem(Node repoNode) {
- this.repoNode = repoNode;
- }
-
- /** Lazily connects to repository */
- protected void connect() {
- if (repository != null)
- return;
- repository = RepoUtils.getRepository(repositoryFactory, keyring,
- repoNode);
- credentials = RepoUtils.getRepositoryCredentials(keyring, repoNode);
- }
-
- public String getLabel() {
- try {
- if (repoNode.isNodeType(NodeType.MIX_TITLE)) {
- return repoNode.getProperty(Property.JCR_TITLE).getString();
- } else {
- return repoNode.getName();
- }
- } catch (RepositoryException e) {
- throw new SlcException("Cannot read label of " + repoNode, e);
- }
- }
-
- public String toString() {
- return repoNode.toString();
- }
-
- public Object[] getChildren() {
- connect();
- Session session = null;
- try {
- session = repository.login(credentials);
- String[] workspaceNames = session.getWorkspace()
- .getAccessibleWorkspaceNames();
- List<DistributionElem> distributionElems = new ArrayList<DistributionElem>();
- for (String workspaceName : workspaceNames) {
- Node workspaceNode = repoNode.hasNode(workspaceName) ? repoNode
- .getNode(workspaceName) : repoNode
- .addNode(workspaceName);
- repoNode.getSession().save();
- distributionElems.add(new DistributionElem(this,
- workspaceNode));
- // FIXME remove deleted workspaces
- }
- return distributionElems.toArray();
- } catch (RepositoryException e) {
- throw new SlcException(
- "Cannot list workspaces for " + repoNode, e);
- } finally {
- JcrUtils.logoutQuietly(session);
- }
- }
-
- public void dispose() {
- }
-
- public Node getRepoNode() {
- return repoNode;
- }
-
- public String getRepoPath() {
- try {
- return repoNode.getPath();
- } catch (RepositoryException e) {
- throw new SlcException("Cannot get path for " + repoNode, e);
- }
- }
-
- public Repository getRepository() {
- connect();
- return repository;
- }
-
- public Credentials getCredentials() {
- return credentials;
- }
-
- }
-
- /** Abstracts a distribution, that is a workspace */
- private static class DistributionElem extends TreeParent {
- private final RepoElem repoElem;
- private final Node workspaceNode;
-
- public DistributionElem(RepoElem repoElem, Node workspaceNode) {
- super(JcrUtils.getNameQuietly(workspaceNode));
- this.repoElem = repoElem;
- this.workspaceNode = workspaceNode;
- }
-
- public Node getWorkspaceNode() {
- return workspaceNode;
- }
-
- public String getWorkspaceName() {
- return JcrUtils.getNameQuietly(workspaceNode);
- }
-
- public String getWorkspacePath() {
- try {
- return workspaceNode.getPath();
- } catch (RepositoryException e) {
- throw new SlcException("Cannot get or add workspace path "
- + getWorkspaceName(), e);
- }
- }
-
- public String getRepoPath() {
- try {
- return workspaceNode.getParent().getPath();
- } catch (RepositoryException e) {
- throw new SlcException("Cannot get or add workspace path "
- + getWorkspaceName(), e);
- }
- }
-
- public RepoElem getRepoElem() {
- return repoElem;
- }
-
- public Credentials getCredentials() {
- return repoElem.getCredentials();
- }