]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/SlcJcrUtils.java
Improve authenticated application context initialization
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / SlcJcrUtils.java
1 package org.argeo.slc.jcr;
2
3 import java.util.Calendar;
4 import java.util.GregorianCalendar;
5
6 import javax.jcr.Node;
7 import javax.jcr.RepositoryException;
8
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;
14
15 /**
16 * Utilities around the SLC JCR model. Note that it relies on fixed base paths
17 * (convention over configuration) for optimization purposes.
18 */
19 public class SlcJcrUtils {
20 public final static Integer AGENT_FACTORY_DEPTH = 3;
21
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]);
28 }
29 return buf.toString();
30 }
31
32 /** Module node name based on module name and version */
33 public static String getModuleNodeName(ModuleDescriptor moduleDescriptor) {
34 return moduleDescriptor.getName() + "_" + moduleDescriptor.getVersion();
35 }
36
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('_'));
42 }
43
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);
49 }
50
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]);
58 }
59 return buf.toString();
60 }
61
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;
67
68 }
69
70 /**
71 * Set the value of the primitive accessor as a JCR property. Does nothing
72 * if the value is null.
73 */
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);
79 }
80
81 /** Map a primitive value to JCR ptoperty value. */
82 public static void setPrimitiveAsProperty(Node node, String propertyName,
83 String type, Object value) {
84 if (value == null)
85 return;
86 if (value instanceof CharSequence)
87 value = PrimitiveUtils.convert(type,
88 ((CharSequence) value).toString());
89
90 try {
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));
103 else
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);
108 }
109 }
110
111 /** Prevents instantiation */
112 private SlcJcrUtils() {
113
114 }
115
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));
124 }
125
126 }