]> 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
Improve logging
[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.runtime.SlcAgent;
19 import org.argeo.slc.runtime.SlcAgentFactory;
20
21 /**
22 * We use a separate class (not in UI components) so that it can be a singleton
23 * in an application context.
24 */
25 public class ProcessController {
26 private final static Log log = LogFactory.getLog(ProcessController.class);
27 private Map<String, SlcAgentFactory> agentFactories = new HashMap<String, SlcAgentFactory>();
28
29 public ExecutionProcess process(Node processNode) {
30 JcrExecutionProcess process = new JcrExecutionProcess(processNode);
31 try {
32 // we currently only deal with single agents
33 Node realizedFlowNode = processNode.getNode(SlcNames.SLC_FLOW);
34 NodeIterator nit = realizedFlowNode.getNodes();
35 if (nit.hasNext()) {
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 "
48 + agentFactoryPath);
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)
54 .getString();
55
56 // process
57 SlcAgent slcAgent = agentFactory.getAgent(agentUuid);
58 slcAgent.process(process);
59 }
60 return process;
61 } catch (Exception e) {
62 if (!process.getStatus().equals(ExecutionProcess.ERROR))
63 process.setStatus(ExecutionProcess.ERROR);
64 throw new SlcException("Cannot execute " + processNode, e);
65 }
66 }
67
68 public synchronized void register(SlcAgentFactory agentFactory,
69 Map<String, String> properties) {
70 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
71 if (log.isDebugEnabled())
72 log.debug("Agent factory registered under " + path);
73 agentFactories.put(path, agentFactory);
74 }
75
76 public synchronized void unregister(SlcAgentFactory agentFactory,
77 Map<String, String> properties) {
78 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
79 if (log.isDebugEnabled())
80 log.debug("Agent factory unregistered from " + path);
81 agentFactories.remove(path);
82 }
83 }