]> 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
Remove SLC GIS
[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
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.process.SlcExecution;
19 import org.argeo.slc.runtime.SlcAgent;
20 import org.argeo.slc.runtime.SlcAgentFactory;
21 import org.argeo.slc.services.SlcExecutionService;
22
23 /**
24 * We use a separate class (not in UI components) so that it can be a singleton
25 * in an application context.
26 */
27 public class ProcessController {
28 private final static Log log = LogFactory.getLog(ProcessController.class);
29 private SlcExecutionService slcExecutionService;
30 private Map<String, SlcAgentFactory> agentFactories = new HashMap<String, SlcAgentFactory>();
31
32 public void execute(SlcAgent agent, SlcExecution slcExecution) {
33 slcExecutionService.newExecution(slcExecution);
34 agent.process(slcExecution);
35 if (log.isDebugEnabled())
36 log.debug("SlcExecution " + slcExecution.getUuid()
37 + " launched on Agent " + agent.toString());
38 }
39
40 public void process(Node processNode) {
41 JcrExecutionProcess process = new JcrExecutionProcess(processNode);
42 try {
43 // we currently only deal with single agents
44 Node realizedFlowNode = processNode.getNode(SlcNames.SLC_FLOW);
45 NodeIterator nit = realizedFlowNode.getNodes();
46 if (nit.hasNext()) {
47 // TODO find a better way to determine which agent to use
48 // currently we check the agent of the first registered flow
49 Node firstRealizedFlow = nit.nextNode();
50 // we assume there is an nt:address
51 String firstFlowPath = firstRealizedFlow
52 .getNode(SlcNames.SLC_ADDRESS)
53 .getProperty(Property.JCR_PATH).getString();
54 Node flowNode = processNode.getSession().getNode(firstFlowPath);
55 String agentFactoryPath = SlcJcrUtils
56 .flowAgentFactoryPath(firstFlowPath);
57 if (!agentFactories.containsKey(agentFactoryPath))
58 throw new SlcException("No agent factory registered under "
59 + agentFactoryPath);
60 SlcAgentFactory agentFactory = agentFactories
61 .get(agentFactoryPath);
62 Node agentNode = ((Node) flowNode
63 .getAncestor(SlcJcrUtils.AGENT_FACTORY_DEPTH + 1));
64 String agentUuid = agentNode.getProperty(SlcNames.SLC_UUID)
65 .getString();
66
67 // process
68 SlcAgent slcAgent = agentFactory.getAgent(agentUuid);
69 slcAgent.process(process);
70 }
71 } catch (Exception e) {
72 if (!process.getStatus().equals(ExecutionProcess.ERROR))
73 process.setStatus(ExecutionProcess.ERROR);
74 throw new SlcException("Cannot execute " + processNode, e);
75 }
76 }
77
78 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
79 this.slcExecutionService = slcExecutionService;
80 }
81
82 public synchronized void register(SlcAgentFactory agentFactory,
83 Map<String, String> properties) {
84 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
85 if (log.isDebugEnabled())
86 log.debug("Agent factory registered under " + path);
87 agentFactories.put(path, agentFactory);
88 }
89
90 public synchronized void unregister(SlcAgentFactory agentFactory,
91 Map<String, String> properties) {
92 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
93 if (log.isDebugEnabled())
94 log.debug("Agent factory unregistered from " + path);
95 agentFactories.remove(path);
96 }
97 }