1 package org
.argeo
.slc
.client
.ui
.dist
;
3 import javax
.jcr
.Credentials
;
4 import javax
.jcr
.GuestCredentials
;
6 import javax
.jcr
.NodeIterator
;
7 import javax
.jcr
.Property
;
8 import javax
.jcr
.PropertyIterator
;
9 import javax
.jcr
.Repository
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.RepositoryFactory
;
12 import javax
.jcr
.SimpleCredentials
;
13 import javax
.jcr
.nodetype
.NodeType
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.ArgeoException
;
18 import org
.argeo
.jcr
.ArgeoJcrUtils
;
19 import org
.argeo
.jcr
.ArgeoNames
;
20 import org
.argeo
.jcr
.ArgeoTypes
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.slc
.SlcException
;
23 import org
.argeo
.slc
.jcr
.SlcNames
;
24 import org
.argeo
.slc
.jcr
.SlcTypes
;
25 import org
.argeo
.util
.security
.Keyring
;
27 /** Static utilities */
28 public class DistUiUtils
implements ArgeoNames
, SlcNames
{
29 private final static Log log
= LogFactory
.getLog(DistUiUtils
.class);
31 /** Retrieve repository based on information in the repo node */
32 public static Repository
getRepository(RepositoryFactory repositoryFactory
,
33 Keyring keyring
, Node repoNode
) {
35 Repository repository
;
36 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
37 String uri
= repoNode
.getProperty(ARGEO_URI
).getString();
38 if (uri
.startsWith("http")) {// http, https
39 repository
= ArgeoJcrUtils
.getRepositoryByUri(
40 repositoryFactory
, uri
);
43 repository
= ArgeoJcrUtils
.getRepositoryByAlias(
44 repositoryFactory
, alias
);
48 throw new SlcException("Unsupported node type " + repoNode
);
50 } catch (RepositoryException e
) {
51 throw new SlcException("Cannot connect to repository " + repoNode
,
58 * Reads credentials from node, using keyring if there is a password. Cann
59 * return null if no credentials needed (local repo) at all, but returns
60 * {@link GuestCredentials} if user id is 'anonymous' .
62 public static Credentials
getRepositoryCredentials(Keyring keyring
,
65 if (repoNode
.isNodeType(ArgeoTypes
.ARGEO_REMOTE_REPOSITORY
)) {
66 if (!repoNode
.hasProperty(ARGEO_USER_ID
))
69 String userId
= repoNode
.getProperty(ARGEO_USER_ID
).getString();
70 if (userId
.equals("anonymous"))// FIXME hardcoded userId
71 return new GuestCredentials();
72 char[] password
= keyring
.getAsChars(repoNode
.getPath() + '/'
74 Credentials credentials
= new SimpleCredentials(userId
,
78 throw new SlcException("Unsupported node type " + repoNode
);
80 } catch (RepositoryException e
) {
81 throw new SlcException("Cannot connect to repository " + repoNode
,
87 * Custom copy since the one in commons does not fit the needs when copying
88 * a workspace completely.
90 public static void copy(Node fromNode
, Node toNode
) {
92 if (log
.isDebugEnabled())
93 log
.debug("copy node :" + fromNode
.getPath());
95 // FIXME : small hack to enable specific workspace copy
96 if (fromNode
.isNodeType("rep:ACL")
97 || fromNode
.isNodeType("rep:system")) {
98 if (log
.isTraceEnabled())
99 log
.trace("node " + fromNode
+ " skipped");
104 for (NodeType mixinType
: fromNode
.getMixinNodeTypes()) {
105 toNode
.addMixin(mixinType
.getName());
109 for (NodeType mixinType
: toNode
.getMixinNodeTypes()) {
110 if (log
.isDebugEnabled())
111 log
.debug(mixinType
.getName());
114 // process properties
115 PropertyIterator pit
= fromNode
.getProperties();
116 properties
: while (pit
.hasNext()) {
117 Property fromProperty
= pit
.nextProperty();
118 String propName
= fromProperty
.getName();
120 String propertyName
= fromProperty
.getName();
121 if (toNode
.hasProperty(propertyName
)
122 && toNode
.getProperty(propertyName
).getDefinition()
126 if (fromProperty
.getDefinition().isProtected())
129 if (propertyName
.equals("jcr:created")
130 || propertyName
.equals("jcr:createdBy")
131 || propertyName
.equals("jcr:lastModified")
132 || propertyName
.equals("jcr:lastModifiedBy"))
135 if (fromProperty
.isMultiple()) {
136 toNode
.setProperty(propertyName
,
137 fromProperty
.getValues());
139 toNode
.setProperty(propertyName
,
140 fromProperty
.getValue());
142 } catch (RepositoryException e
) {
143 throw new ArgeoException("Cannot property " + propName
, e
);
147 // recursively process children nodes
148 NodeIterator nit
= fromNode
.getNodes();
149 while (nit
.hasNext()) {
150 Node fromChild
= nit
.nextNode();
151 Integer index
= fromChild
.getIndex();
152 String nodeRelPath
= fromChild
.getName() + "[" + index
+ "]";
154 if (toNode
.hasNode(nodeRelPath
))
155 toChild
= toNode
.getNode(nodeRelPath
);
157 toChild
= toNode
.addNode(fromChild
.getName(), fromChild
158 .getPrimaryNodeType().getName());
159 copy(fromChild
, toChild
);
162 // update jcr:lastModified and jcr:lastModifiedBy in toNode in
165 if (!toNode
.getDefinition().isProtected()
166 && toNode
.isNodeType(NodeType
.MIX_LAST_MODIFIED
))
167 JcrUtils
.updateLastModified(toNode
);
169 // Workaround to reduce session size: artifact is a saveable
171 if (toNode
.isNodeType(SlcTypes
.SLC_ARTIFACT
))
172 toNode
.getSession().save();
174 } catch (RepositoryException e
) {
175 throw new ArgeoException("Cannot copy " + fromNode
+ " to "
180 private DistUiUtils() {