1 /* 2 * Copyright (C) 2007-2012 Argeo GmbH 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 javax.jcr.Node; 19 import javax.jcr.RepositoryException; 20 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; 25 26 /** 27 * We use a separate class (not in UI components) so that it can be a singleton 28 * in an application context. 29 */ 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>(); 35 36 private SlcAgent agent; 37 38 public ExecutionProcess process(Node processNode) { 39 JcrExecutionProcess process = new JcrExecutionProcess(processNode); 40 try { 41 SlcAgent slcAgent = findAgent(processNode); 42 if (slcAgent == null) 43 throw new SlcException("Cannot find agent for " + processNode); 44 slcAgent.process(process); 45 return 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); 50 } 51 } 52 53 public void kill(Node processNode) { 54 JcrExecutionProcess process = new JcrExecutionProcess(processNode); 55 try { 56 SlcAgent slcAgent = findAgent(processNode); 57 if (slcAgent == null) 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); 64 } 65 } 66 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) 90 // .getString(); 91 // 92 // // process 93 // return agentFactory.getAgent(agentUuid); 94 // } 95 96 return agent; 97 } 98 99 public void setAgent(SlcAgent agent) { 100 this.agent = agent; 101 } 102 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); 109 // } 110 // 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); 117 // } 118 }