]> 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
Make module operations interruptible
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / controllers / ProcessController.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.controllers;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import javax.jcr.Node;
22 import javax.jcr.NodeIterator;
23 import javax.jcr.Property;
24 import javax.jcr.RepositoryException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.SlcException;
29 import org.argeo.slc.execution.ExecutionProcess;
30 import org.argeo.slc.jcr.SlcJcrConstants;
31 import org.argeo.slc.jcr.SlcJcrUtils;
32 import org.argeo.slc.jcr.SlcNames;
33 import org.argeo.slc.jcr.execution.JcrExecutionProcess;
34 import org.argeo.slc.runtime.SlcAgent;
35 import org.argeo.slc.runtime.SlcAgentFactory;
36
37 /**
38 * We use a separate class (not in UI components) so that it can be a singleton
39 * in an application context.
40 */
41 public class ProcessController {
42 private final static Log log = LogFactory.getLog(ProcessController.class);
43 private Map<String, SlcAgentFactory> agentFactories = new HashMap<String, SlcAgentFactory>();
44
45 public ExecutionProcess process(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.process(process);
52 return process;
53 } catch (Exception e) {
54 if (!process.getStatus().equals(ExecutionProcess.ERROR))
55 process.setStatus(ExecutionProcess.ERROR);
56 throw new SlcException("Cannot execute " + processNode, e);
57 }
58 }
59
60 public void kill(Node processNode) {
61 JcrExecutionProcess process = new JcrExecutionProcess(processNode);
62 try {
63 SlcAgent slcAgent = findAgent(processNode);
64 if (slcAgent == null)
65 throw new SlcException("Cannot find agent for " + processNode);
66 slcAgent.kill(process);
67 } catch (Exception e) {
68 if (!process.getStatus().equals(ExecutionProcess.ERROR))
69 process.setStatus(ExecutionProcess.ERROR);
70 throw new SlcException("Cannot execute " + processNode, e);
71 }
72 }
73
74 protected SlcAgent findAgent(Node processNode) throws RepositoryException {
75 // we currently only deal with single agents
76 Node realizedFlowNode = processNode.getNode(SlcNames.SLC_FLOW);
77 NodeIterator nit = realizedFlowNode.getNodes();
78 if (nit.hasNext()) {
79 // TODO find a better way to determine which agent to use
80 // currently we check the agent of the first registered flow
81 Node firstRealizedFlow = nit.nextNode();
82 // we assume there is an nt:address
83 String firstFlowPath = firstRealizedFlow
84 .getNode(SlcNames.SLC_ADDRESS)
85 .getProperty(Property.JCR_PATH).getString();
86 Node flowNode = processNode.getSession().getNode(firstFlowPath);
87 String agentFactoryPath = SlcJcrUtils
88 .flowAgentFactoryPath(firstFlowPath);
89 if (!agentFactories.containsKey(agentFactoryPath))
90 throw new SlcException("No agent factory registered under "
91 + agentFactoryPath);
92 SlcAgentFactory agentFactory = agentFactories.get(agentFactoryPath);
93 Node agentNode = ((Node) flowNode
94 .getAncestor(SlcJcrUtils.AGENT_FACTORY_DEPTH + 1));
95 String agentUuid = agentNode.getProperty(SlcNames.SLC_UUID)
96 .getString();
97
98 // process
99 return agentFactory.getAgent(agentUuid);
100 }
101 return null;
102 }
103
104 public synchronized void register(SlcAgentFactory agentFactory,
105 Map<String, String> properties) {
106 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
107 if (log.isDebugEnabled())
108 log.debug("Agent factory registered under " + path);
109 agentFactories.put(path, agentFactory);
110 }
111
112 public synchronized void unregister(SlcAgentFactory agentFactory,
113 Map<String, String> properties) {
114 String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
115 if (log.isDebugEnabled())
116 log.debug("Agent factory unregistered from " + path);
117 agentFactories.remove(path);
118 }
119 }