1 package org
.argeo
.slc
.client
.ui
.controllers
;
3 import java
.util
.HashMap
;
7 import javax
.jcr
.NodeIterator
;
8 import javax
.jcr
.Property
;
9 import javax
.jcr
.RepositoryException
;
11 import org
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.slc
.SlcException
;
14 import org
.argeo
.slc
.execution
.ExecutionProcess
;
15 import org
.argeo
.slc
.jcr
.SlcJcrConstants
;
16 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
17 import org
.argeo
.slc
.jcr
.SlcNames
;
18 import org
.argeo
.slc
.jcr
.execution
.JcrExecutionProcess
;
19 import org
.argeo
.slc
.runtime
.SlcAgent
;
20 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
23 * We use a separate class (not in UI components) so that it can be a singleton
24 * in an application context.
26 public class ProcessController
{
27 private final static Log log
= LogFactory
.getLog(ProcessController
.class);
28 private Map
<String
, SlcAgentFactory
> agentFactories
= new HashMap
<String
, SlcAgentFactory
>();
30 public ExecutionProcess
process(Node processNode
) {
31 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
33 SlcAgent slcAgent
= findAgent(processNode
);
35 throw new SlcException("Cannot find agent for " + processNode
);
36 slcAgent
.process(process
);
38 } catch (Exception e
) {
39 if (!process
.getStatus().equals(ExecutionProcess
.ERROR
))
40 process
.setStatus(ExecutionProcess
.ERROR
);
41 throw new SlcException("Cannot execute " + processNode
, e
);
45 public void kill(Node processNode
) {
46 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
48 SlcAgent slcAgent
= findAgent(processNode
);
50 throw new SlcException("Cannot find agent for " + processNode
);
51 slcAgent
.kill(process
);
52 } catch (Exception e
) {
53 if (!process
.getStatus().equals(ExecutionProcess
.ERROR
))
54 process
.setStatus(ExecutionProcess
.ERROR
);
55 throw new SlcException("Cannot execute " + processNode
, e
);
59 protected SlcAgent
findAgent(Node processNode
) throws RepositoryException
{
60 // we currently only deal with single agents
61 Node realizedFlowNode
= processNode
.getNode(SlcNames
.SLC_FLOW
);
62 NodeIterator nit
= realizedFlowNode
.getNodes();
64 // TODO find a better way to determine which agent to use
65 // currently we check the agent of the first registered flow
66 Node firstRealizedFlow
= nit
.nextNode();
67 // we assume there is an nt:address
68 String firstFlowPath
= firstRealizedFlow
69 .getNode(SlcNames
.SLC_ADDRESS
)
70 .getProperty(Property
.JCR_PATH
).getString();
71 Node flowNode
= processNode
.getSession().getNode(firstFlowPath
);
72 String agentFactoryPath
= SlcJcrUtils
73 .flowAgentFactoryPath(firstFlowPath
);
74 if (!agentFactories
.containsKey(agentFactoryPath
))
75 throw new SlcException("No agent factory registered under "
77 SlcAgentFactory agentFactory
= agentFactories
.get(agentFactoryPath
);
78 Node agentNode
= ((Node
) flowNode
79 .getAncestor(SlcJcrUtils
.AGENT_FACTORY_DEPTH
+ 1));
80 String agentUuid
= agentNode
.getProperty(SlcNames
.SLC_UUID
)
84 return agentFactory
.getAgent(agentUuid
);
89 public synchronized void register(SlcAgentFactory agentFactory
,
90 Map
<String
, String
> properties
) {
91 String path
= properties
.get(SlcJcrConstants
.PROPERTY_PATH
);
92 if (log
.isDebugEnabled())
93 log
.debug("Agent factory registered under " + path
);
94 agentFactories
.put(path
, agentFactory
);
97 public synchronized void unregister(SlcAgentFactory agentFactory
,
98 Map
<String
, String
> properties
) {
99 String path
= properties
.get(SlcJcrConstants
.PROPERTY_PATH
);
100 if (log
.isDebugEnabled())
101 log
.debug("Agent factory unregistered from " + path
);
102 agentFactories
.remove(path
);