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