2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.jcr
;
18 import java
.util
.Calendar
;
19 import java
.util
.GregorianCalendar
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.RepositoryException
;
24 import javax
.jcr
.Session
;
26 import org
.argeo
.jcr
.JcrUtils
;
27 import org
.argeo
.jcr
.UserJcrUtils
;
28 import org
.argeo
.slc
.SlcException
;
29 import org
.argeo
.slc
.core
.execution
.PrimitiveAccessor
;
30 import org
.argeo
.slc
.core
.execution
.PrimitiveUtils
;
31 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
32 import org
.argeo
.slc
.test
.TestStatus
;
35 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
36 * (convention over configuration) for optimization purposes.
38 public class SlcJcrUtils
implements SlcNames
{
39 public final static Integer AGENT_FACTORY_DEPTH
= 3;
41 /** Extracts the path of a flow relative to its execution module */
42 public static String
flowRelativePath(String fullFlowPath
) {
43 String
[] tokens
= fullFlowPath
.split("/");
44 StringBuffer buf
= new StringBuffer(fullFlowPath
.length());
45 for (int i
= AGENT_FACTORY_DEPTH
+ 3; i
< tokens
.length
; i
++) {
46 buf
.append('/').append(tokens
[i
]);
48 return buf
.toString();
51 /** Extracts the path to the related execution module */
52 public static String
modulePath(String fullFlowPath
) {
53 String
[] tokens
= fullFlowPath
.split("/");
54 StringBuffer buf
= new StringBuffer(fullFlowPath
.length());
55 for (int i
= 0; i
< AGENT_FACTORY_DEPTH
+ 3; i
++) {
56 if (!tokens
[i
].equals(""))
57 buf
.append('/').append(tokens
[i
]);
59 return buf
.toString();
62 /** Extracts the module name from a flow path */
63 public static String
moduleName(String fullFlowPath
) {
64 String
[] tokens
= fullFlowPath
.split("/");
65 String moduleName
= tokens
[AGENT_FACTORY_DEPTH
+ 2];
66 moduleName
= moduleName
.substring(0, moduleName
.indexOf('_'));
70 /** Module node name based on module name and version */
71 public static String
getModuleNodeName(ModuleDescriptor moduleDescriptor
) {
72 return moduleDescriptor
.getName() + "_" + moduleDescriptor
.getVersion();
75 /** Extracts the agent factory of a flow */
76 public static String
flowAgentFactoryPath(String fullFlowPath
) {
77 String
[] tokens
= fullFlowPath
.split("/");
78 StringBuffer buf
= new StringBuffer(fullFlowPath
.length());
79 // first token is always empty
80 for (int i
= 1; i
< AGENT_FACTORY_DEPTH
+ 1; i
++) {
81 buf
.append('/').append(tokens
[i
]);
83 return buf
.toString();
86 /** Create a new execution process path based on the current time */
87 public static String
createExecutionProcessPath(Session session
, String uuid
) {
88 Calendar now
= new GregorianCalendar();
89 return getSlcProcessesBasePath(session
) + '/'
90 + JcrUtils
.dateAsPath(now
, true) + uuid
;
93 /** Get the base for the user processi. */
94 public static String
getSlcProcessesBasePath(Session session
) {
96 Node userHome
= UserJcrUtils
.getUserHome(session
);
98 throw new SlcException("No user home available for "
99 + session
.getUserID());
100 return userHome
.getPath() + '/' + SlcNames
.SLC_SYSTEM
+ '/'
101 + SlcNames
.SLC_PROCESSES
;
102 } catch (RepositoryException re
) {
103 throw new SlcException(
104 "Unexpected error while getting Slc Results Base Path.", re
);
109 * Create a new execution result path in the user home based on the current
112 public static String
createResultPath(Session session
, String uuid
)
113 throws RepositoryException
{
114 Calendar now
= new GregorianCalendar();
115 return SlcJcrResultUtils
.getSlcResultsBasePath(session
) + '/'
116 + JcrUtils
.dateAsPath(now
, true) + uuid
;
120 * Set the value of the primitive accessor as a JCR property. Does nothing
121 * if the value is null.
123 public static void setPrimitiveAsProperty(Node node
, String propertyName
,
124 PrimitiveAccessor primitiveAccessor
) {
125 String type
= primitiveAccessor
.getType();
126 Object value
= primitiveAccessor
.getValue();
127 setPrimitiveAsProperty(node
, propertyName
, type
, value
);
130 /** Map a primitive value to JCR property value. */
131 public static void setPrimitiveAsProperty(Node node
, String propertyName
,
132 String type
, Object value
) {
135 if (value
instanceof CharSequence
)
136 value
= PrimitiveUtils
.convert(type
,
137 ((CharSequence
) value
).toString());
138 if (value
instanceof char[])
139 value
= new String((char[]) value
);
142 if (type
.equals(PrimitiveAccessor
.TYPE_STRING
))
143 node
.setProperty(propertyName
, value
.toString());
144 else if (type
.equals(PrimitiveAccessor
.TYPE_PASSWORD
))
145 node
.setProperty(propertyName
, value
.toString());
146 else if (type
.equals(PrimitiveAccessor
.TYPE_INTEGER
))
147 node
.setProperty(propertyName
, (long) ((Integer
) value
));
148 else if (type
.equals(PrimitiveAccessor
.TYPE_LONG
))
149 node
.setProperty(propertyName
, ((Long
) value
));
150 else if (type
.equals(PrimitiveAccessor
.TYPE_FLOAT
))
151 node
.setProperty(propertyName
, (double) ((Float
) value
));
152 else if (type
.equals(PrimitiveAccessor
.TYPE_DOUBLE
))
153 node
.setProperty(propertyName
, ((Double
) value
));
154 else if (type
.equals(PrimitiveAccessor
.TYPE_BOOLEAN
))
155 node
.setProperty(propertyName
, ((Boolean
) value
));
157 throw new SlcException("Unsupported type " + type
);
158 } catch (RepositoryException e
) {
159 throw new SlcException("Cannot set primitive of " + type
160 + " as property " + propertyName
+ " on " + node
, e
);
164 /** Aggregates the {@link TestStatus} of this sub-tree. */
165 public static Integer
aggregateTestStatus(Node node
) {
167 Integer status
= TestStatus
.PASSED
;
168 if (node
.isNodeType(SlcTypes
.SLC_CHECK
))
169 if (node
.getProperty(SLC_SUCCESS
).getBoolean())
170 status
= TestStatus
.PASSED
;
171 else if (node
.hasProperty(SLC_ERROR_MESSAGE
))
172 status
= TestStatus
.ERROR
;
174 status
= TestStatus
.FAILED
;
176 NodeIterator it
= node
.getNodes();
177 while (it
.hasNext()) {
178 Integer childStatus
= aggregateTestStatus(it
.nextNode());
179 if (childStatus
> status
)
180 status
= childStatus
;
183 } catch (Exception e
) {
184 throw new SlcException("Could not aggregate test status from "
190 * Aggregates the {@link TestStatus} of this sub-tree.
192 * @return the same {@link StringBuffer}, for convenience (typically calling
195 public static StringBuffer
aggregateTestMessages(Node node
,
196 StringBuffer messages
) {
198 if (node
.isNodeType(SlcTypes
.SLC_CHECK
)) {
199 if (node
.hasProperty(SLC_MESSAGE
)) {
200 if (messages
.length() > 0)
201 messages
.append('\n');
202 messages
.append(node
.getProperty(SLC_MESSAGE
).getString());
204 if (node
.hasProperty(SLC_ERROR_MESSAGE
)) {
205 if (messages
.length() > 0)
206 messages
.append('\n');
207 messages
.append(node
.getProperty(SLC_ERROR_MESSAGE
)
211 NodeIterator it
= node
.getNodes();
212 while (it
.hasNext()) {
213 Node child
= it
.nextNode();
214 aggregateTestMessages(child
, messages
);
217 } catch (Exception e
) {
218 throw new SlcException("Could not aggregate test messages from "
223 /** Prevents instantiation */
224 private SlcJcrUtils() {