1 package org
.argeo
.slc
.client
.ui
.dist
.providers
;
3 import java
.util
.ArrayList
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.RepositoryFactory
;
12 import javax
.jcr
.Session
;
13 import javax
.jcr
.nodetype
.NodeType
;
15 import org
.argeo
.jcr
.ArgeoJcrUtils
;
16 import org
.argeo
.jcr
.ArgeoNames
;
17 import org
.argeo
.jcr
.ArgeoTypes
;
18 import org
.argeo
.jcr
.JcrUtils
;
19 import org
.argeo
.jcr
.UserJcrUtils
;
20 import org
.argeo
.slc
.SlcException
;
21 import org
.argeo
.slc
.client
.ui
.dist
.model
.DistParentElem
;
22 import org
.argeo
.slc
.client
.ui
.dist
.model
.RepoElem
;
23 import org
.argeo
.slc
.client
.ui
.dist
.model
.WorkspaceElem
;
24 import org
.argeo
.slc
.repo
.RepoConstants
;
25 import org
.argeo
.util
.security
.Keyring
;
26 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
27 import org
.eclipse
.jface
.viewers
.Viewer
;
30 * Enables browsing in local and remote slc distribution repositories. Keyring
31 * and repository factory must be injected
33 public class DistTreeContentProvider
implements ITreeContentProvider
{
34 private Session nodeSession
;
35 List
<RepoElem
> repositories
= new ArrayList
<RepoElem
>();
37 private RepositoryFactory repositoryFactory
;
38 private Keyring keyring
;
40 public Object
[] getElements(Object input
) {
41 Repository nodeRepository
= (Repository
) input
;
43 if (nodeSession
!= null)
45 nodeSession
= nodeRepository
.login();
47 String reposPath
= UserJcrUtils
.getUserHome(nodeSession
).getPath()
48 + RepoConstants
.REPOSITORIES_BASE_PATH
;
50 if (!nodeSession
.itemExists(reposPath
))
51 initializeModel(nodeSession
);
53 NodeIterator repos
= nodeSession
.getNode(reposPath
).getNodes();
54 while (repos
.hasNext()) {
55 Node repoNode
= repos
.nextNode();
56 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
57 if (RepoConstants
.DEFAULT_JAVA_REPOSITORY_ALIAS
58 .equals(repoNode
.getName()))
59 repositories
.add(new RepoElem(repoNode
,
60 repositoryFactory
, keyring
, true, false));
61 else if (repoNode
.hasNode(ArgeoNames
.ARGEO_PASSWORD
))
62 repositories
.add(new RepoElem(repoNode
,
63 repositoryFactory
, keyring
));
65 repositories
.add(new RepoElem(repoNode
,
66 repositoryFactory
, keyring
, false, true));
69 } catch (RepositoryException e
) {
70 throw new SlcException("Cannot get base elements", e
);
72 return repositories
.toArray();
75 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
78 public Object
[] getChildren(Object parentElement
) {
79 if (parentElement
instanceof DistParentElem
) {
80 return ((DistParentElem
) parentElement
).getChildren();
81 } else if (parentElement
instanceof WorkspaceElem
) {
82 return ((WorkspaceElem
) parentElement
).getChildren();
87 public Object
getParent(Object element
) {
88 // TODO register repo elem in distribution elem?
92 public boolean hasChildren(Object element
) {
93 if (element
instanceof DistParentElem
) {
95 } else if (element
instanceof WorkspaceElem
) {
101 public void dispose() {
102 for (RepoElem repoElem
: repositories
)
104 repositories
= new ArrayList
<RepoElem
>();
105 JcrUtils
.logoutQuietly(nodeSession
);
108 private void initializeModel(Session nodeSession
) {
111 Node homeNode
= UserJcrUtils
.getUserHome(nodeSession
);
112 if (homeNode
== null) // anonymous
113 throw new SlcException("User must be authenticated.");
115 // make sure base directory is available
116 Node repos
= JcrUtils
.mkdirs(nodeSession
, homeNode
.getPath()
117 + RepoConstants
.REPOSITORIES_BASE_PATH
);
120 // register default local java repository
121 String alias
= RepoConstants
.DEFAULT_JAVA_REPOSITORY_ALIAS
;
122 Repository javaRepository
= ArgeoJcrUtils
.getRepositoryByAlias(
123 repositoryFactory
, alias
);
124 if (javaRepository
!= null) {
125 if (!repos
.hasNode(alias
)) {
126 Node repoNode
= repos
.addNode(alias
,
127 ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
);
128 repoNode
.setProperty(ArgeoNames
.ARGEO_URI
, "vm:///" + alias
);
129 repoNode
.addMixin(NodeType
.MIX_TITLE
);
130 repoNode
.setProperty(Property
.JCR_TITLE
,
131 RepoConstants
.DEFAULT_JAVA_REPOSITORY_LABEL
);
135 } catch (RepositoryException e
) {
136 throw new SlcException("Cannot initialize model", e
);
141 * DEPENDENCY INJECTION
143 public void setRepositoryFactory(RepositoryFactory repositoryFactory
) {
144 this.repositoryFactory
= repositoryFactory
;
147 public void setKeyring(Keyring keyring
) {
148 this.keyring
= keyring
;