]> git.argeo.org Git - gpl/argeo-slc.git/blob - plugins/org.argeo.slc.client.ui.dist/src/main/java/org/argeo/slc/client/ui/dist/providers/DistTreeContentProvider.java
e1f06c2f4d91a9310a7840a577ee605663884d3b
[gpl/argeo-slc.git] / plugins / org.argeo.slc.client.ui.dist / src / main / java / org / argeo / slc / client / ui / dist / providers / DistTreeContentProvider.java
1 package org.argeo.slc.client.ui.dist.providers;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jcr.Node;
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;
14
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;
28
29 /**
30 * Enables browsing in local and remote slc distribution repositories. Keyring
31 * and repository factory must be injected
32 */
33 public class DistTreeContentProvider implements ITreeContentProvider {
34 private Session nodeSession;
35 List<RepoElem> repositories = new ArrayList<RepoElem>();
36
37 private RepositoryFactory repositoryFactory;
38 private Keyring keyring;
39
40 public Object[] getElements(Object input) {
41 Repository nodeRepository = (Repository) input;
42 try {
43 if (nodeSession != null)
44 dispose();
45 nodeSession = nodeRepository.login();
46
47 String reposPath = UserJcrUtils.getUserHome(nodeSession).getPath()
48 + RepoConstants.REPOSITORIES_BASE_PATH;
49
50 if (!nodeSession.itemExists(reposPath))
51 initializeModel(nodeSession);
52
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));
64 else
65 repositories.add(new RepoElem(repoNode,
66 repositoryFactory, keyring, false, true));
67 }
68 }
69 } catch (RepositoryException e) {
70 throw new SlcException("Cannot get base elements", e);
71 }
72 return repositories.toArray();
73 }
74
75 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
76 }
77
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();
83 }
84 return null;
85 }
86
87 public Object getParent(Object element) {
88 // TODO register repo elem in distribution elem?
89 return null;
90 }
91
92 public boolean hasChildren(Object element) {
93 if (element instanceof DistParentElem) {
94 return true;
95 } else if (element instanceof WorkspaceElem) {
96 return false;
97 }
98 return false;
99 }
100
101 public void dispose() {
102 for (RepoElem repoElem : repositories)
103 repoElem.dispose();
104 repositories = new ArrayList<RepoElem>();
105 JcrUtils.logoutQuietly(nodeSession);
106 }
107
108 private void initializeModel(Session nodeSession) {
109 try {
110
111 Node homeNode = UserJcrUtils.getUserHome(nodeSession);
112 if (homeNode == null) // anonymous
113 throw new SlcException("User must be authenticated.");
114
115 // make sure base directory is available
116 Node repos = JcrUtils.mkdirs(nodeSession, homeNode.getPath()
117 + RepoConstants.REPOSITORIES_BASE_PATH);
118 nodeSession.save();
119
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);
132 nodeSession.save();
133 }
134 }
135 } catch (RepositoryException e) {
136 throw new SlcException("Cannot initialize model", e);
137 }
138 }
139
140 /*
141 * DEPENDENCY INJECTION
142 */
143 public void setRepositoryFactory(RepositoryFactory repositoryFactory) {
144 this.repositoryFactory = repositoryFactory;
145 }
146
147 public void setKeyring(Keyring keyring) {
148 this.keyring = keyring;
149 }
150 }