]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java
JCR UI can run processes
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / runtime / DefaultAgent.java
index ef2c52382f073f6ba6b5358b866ccfb53f1437ef..3220e910f2557957733372fa4ef766e6da51a44a 100644 (file)
 
 package org.argeo.slc.core.runtime;
 
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.List;
+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.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 {
-       // private final static Log log = LogFactory.getLog(AbstractAgent.class);
-
+       private SlcAgentDescriptor agentDescriptor;
        private ExecutionModulesManager modulesManager;
 
-       public void runSlcExecution(final SlcExecution slcExecution) {
-               modulesManager.process(slcExecution);
+       /*
+        * LIFECYCLE
+        */
+       public void init() {
+               try {
+                       agentDescriptor = new SlcAgentDescriptor();
+                       agentDescriptor.setUuid(initAgentUuid());
+                       agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
+               } catch (UnknownHostException e) {
+                       throw new SlcException("Unable to create agent descriptor.", e);
+               }
+       }
+
+       public void dispose() {
+
+       }
+
+       /**
+        * 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(modulesManager,
+                               process);
+               processThread.start();
+       }
+
+       /** Creates the thread which will coordinate the execution for this agent. */
+       protected ProcessThread createProcessThread(
+                       ExecutionModulesManager modulesManager, ExecutionProcess process) {
+               if (!(process instanceof SlcExecution))
+                       throw new SlcException("Unsupported process type "
+                                       + process.getClass());
+               ProcessThread processThread = new ProcessThread(modulesManager,
+                               (SlcExecution) process);
+               return processThread;
        }
 
        public ExecutionModuleDescriptor getExecutionModuleDescriptor(
                        String moduleName, String version) {
-               return modulesManager.getExecutionModuleDescriptor(moduleName,
-                               version);
+               return modulesManager.getExecutionModuleDescriptor(moduleName, version);
        }
 
        public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
@@ -46,12 +97,23 @@ public class DefaultAgent implements SlcAgent {
                return true;
        }
 
+       /*
+        * BEAN
+        */
        public void setModulesManager(ExecutionModulesManager modulesManager) {
                this.modulesManager = modulesManager;
        }
 
-       public ExecutionModulesManager getModulesManager() {
-               return modulesManager;
+       protected SlcAgentDescriptor getAgentDescriptor() {
+               return agentDescriptor;
        }
 
+       public String getAgentUuid() {
+               return agentDescriptor.getUuid();
+       }
+
+       @Override
+       public String toString() {
+               return agentDescriptor.toString();
+       }
 }