- viewer.setContentProvider(new DistributionsContentProvider());
- }
-
- /*
- * UI MODEL
- */
- private class DistributionLabelProvider extends ColumnLabelProvider {
- @Override
- public String getText(Object element) {
- if (element instanceof BrowserElem)
- return ((BrowserElem) element).getLabel();
- else
- return element.toString();
- }
-
- @Override
- public Image getImage(Object element) {
- if (element instanceof BrowserElem) {
- BrowserElem bElement = (BrowserElem) element;
- if (bElement instanceof RepoElem) {
- if (bElement.isHomeRepo())
- return DistImages.IMG_HOME_REPO;
- else if (bElement.isReadOnly())
- return DistImages.IMG_REPO_READONLY;
- else
- return DistImages.IMG_REPO;
-
- } else if (bElement instanceof DistribGroupElem) {
- return DistImages.IMG_WKSP;
- }
- } else if (element instanceof DistributionElem)
- if (((DistributionElem) element).isReadOnly())
- return DistImages.IMG_DISTGRP_READONLY;
- else
- return DistImages.IMG_DISTGRP;
-
- return null;
- }
- }
-
- /** 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)) {
- if (RepoConstants.DEFAULT_JAVA_REPOSITORY_ALIAS
- .equals(repoNode.getName()))
- repositories
- .add(new RepoElem(repoNode, true, false));
- else if (repoNode.hasNode(ARGEO_PASSWORD))
- repositories.add(new RepoElem(repoNode));
- else
- repositories
- .add(new RepoElem(repoNode, false, true));
- }
- }
- } 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 BrowserElem) {
- return ((BrowserElem) parentElement).getChildren();
- } else if (parentElement instanceof DistributionElem) {
- return ((DistributionElem) parentElement).getChildren();
- }
- return null;
- }
-
- public Object getParent(Object element) {
- // TODO register repo elem in distribution elem?
- return null;
- }
-
- public boolean hasChildren(Object element) {
- if (element instanceof BrowserElem) {
- 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);
- }
- }
-
- /** Add some view specific behaviours to the comparator */
- private class BrowserElementComparator extends ArtifactNamesComparator {
- @Override
- public int category(Object element) {
- // Home repository always first
- if (element instanceof RepoElem
- && ((RepoElem) element).isHomeRepo())
- return 2;
- else
- return super.category(element);
- }
-
- @Override
- public int compare(Viewer viewer, Object e1, Object e2) {
- // reverse order for versions
- if (e1 instanceof DistributionElem)
- return -super.compare(viewer, e1, e2);
- else
- return super.compare(viewer, e1, e2);
- }
- }
-
- /** Abstract class to simplify UI conditions build */
- abstract class BrowserElem {
- private boolean isHomeRepo = false;
- private boolean isReadOnly = false;
-
- public BrowserElem(boolean isHomeRepo, boolean isReadOnly) {
- this.isHomeRepo = isHomeRepo;
- this.isReadOnly = isReadOnly;
- }
-
- public BrowserElem() {
- }
-
- public abstract String getLabel();
-
- public abstract Object[] getChildren();
-
- public void dispose() {
- }
-
- public boolean isHomeRepo() {
- return isHomeRepo;
- }
-
- public boolean isReadOnly() {
- return isReadOnly;
- }
- }
-
- /** A software repository */
- private class RepoElem extends BrowserElem {
- private Node repoNode;
- private Repository repository;
- private Credentials credentials;
-
- public RepoElem(Node repoNode, boolean isHomeRepo, boolean isReadOnly) {
- super(isHomeRepo, isReadOnly);
- this.repoNode = repoNode;
- }
-
- 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>();
- Map<String, DistribGroupElem> children = new HashMap<String, DistributionsView.DistribGroupElem>();
- for (String workspaceName : workspaceNames) {
- // filter technical workspaces
- // FIXME: rely on a more robust rule than just wksp name
- if (workspaceName.lastIndexOf('-') > 0) {
- String prefix = workspaceName.substring(0,
- workspaceName.lastIndexOf('-'));
- if (!repoNode.hasNode(workspaceName))
- repoNode.addNode(workspaceName);
- repoNode.getSession().save();
- if (!children.containsKey(prefix)) {
- children.put(prefix, new DistribGroupElem(
- RepoElem.this, prefix));
- }
- // FIXME remove deleted workspaces
- }
- }
- return children.values().toArray();
- } catch (RepositoryException e) {
- throw new SlcException(
- "Cannot list workspaces for " + repoNode, e);
- } finally {
- JcrUtils.logoutQuietly(session);
- }
- }
-
- 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;
- }
-
- public Node getRepoNode() {
- return repoNode;
- }
-
- }
-
- /**
- * Abstracts a group of distribution, that is a bunch of workspaces with
- * same prefix.
- */
- private class DistribGroupElem extends BrowserElem {
- private RepoElem repoElem;
- private String name;
-
- public DistribGroupElem(RepoElem repoElem, String prefix) {
- super(repoElem.isHomeRepo(), repoElem.isReadOnly());
- this.repoElem = repoElem;
- this.name = prefix;
- }
-
- public Object[] getChildren() {
- repoElem.connect();
- Session session = null;
- try {
- Repository repository = repoElem.getRepository();
- Node repoNode = repoElem.getRepoNode();
- session = repository.login(repoElem.getCredentials());
-
- String[] workspaceNames = session.getWorkspace()
- .getAccessibleWorkspaceNames();
- List<DistributionElem> distributionElems = new ArrayList<DistributionElem>();
- for (String workspaceName : workspaceNames) {
- // filter technical workspaces
- if (workspaceName.startsWith(name)) {
- Node workspaceNode = repoNode.hasNode(workspaceName) ? repoNode
- .getNode(workspaceName) : repoNode
- .addNode(workspaceName);
- distributionElems.add(new DistributionElem(repoElem,
- workspaceNode));
- // FIXME remove deleted workspaces
- }
- }
- return distributionElems.toArray();
- } catch (RepositoryException e) {
- throw new SlcException("Cannot list workspaces for prefix "
- + name, e);
- } finally {
- JcrUtils.logoutQuietly(session);
- }
- }
-
- public String getLabel() {
- return name;
- }
-
- public String toString() {
- return getLabel();
- }
-
- public void dispose() {
- }
-
- public RepoElem getRepoElem() {
- return repoElem;
- }
-
- }
-
- /** Abstracts a distribution, that is a workspace */
- private static class DistributionElem extends TreeParent {
- private final RepoElem repoElem;
- private final Node workspaceNode;
-
- /**
- * Helper to display only version when the workspace name is well
- * formatted
- */
- private static String formatName(Node workspaceNode) {
- String name = JcrUtils.getNameQuietly(workspaceNode);
- if (name != null && name.lastIndexOf('-') > 0)
- return name.substring(name.lastIndexOf('-') + 1);
- else
- return name;
- }
-
- public DistributionElem(RepoElem repoElem, Node workspaceNode) {
- super(formatName(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();
- }
-
- public boolean isReadOnly() {
- return repoElem.isReadOnly();
- }