X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fruntime%2FDefaultAgent.java;h=e4676c77419a28eb46a79a8b3913ba42887bbce6;hb=24d560ee846fda5d7954d44f83cb22ab449dbe61;hp=430b6bd973e6c12df8228b5993c3e7bbc1620956;hpb=d601802d5a50e4e30eb639a08eff5270b63fe599;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java index 430b6bd97..e4676c774 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java @@ -18,34 +18,94 @@ package org.argeo.slc.core.runtime; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.UUID; import org.argeo.slc.SlcException; +import org.argeo.slc.core.execution.ProcessThread; import org.argeo.slc.execution.ExecutionModuleDescriptor; import org.argeo.slc.execution.ExecutionModulesManager; +import org.argeo.slc.execution.ExecutionProcess; +import org.argeo.slc.execution.ExecutionProcessNotifier; +import org.argeo.slc.execution.ExecutionStep; import org.argeo.slc.process.SlcExecution; import org.argeo.slc.runtime.SlcAgent; import org.argeo.slc.runtime.SlcAgentDescriptor; -public class DefaultAgent implements SlcAgent { - // private final static Log log = LogFactory.getLog(AbstractAgent.class); - - private final SlcAgentDescriptor agentDescriptor; +/** Implements the base methods of an SLC agent. */ +public class DefaultAgent implements SlcAgent, ExecutionProcessNotifier { + private SlcAgentDescriptor agentDescriptor; private ExecutionModulesManager modulesManager; - public DefaultAgent() { + private ThreadGroup processesThreadGroup; + private Map runningProcesses = Collections + .synchronizedMap(new HashMap()); + + /* + * LIFECYCLE + */ + public void init() { try { agentDescriptor = new SlcAgentDescriptor(); - agentDescriptor.setUuid(UUID.randomUUID().toString()); + agentDescriptor.setUuid(initAgentUuid()); agentDescriptor.setHost(InetAddress.getLocalHost().getHostName()); } catch (UnknownHostException e) { throw new SlcException("Unable to create agent descriptor.", e); } + processesThreadGroup = new ThreadGroup("SLC Processes of Agent #" + + agentDescriptor.getUuid()); + modulesManager.registerProcessNotifier(this, + new HashMap()); + } + + public void dispose() { + modulesManager.unregisterProcessNotifier(this, + new HashMap()); + } + + /** + * Called during initialization in order to determines the agent UUID. To be + * overridden. By default creates a new one per instance. + */ + protected String initAgentUuid() { + return UUID.randomUUID().toString(); + } + + /* + * SLC AGENT + */ + public void runSlcExecution(SlcExecution slcExecution) { + process(slcExecution); + } + + public void process(ExecutionProcess process) { + ProcessThread processThread = createProcessThread(processesThreadGroup, + modulesManager, process); + processThread.start(); + runningProcesses.put(process.getUuid(), processThread); + // FIXME find a way to remove them from this register + } + + public void kill(ExecutionProcess process) { + String processUuid = process.getUuid(); + if (runningProcesses.containsKey(processUuid)) { + runningProcesses.get(processUuid).interrupt(); + } } - public void runSlcExecution(final SlcExecution slcExecution) { - modulesManager.process(slcExecution); + /** Creates the thread which will coordinate the execution for this agent. */ + protected ProcessThread createProcessThread( + ThreadGroup processesThreadGroup, + ExecutionModulesManager modulesManager, ExecutionProcess process) { + if (!(process instanceof SlcExecution)) + throw new SlcException("Unsupported process type " + + process.getClass()); + ProcessThread processThread = new ProcessThread(processesThreadGroup, + modulesManager, (SlcExecution) process); + return processThread; } public ExecutionModuleDescriptor getExecutionModuleDescriptor( @@ -61,12 +121,26 @@ public class DefaultAgent implements SlcAgent { return true; } - public void setModulesManager(ExecutionModulesManager modulesManager) { - this.modulesManager = modulesManager; + /* + * PROCESS NOTIFIER + */ + public void updateStatus(ExecutionProcess process, String oldStatus, + String newStatus) { + if (newStatus.equals(ExecutionProcess.COMPLETED) + || newStatus.equals(ExecutionProcess.ERROR) + || newStatus.equals(ExecutionProcess.KILLED)) { + runningProcesses.remove(process.getUuid()); + } + } + + public void addSteps(ExecutionProcess process, List steps) { } - public ExecutionModulesManager getModulesManager() { - return modulesManager; + /* + * BEAN + */ + public void setModulesManager(ExecutionModulesManager modulesManager) { + this.modulesManager = modulesManager; } protected SlcAgentDescriptor getAgentDescriptor() { @@ -74,7 +148,7 @@ public class DefaultAgent implements SlcAgent { } public String getAgentUuid() { - return getAgentDescriptor().getUuid(); + return agentDescriptor.getUuid(); } @Override