X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.slc.client.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fclient%2Fui%2Fcontrollers%2FProcessController.java;h=d6719dce0c10975b4922015d4a7ee0a59d4d2500;hb=83996361545b06f97a85d20f3b5824af0a2c4904;hp=ccb54b5a079e008293380742599b93ec2a7be825;hpb=a181e3d059185a9dc108e81f38c66f48f4e4aac8;p=gpl%2Fargeo-slc.git diff --git a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/controllers/ProcessController.java b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/controllers/ProcessController.java index ccb54b5a0..d6719dce0 100644 --- a/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/controllers/ProcessController.java +++ b/eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/controllers/ProcessController.java @@ -11,60 +11,79 @@ import javax.jcr.RepositoryException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; +import org.argeo.slc.execution.ExecutionProcess; import org.argeo.slc.jcr.SlcJcrConstants; import org.argeo.slc.jcr.SlcJcrUtils; import org.argeo.slc.jcr.SlcNames; import org.argeo.slc.jcr.execution.JcrExecutionProcess; -import org.argeo.slc.process.SlcExecution; import org.argeo.slc.runtime.SlcAgent; import org.argeo.slc.runtime.SlcAgentFactory; -import org.argeo.slc.services.SlcExecutionService; +/** + * We use a separate class (not in UI components) so that it can be a singleton + * in an application context. + */ public class ProcessController { private final static Log log = LogFactory.getLog(ProcessController.class); - private SlcExecutionService slcExecutionService; private Map agentFactories = new HashMap(); - public void execute(SlcAgent agent, SlcExecution slcExecution) { - slcExecutionService.newExecution(slcExecution); - agent.process(slcExecution); - if (log.isDebugEnabled()) - log.debug("SlcExcution " + slcExecution.getUuid() - + " launched on Agent " + agent.toString()); + public ExecutionProcess process(Node processNode) { + JcrExecutionProcess process = new JcrExecutionProcess(processNode); + try { + SlcAgent slcAgent = findAgent(processNode); + if (slcAgent == null) + throw new SlcException("Cannot find agent for " + processNode); + slcAgent.process(process); + return process; + } catch (Exception e) { + if (!process.getStatus().equals(ExecutionProcess.ERROR)) + process.setStatus(ExecutionProcess.ERROR); + throw new SlcException("Cannot execute " + processNode, e); + } } - public void process(Node processNode) { + public void kill(Node processNode) { + JcrExecutionProcess process = new JcrExecutionProcess(processNode); try { - // we currently only deal with single agents - Node flowNode = processNode.getNode(SlcNames.SLC_FLOW); - NodeIterator nit = flowNode.getNodes(); - if (nit.hasNext()) { - Node firstFlow = nit.nextNode(); - // we assume there is an nt:address - String firstFlowPath = firstFlow.getNode(SlcNames.SLC_ADDRESS) - .getProperty(Property.JCR_PATH).getString(); - String agentFactoryPath = SlcJcrUtils - .flowAgentFactoryPath(firstFlowPath); - if (!agentFactories.containsKey(agentFactoryPath)) - throw new SlcException("No agent factory registered under " - + agentFactoryPath); - SlcAgentFactory agentFactory = agentFactories - .get(agentFactoryPath); - String agentUuid = ((Node) processNode - .getAncestor(SlcJcrUtils.AGENT_FACTORY_DEPTH + 1)) - .getName(); - - // process - SlcAgent slcAgent = agentFactory.getAgent(agentUuid); - slcAgent.process(new JcrExecutionProcess(processNode)); - } - } catch (RepositoryException e) { + SlcAgent slcAgent = findAgent(processNode); + if (slcAgent == null) + throw new SlcException("Cannot find agent for " + processNode); + slcAgent.kill(process); + } catch (Exception e) { + if (!process.getStatus().equals(ExecutionProcess.ERROR)) + process.setStatus(ExecutionProcess.ERROR); throw new SlcException("Cannot execute " + processNode, e); } } - public void setSlcExecutionService(SlcExecutionService slcExecutionService) { - this.slcExecutionService = slcExecutionService; + protected SlcAgent findAgent(Node processNode) throws RepositoryException { + // we currently only deal with single agents + Node realizedFlowNode = processNode.getNode(SlcNames.SLC_FLOW); + NodeIterator nit = realizedFlowNode.getNodes(); + if (nit.hasNext()) { + // TODO find a better way to determine which agent to use + // currently we check the agent of the first registered flow + Node firstRealizedFlow = nit.nextNode(); + // we assume there is an nt:address + String firstFlowPath = firstRealizedFlow + .getNode(SlcNames.SLC_ADDRESS) + .getProperty(Property.JCR_PATH).getString(); + Node flowNode = processNode.getSession().getNode(firstFlowPath); + String agentFactoryPath = SlcJcrUtils + .flowAgentFactoryPath(firstFlowPath); + if (!agentFactories.containsKey(agentFactoryPath)) + throw new SlcException("No agent factory registered under " + + agentFactoryPath); + SlcAgentFactory agentFactory = agentFactories.get(agentFactoryPath); + Node agentNode = ((Node) flowNode + .getAncestor(SlcJcrUtils.AGENT_FACTORY_DEPTH + 1)); + String agentUuid = agentNode.getProperty(SlcNames.SLC_UUID) + .getString(); + + // process + return agentFactory.getAgent(agentUuid); + } + return null; } public synchronized void register(SlcAgentFactory agentFactory,