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 java
.util
.HashMap
;
21 import javax
.jcr
.Node
;
22 import javax
.jcr
.NodeIterator
;
23 import javax
.jcr
.Property
;
24 import javax
.jcr
.RepositoryException
;
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
;
38 * We use a separate class (not in UI components) so that it can be a singleton
39 * in an application context.
41 public class ProcessController
{
42 private final static Log log
= LogFactory
.getLog(ProcessController
.class);
43 private Map
<String
, SlcAgentFactory
> agentFactories
= new HashMap
<String
, SlcAgentFactory
>();
45 public ExecutionProcess
process(Node processNode
) {
46 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
48 SlcAgent slcAgent
= findAgent(processNode
);
50 throw new SlcException("Cannot find agent for " + processNode
);
51 slcAgent
.process(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
);
60 public void kill(Node processNode
) {
61 JcrExecutionProcess process
= new JcrExecutionProcess(processNode
);
63 SlcAgent slcAgent
= findAgent(processNode
);
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
);
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();
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 "
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
)
99 return agentFactory
.getAgent(agentUuid
);
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
);
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
);