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 @SuppressWarnings("deprecation")
40 public class DefaultAgent
implements SlcAgent
, ExecutionProcessNotifier
{
41 private SlcAgentDescriptor agentDescriptor
;
42 private ExecutionModulesManager modulesManager
;
44 private ThreadGroup processesThreadGroup
;
45 private Map
<String
, ProcessThread
> runningProcesses
= Collections
46 .synchronizedMap(new HashMap
<String
, ProcessThread
>());
54 agentDescriptor
= new SlcAgentDescriptor();
55 agentDescriptor
.setUuid(initAgentUuid());
56 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
57 } catch (UnknownHostException e
) {
58 throw new SlcException("Unable to create agent descriptor.", e
);
60 processesThreadGroup
= new ThreadGroup("SLC Processes of Agent #"
61 + agentDescriptor
.getUuid());
62 modulesManager
.registerProcessNotifier(this,
63 new HashMap
<String
, String
>());
66 /** Clean up (needs to be called by overriding method) */
67 public void destroy() {
68 modulesManager
.unregisterProcessNotifier(this,
69 new HashMap
<String
, String
>());
73 * Called during initialization in order to determines the agent UUID. To be
74 * overridden. By default creates a new one per instance.
76 protected String
initAgentUuid() {
77 return UUID
.randomUUID().toString();
83 public void process(ExecutionProcess process
) {
84 ProcessThread processThread
= createProcessThread(processesThreadGroup
,
85 modulesManager
, process
);
86 processThread
.start();
87 runningProcesses
.put(process
.getUuid(), processThread
);
88 // FIXME find a way to remove them from this register
91 public void kill(ExecutionProcess process
) {
92 String processUuid
= process
.getUuid();
93 if (runningProcesses
.containsKey(processUuid
)) {
94 runningProcesses
.get(processUuid
).interrupt();
98 /** Creates the thread which will coordinate the execution for this agent. */
99 protected ProcessThread
createProcessThread(
100 ThreadGroup processesThreadGroup
,
101 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
102 if (!(process
instanceof SlcExecution
))
103 throw new SlcException("Unsupported process type "
104 + process
.getClass());
105 ProcessThread processThread
= new ProcessThread(processesThreadGroup
,
106 modulesManager
, (SlcExecution
) process
);
107 return processThread
;
110 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
111 String moduleName
, String version
) {
112 return modulesManager
.getExecutionModuleDescriptor(moduleName
, version
);
115 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
116 return modulesManager
.listExecutionModules();
119 public boolean ping() {
126 public void updateStatus(ExecutionProcess process
, String oldStatus
,
128 if (newStatus
.equals(ExecutionProcess
.COMPLETED
)
129 || newStatus
.equals(ExecutionProcess
.ERROR
)
130 || newStatus
.equals(ExecutionProcess
.KILLED
)) {
131 runningProcesses
.remove(process
.getUuid());
135 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
141 public void setModulesManager(ExecutionModulesManager modulesManager
) {
142 this.modulesManager
= modulesManager
;
145 protected SlcAgentDescriptor
getAgentDescriptor() {
146 return agentDescriptor
;
149 public String
getAgentUuid() {
150 return agentDescriptor
.getUuid();
154 public String
toString() {
155 return agentDescriptor
.toString();