1 package org
.argeo
.slc
.client
.ui
.controllers
;
3 import java
.util
.HashMap
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
10 import org
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
12 import org
.argeo
.slc
.SlcException
;
13 import org
.argeo
.slc
.execution
.ExecutionProcess
;
14 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
15 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
16 import org
.argeo
.slc
.jcr
.SlcNames
;
17 import org
.argeo
.slc
.jcr
.execution
.JcrExecutionProcess
;
18 import org
.argeo
.slc
.runtime
.SlcAgent
;
19 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
22 * We use a separate class (not in UI components) so that it can be a singleton
23 * in an application context.
25 public class ProcessController
{
26 private final static Log log
= LogFactory
.getLog(ProcessController
.class);
27 private Map
<String
, SlcAgentFactory
> agentFactories
= new HashMap
<String
, SlcAgentFactory
>();
29 public void process(Node processNode
) {
30 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
32 // we currently only deal with single agents
33 Node realizedFlowNode
= processNode
.getNode(SlcNames
.SLC_FLOW
);
34 NodeIterator nit
= realizedFlowNode
.getNodes();
36 // TODO find a better way to determine which agent to use
37 // currently we check the agent of the first registered flow
38 Node firstRealizedFlow
= nit
.nextNode();
39 // we assume there is an nt:address
40 String firstFlowPath
= firstRealizedFlow
41 .getNode(SlcNames
.SLC_ADDRESS
)
42 .getProperty(Property
.JCR_PATH
).getString();
43 Node flowNode
= processNode
.getSession().getNode(firstFlowPath
);
44 String agentFactoryPath
= SlcJcrUtils
45 .flowAgentFactoryPath(firstFlowPath
);
46 if (!agentFactories
.containsKey(agentFactoryPath
))
47 throw new SlcException("No agent factory registered under "
49 SlcAgentFactory agentFactory
= agentFactories
50 .get(agentFactoryPath
);
51 Node agentNode
= ((Node
) flowNode
52 .getAncestor(SlcJcrUtils
.AGENT_FACTORY_DEPTH
+ 1));
53 String agentUuid
= agentNode
.getProperty(SlcNames
.SLC_UUID
)
57 SlcAgent slcAgent
= agentFactory
.getAgent(agentUuid
);
58 slcAgent
.process(process
);
60 } catch (Exception e
) {
61 if (!process
.getStatus().equals(ExecutionProcess
.ERROR
))
62 process
.setStatus(ExecutionProcess
.ERROR
);
63 throw new SlcException("Cannot execute " + processNode
, e
);
67 public synchronized void register(SlcAgentFactory agentFactory
,
68 Map
<String
, String
> properties
) {
69 String path
= properties
.get(SlcJcrConstants
.PROPERTY_PATH
);
70 if (log
.isDebugEnabled())
71 log
.debug("Agent factory registered under " + path
);
72 agentFactories
.put(path
, agentFactory
);
75 public synchronized void unregister(SlcAgentFactory agentFactory
,
76 Map
<String
, String
> properties
) {
77 String path
= properties
.get(SlcJcrConstants
.PROPERTY_PATH
);
78 if (log
.isDebugEnabled())
79 log
.debug("Agent factory unregistered from " + path
);
80 agentFactories
.remove(path
);