]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/controllers/ProcessController.java
Re-enable tooltip support (works only on RCP so far)
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / controllers / ProcessController.java
1 package org.argeo.slc.client.ui.controllers;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.jcr.Node;
7 import javax.jcr.NodeIterator;
8 import javax.jcr.Property;
9 import javax.jcr.RepositoryException;
10
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;
21
22 /**
23 * We use a separate class (not in UI components) so that it can be a singleton
24 * in an application context.
25 */
26 public class ProcessController {
27 private final static Log log = LogFactory.getLog(ProcessController.class);
28 private Map<String, SlcAgentFactory> agentFactories = new HashMap<String, SlcAgentFactory>();
29
30 public ExecutionProcess process(Node processNode) {
31 JcrExecutionProcess process = new JcrExecutionProcess(processNode);
32 try {
33 SlcAgent slcAgent = findAgent(processNode);
34 if (slcAgent == null)
35 throw new SlcException("Cannot find agent for " + processNode);
36 slcAgent.process(process);
37 return 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);
42 }
43 }
44
45 public void kill(Node processNode) {
46 JcrExecutionProcess process = new JcrExecutionProcess(processNode);
47 try {
48 SlcAgent slcAgent = findAgent(processNode);
49 if (slcAgent == null)
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);
56 }
57 }
58
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();
63 if (nit.hasNext()) {
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 "
76 + agentFactoryPath);
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)
81 .getString();
82
83 // process
84 return agentFactory.getAgent(agentUuid);
85 }
86 return null;
87 }
88
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);
95 }
96
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);
103 }
104 }