2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.controllers
;
18 import javax
.jcr
.Node
;
19 import javax
.jcr
.RepositoryException
;
21 import org
.argeo
.slc
.SlcException
;
22 import org
.argeo
.slc
.execution
.ExecutionProcess
;
23 import org
.argeo
.slc
.execution
.SlcAgent
;
24 import org
.argeo
.slc
.jcr
.execution
.JcrExecutionProcess
;
27 * We use a separate class (not in UI components) so that it can be a singleton
28 * in an application context.
30 public class ProcessController
{
31 // private final static Log log =
32 // LogFactory.getLog(ProcessController.class);
33 // private Map<String, SlcAgentFactory> agentFactories = new HashMap<String,
34 // SlcAgentFactory>();
36 private SlcAgent agent
;
38 public ExecutionProcess
process(Node processNode
) {
39 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
41 SlcAgent slcAgent
= findAgent(processNode
);
43 throw new SlcException("Cannot find agent for " + processNode
);
44 slcAgent
.process(process
);
46 } catch (Exception e
) {
47 if (!process
.getStatus().equals(ExecutionProcess
.ERROR
))
48 process
.setStatus(ExecutionProcess
.ERROR
);
49 throw new SlcException("Cannot execute " + processNode
, e
);
53 public void kill(Node processNode
) {
54 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
56 SlcAgent slcAgent
= findAgent(processNode
);
58 throw new SlcException("Cannot find agent for " + processNode
);
59 slcAgent
.kill(process
.getUuid());
60 } catch (Exception e
) {
61 if (!process
.getStatus().equals(ExecutionProcess
.ERROR
))
62 process
.setStatus(ExecutionProcess
.ERROR
);
63 throw new SlcException("Cannot execute " + processNode
, e
);
67 /** Always return the default runtime agent */
68 protected SlcAgent
findAgent(Node processNode
) throws RepositoryException
{
69 // we currently only deal with single agents
70 // Node realizedFlowNode = processNode.getNode(SlcNames.SLC_FLOW);
71 // NodeIterator nit = realizedFlowNode.getNodes();
72 // if (nit.hasNext()) {
73 // // TODO find a better way to determine which agent to use
74 // // currently we check the agent of the first registered flow
75 // Node firstRealizedFlow = nit.nextNode();
76 // // we assume there is an nt:address
77 // String firstFlowPath = firstRealizedFlow
78 // .getNode(SlcNames.SLC_ADDRESS)
79 // .getProperty(Property.JCR_PATH).getString();
80 // Node flowNode = processNode.getSession().getNode(firstFlowPath);
81 // String agentFactoryPath = SlcJcrUtils
82 // .flowAgentFactoryPath(firstFlowPath);
83 // if (!agentFactories.containsKey(agentFactoryPath))
84 // throw new SlcException("No agent factory registered under "
85 // + agentFactoryPath);
86 // SlcAgentFactory agentFactory = agentFactories.get(agentFactoryPath);
87 // Node agentNode = ((Node) flowNode
88 // .getAncestor(SlcJcrUtils.AGENT_FACTORY_DEPTH + 1));
89 // String agentUuid = agentNode.getProperty(SlcNames.SLC_UUID)
93 // return agentFactory.getAgent(agentUuid);
99 public void setAgent(SlcAgent agent
) {
103 // public synchronized void register(SlcAgentFactory agentFactory,
104 // Map<String, String> properties) {
105 // String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
106 // if (log.isDebugEnabled())
107 // log.debug("Agent factory registered under " + path);
108 // agentFactories.put(path, agentFactory);
111 // public synchronized void unregister(SlcAgentFactory agentFactory,
112 // Map<String, String> properties) {
113 // String path = properties.get(SlcJcrConstants.PROPERTY_PATH);
114 // if (log.isDebugEnabled())
115 // log.debug("Agent factory unregistered from " + path);
116 // agentFactories.remove(path);