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=feb59eef0ac92fe73a6b85b5f7a03da2045667b3;hb=74904a755b5b344238eafa798419b80c5e74f7ed;hp=ca230856f4f2103f3288dfacf49cd52edb52d382;hpb=548235484edd6b48de5ddc3bbca318e0e49cb57b;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 ca230856f..feb59eef0 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 @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Mathieu Baudier + * Copyright (C) 2007-2012 Mathieu Baudier * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,29 +13,41 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - 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; /** Implements the base methods of an SLC agent. */ -public class DefaultAgent implements SlcAgent { +@SuppressWarnings("deprecation") +public class DefaultAgent implements SlcAgent, ExecutionProcessNotifier { private SlcAgentDescriptor agentDescriptor; private ExecutionModulesManager modulesManager; + private ThreadGroup processesThreadGroup; + private Map runningProcesses = Collections + .synchronizedMap(new HashMap()); + /* * LIFECYCLE */ + /** Initialization */ public void init() { try { agentDescriptor = new SlcAgentDescriptor(); @@ -44,10 +56,16 @@ public class DefaultAgent implements SlcAgent { } 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() { - + /** Clean up (needs to be called by overriding method) */ + public void destroy() { + modulesManager.unregisterProcessNotifier(this, + new HashMap()); } /** @@ -61,8 +79,31 @@ public class DefaultAgent implements SlcAgent { /* * SLC AGENT */ - public void runSlcExecution(final SlcExecution slcExecution) { - modulesManager.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(); + } + } + + /** 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( @@ -78,6 +119,21 @@ public class DefaultAgent implements SlcAgent { return true; } + /* + * 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) { + } + /* * BEAN */