2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.runtime
;
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
.SlcException
;
29 import org
.argeo
.slc
.core
.execution
.ProcessThread
;
30 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
31 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
32 import org
.argeo
.slc
.execution
.ExecutionProcess
;
33 import org
.argeo
.slc
.execution
.ExecutionProcessNotifier
;
34 import org
.argeo
.slc
.execution
.ExecutionStep
;
35 import org
.argeo
.slc
.process
.SlcExecution
;
36 import org
.argeo
.slc
.runtime
.SlcAgent
;
37 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
39 /** Implements the base methods of an SLC agent. */
40 @SuppressWarnings("deprecation")
41 public class DefaultAgent
implements SlcAgent
, ExecutionProcessNotifier
{
42 private final static Log log
= LogFactory
.getLog(DefaultAgent
.class);
44 private SlcAgentDescriptor agentDescriptor
;
45 private ExecutionModulesManager modulesManager
;
47 private ThreadGroup processesThreadGroup
;
48 private Map
<String
, ProcessThread
> runningProcesses
= Collections
49 .synchronizedMap(new HashMap
<String
, ProcessThread
>());
56 agentDescriptor
= new SlcAgentDescriptor();
57 agentDescriptor
.setUuid(initAgentUuid());
59 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
60 } catch (UnknownHostException e
) {
61 log
.error("Cannot resolve localhost host name: " + e
.getMessage());
62 agentDescriptor
.setHost("localhost");
64 processesThreadGroup
= new ThreadGroup("SLC Processes of Agent #"
65 + agentDescriptor
.getUuid());
66 modulesManager
.registerProcessNotifier(this,
67 new HashMap
<String
, String
>());
70 /** Clean up (needs to be called by overriding method) */
71 public void destroy() {
72 modulesManager
.unregisterProcessNotifier(this,
73 new HashMap
<String
, String
>());
77 * Called during initialization in order to determines the agent UUID. To be
78 * overridden. By default creates a new one per instance.
80 protected String
initAgentUuid() {
81 return UUID
.randomUUID().toString();
87 public void process(ExecutionProcess process
) {
88 ProcessThread processThread
= createProcessThread(processesThreadGroup
,
89 modulesManager
, process
);
90 processThread
.start();
91 runningProcesses
.put(process
.getUuid(), processThread
);
92 // FIXME find a way to remove them from this register
95 public void kill(ExecutionProcess process
) {
96 String processUuid
= process
.getUuid();
97 if (runningProcesses
.containsKey(processUuid
)) {
98 runningProcesses
.get(processUuid
).interrupt();
102 /** Creates the thread which will coordinate the execution for this agent. */
103 protected ProcessThread
createProcessThread(
104 ThreadGroup processesThreadGroup
,
105 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
106 if (!(process
instanceof SlcExecution
))
107 throw new SlcException("Unsupported process type "
108 + process
.getClass());
109 ProcessThread processThread
= new ProcessThread(processesThreadGroup
,
110 modulesManager
, (SlcExecution
) process
);
111 return processThread
;
114 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
115 String moduleName
, String version
) {
116 return modulesManager
.getExecutionModuleDescriptor(moduleName
, version
);
119 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
120 return modulesManager
.listExecutionModules();
123 public boolean ping() {
130 public void updateStatus(ExecutionProcess process
, String oldStatus
,
132 if (newStatus
.equals(ExecutionProcess
.COMPLETED
)
133 || newStatus
.equals(ExecutionProcess
.ERROR
)
134 || newStatus
.equals(ExecutionProcess
.KILLED
)) {
135 runningProcesses
.remove(process
.getUuid());
139 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
145 public void setModulesManager(ExecutionModulesManager modulesManager
) {
146 this.modulesManager
= modulesManager
;
149 protected SlcAgentDescriptor
getAgentDescriptor() {
150 return agentDescriptor
;
153 public String
getAgentUuid() {
154 return agentDescriptor
.getUuid();
158 public String
toString() {
159 return agentDescriptor
.toString();