1 package org
.argeo
.slc
.jcr
;
3 import java
.util
.Calendar
;
4 import java
.util
.GregorianCalendar
;
7 import javax
.jcr
.RepositoryException
;
9 import org
.argeo
.jcr
.JcrUtils
;
10 import org
.argeo
.slc
.SlcException
;
11 import org
.argeo
.slc
.core
.execution
.PrimitiveAccessor
;
12 import org
.argeo
.slc
.core
.execution
.PrimitiveUtils
;
13 import org
.argeo
.slc
.deploy
.ModuleDescriptor
;
16 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
17 * (convention over configuration) for optimization purposes.
19 public class SlcJcrUtils
{
20 public final static Integer AGENT_FACTORY_DEPTH
= 3;
22 /** Extracts the path of a flow relative to its execution module */
23 public static String
flowRelativePath(String fullFlowPath
) {
24 String
[] tokens
= fullFlowPath
.split("/");
25 StringBuffer buf
= new StringBuffer(fullFlowPath
.length());
26 for (int i
= AGENT_FACTORY_DEPTH
+ 3; i
< tokens
.length
; i
++) {
27 buf
.append('/').append(tokens
[i
]);
29 return buf
.toString();
32 /** Module node name based on module name and version */
33 public static String
getModuleNodeName(ModuleDescriptor moduleDescriptor
) {
34 return moduleDescriptor
.getName() + "_" + moduleDescriptor
.getVersion();
37 /** Extracts the execution module name of a flow */
38 public static String
flowExecutionModuleName(String fullFlowPath
) {
39 String
[] tokens
= fullFlowPath
.split("/");
40 String moduleNodeName
= tokens
[AGENT_FACTORY_DEPTH
+ 2];
41 return moduleNodeName
.substring(0, moduleNodeName
.lastIndexOf('_'));
44 /** Extracts the execution module version of a flow */
45 public static String
flowExecutionModuleVersion(String fullFlowPath
) {
46 String
[] tokens
= fullFlowPath
.split("/");
47 String moduleNodeName
= tokens
[AGENT_FACTORY_DEPTH
+ 2];
48 return moduleNodeName
.substring(moduleNodeName
.lastIndexOf('_') + 1);
51 /** Extracts the agent factory of a flow */
52 public static String
flowAgentFactoryPath(String fullFlowPath
) {
53 String
[] tokens
= fullFlowPath
.split("/");
54 StringBuffer buf
= new StringBuffer(fullFlowPath
.length());
55 // first token is always empty
56 for (int i
= 1; i
< AGENT_FACTORY_DEPTH
+ 1; i
++) {
57 buf
.append('/').append(tokens
[i
]);
59 return buf
.toString();
62 /** Create a new execution process path based on the current time */
63 public static String
createExecutionProcessPath(String uuid
) {
64 Calendar now
= new GregorianCalendar();
65 return SlcJcrConstants
.PROCESSES_BASE_PATH
+ '/'
66 + JcrUtils
.dateAsPath(now
, true) + uuid
;
71 * Set the value of the primitive accessor as a JCR property. Does nothing
72 * if the value is null.
74 public static void setPrimitiveAsProperty(Node node
, String propertyName
,
75 PrimitiveAccessor primitiveAccessor
) {
76 String type
= primitiveAccessor
.getType();
77 Object value
= primitiveAccessor
.getValue();
78 setPrimitiveAsProperty(node
, propertyName
, type
, value
);
81 /** Map a primitive value to JCR ptoperty value. */
82 public static void setPrimitiveAsProperty(Node node
, String propertyName
,
83 String type
, Object value
) {
86 if (value
instanceof CharSequence
)
87 value
= PrimitiveUtils
.convert(type
,
88 ((CharSequence
) value
).toString());
91 if (type
.equals(PrimitiveAccessor
.TYPE_STRING
))
92 node
.setProperty(propertyName
, value
.toString());
93 else if (type
.equals(PrimitiveAccessor
.TYPE_INTEGER
))
94 node
.setProperty(propertyName
, (long) ((Integer
) value
));
95 else if (type
.equals(PrimitiveAccessor
.TYPE_LONG
))
96 node
.setProperty(propertyName
, ((Long
) value
));
97 else if (type
.equals(PrimitiveAccessor
.TYPE_FLOAT
))
98 node
.setProperty(propertyName
, (double) ((Float
) value
));
99 else if (type
.equals(PrimitiveAccessor
.TYPE_DOUBLE
))
100 node
.setProperty(propertyName
, ((Double
) value
));
101 else if (type
.equals(PrimitiveAccessor
.TYPE_BOOLEAN
))
102 node
.setProperty(propertyName
, ((Boolean
) value
));
104 throw new SlcException("Unsupported type " + type
);
105 } catch (RepositoryException e
) {
106 throw new SlcException("Cannot set primitive of " + type
107 + " as property " + propertyName
+ " on " + node
, e
);
111 /** Prevents instantiation */
112 private SlcJcrUtils() {
116 public static void main(String
[] args
) {
117 String path
= "/slc/agents/vm/default/org.argeo_1.2.3/myPath/myFlow";
118 System
.out
.println("Flow relative path: " + flowRelativePath(path
));
119 System
.out
.println("Execution Module Name: "
120 + flowExecutionModuleName(path
));
121 System
.out
.println("Execution Module Version: "
122 + flowExecutionModuleVersion(path
));
123 System
.out
.println("Agent Factory path: " + flowAgentFactoryPath(path
));