2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.core
.execution
;
18 import java
.net
.InetAddress
;
19 import java
.net
.UnknownHostException
;
20 import java
.util
.Collections
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
24 import java
.util
.UUID
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
29 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
30 import org
.argeo
.slc
.execution
.ExecutionProcess
;
31 import org
.argeo
.slc
.execution
.ExecutionProcessNotifier
;
32 import org
.argeo
.slc
.execution
.ExecutionStep
;
33 import org
.argeo
.slc
.execution
.SlcAgent
;
34 import org
.argeo
.slc
.execution
.SlcAgentDescriptor
;
36 /** Implements the base methods of an SLC agent. */
37 public class DefaultAgent
implements SlcAgent
, ExecutionProcessNotifier
{
38 private final static Log log
= LogFactory
.getLog(DefaultAgent
.class);
40 private SlcAgentDescriptor agentDescriptor
;
41 private ExecutionModulesManager modulesManager
;
43 private ThreadGroup processesThreadGroup
;
44 private Map
<String
, ProcessThread
> runningProcesses
= Collections
45 .synchronizedMap(new HashMap
<String
, ProcessThread
>());
52 agentDescriptor
= new SlcAgentDescriptor();
53 agentDescriptor
.setUuid(initAgentUuid());
55 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
56 } catch (UnknownHostException e
) {
57 log
.error("Cannot resolve localhost host name: " + e
.getMessage());
58 agentDescriptor
.setHost("localhost");
60 processesThreadGroup
= new ThreadGroup("SLC Processes of Agent #"
61 + agentDescriptor
.getUuid());
62 modulesManager
.registerProcessNotifier(this,
63 new HashMap
<String
, String
>());
65 // final String module = System
66 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY);
67 // final String flow = System
68 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY);
69 // if (module != null) {
70 // // launch a flow and stops
71 // new Thread("Unique Flow") {
73 // public void run() {
74 // executeFlowAndExit(module, null, flow);
80 /** Clean up (needs to be called by overriding method) */
81 public void destroy() {
82 modulesManager
.unregisterProcessNotifier(this,
83 new HashMap
<String
, String
>());
87 * Called during initialization in order to determines the agent UUID. To be
88 * overridden. By default creates a new one per instance.
90 protected String
initAgentUuid() {
91 return UUID
.randomUUID().toString();
97 // protected void executeFlowAndExit(final String module,
98 // final String version, final String flow) {
104 public void process(ExecutionProcess process
) {
105 ProcessThread processThread
= createProcessThread(processesThreadGroup
,
106 modulesManager
, process
);
107 processThread
.start();
108 runningProcesses
.put(process
.getUuid(), processThread
);
109 // FIXME find a way to remove them from this register
112 public void kill(ExecutionProcess process
) {
113 String processUuid
= process
.getUuid();
114 if (runningProcesses
.containsKey(processUuid
)) {
115 runningProcesses
.get(processUuid
).interrupt();
119 /** Creates the thread which will coordinate the execution for this agent. */
120 protected ProcessThread
createProcessThread(
121 ThreadGroup processesThreadGroup
,
122 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
123 ProcessThread processThread
= new ProcessThread(processesThreadGroup
,
124 modulesManager
, process
);
125 return processThread
;
128 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
129 String moduleName
, String version
) {
130 return modulesManager
.getExecutionModuleDescriptor(moduleName
, version
);
133 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
134 return modulesManager
.listExecutionModules();
137 public boolean ping() {
144 public void updateStatus(ExecutionProcess process
, String oldStatus
,
146 if (newStatus
.equals(ExecutionProcess
.COMPLETED
)
147 || newStatus
.equals(ExecutionProcess
.ERROR
)
148 || newStatus
.equals(ExecutionProcess
.KILLED
)) {
149 runningProcesses
.remove(process
.getUuid());
153 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
159 public void setModulesManager(ExecutionModulesManager modulesManager
) {
160 this.modulesManager
= modulesManager
;
163 protected SlcAgentDescriptor
getAgentDescriptor() {
164 return agentDescriptor
;
167 public String
getAgentUuid() {
168 return agentDescriptor
.getUuid();
172 public String
toString() {
173 return agentDescriptor
.toString();