2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.core
.runtime
;
19 import java
.net
.InetAddress
;
20 import java
.net
.UnknownHostException
;
21 import java
.util
.Collections
;
22 import java
.util
.HashMap
;
23 import java
.util
.List
;
25 import java
.util
.UUID
;
27 import org
.argeo
.slc
.SlcException
;
28 import org
.argeo
.slc
.core
.execution
.ProcessThread
;
29 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
30 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
31 import org
.argeo
.slc
.execution
.ExecutionProcess
;
32 import org
.argeo
.slc
.execution
.ExecutionProcessNotifier
;
33 import org
.argeo
.slc
.execution
.ExecutionStep
;
34 import org
.argeo
.slc
.process
.SlcExecution
;
35 import org
.argeo
.slc
.runtime
.SlcAgent
;
36 import org
.argeo
.slc
.runtime
.SlcAgentDescriptor
;
38 /** Implements the base methods of an SLC agent. */
39 public class DefaultAgent
implements SlcAgent
, ExecutionProcessNotifier
{
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());
54 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
55 } catch (UnknownHostException e
) {
56 throw new SlcException("Unable to create agent descriptor.", e
);
58 processesThreadGroup
= new ThreadGroup("SLC Processes of Agent #"
59 + agentDescriptor
.getUuid());
60 modulesManager
.registerProcessNotifier(this,
61 new HashMap
<String
, String
>());
64 public void dispose() {
65 modulesManager
.unregisterProcessNotifier(this,
66 new HashMap
<String
, String
>());
70 * Called during initialization in order to determines the agent UUID. To be
71 * overridden. By default creates a new one per instance.
73 protected String
initAgentUuid() {
74 return UUID
.randomUUID().toString();
80 public void runSlcExecution(SlcExecution slcExecution
) {
81 process(slcExecution
);
84 public void process(ExecutionProcess process
) {
85 ProcessThread processThread
= createProcessThread(processesThreadGroup
,
86 modulesManager
, process
);
87 processThread
.start();
88 runningProcesses
.put(process
.getUuid(), processThread
);
89 // FIXME find a way to remove them from this register
92 public void kill(ExecutionProcess process
) {
93 String processUuid
= process
.getUuid();
94 if (runningProcesses
.containsKey(processUuid
)) {
95 runningProcesses
.get(processUuid
).interrupt();
99 /** Creates the thread which will coordinate the execution for this agent. */
100 protected ProcessThread
createProcessThread(
101 ThreadGroup processesThreadGroup
,
102 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
103 if (!(process
instanceof SlcExecution
))
104 throw new SlcException("Unsupported process type "
105 + process
.getClass());
106 ProcessThread processThread
= new ProcessThread(processesThreadGroup
,
107 modulesManager
, (SlcExecution
) process
);
108 return processThread
;
111 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
112 String moduleName
, String version
) {
113 return modulesManager
.getExecutionModuleDescriptor(moduleName
, version
);
116 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
117 return modulesManager
.listExecutionModules();
120 public boolean ping() {
127 public void updateStatus(ExecutionProcess process
, String oldStatus
,
129 if (newStatus
.equals(ExecutionProcess
.COMPLETED
)
130 || newStatus
.equals(ExecutionProcess
.ERROR
)
131 || newStatus
.equals(ExecutionProcess
.KILLED
)) {
132 runningProcesses
.remove(process
.getUuid());
136 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
142 public void setModulesManager(ExecutionModulesManager modulesManager
) {
143 this.modulesManager
= modulesManager
;
146 protected SlcAgentDescriptor
getAgentDescriptor() {
147 return agentDescriptor
;
150 public String
getAgentUuid() {
151 return agentDescriptor
.getUuid();
155 public String
toString() {
156 return agentDescriptor
.toString();