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
.Iterator
;
23 import java
.util
.List
;
25 import java
.util
.UUID
;
27 import org
.apache
.commons
.logging
.Log
;
28 import org
.apache
.commons
.logging
.LogFactory
;
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
.SlcAgent
;
33 import org
.argeo
.slc
.execution
.SlcAgentDescriptor
;
35 /** Implements the base methods of an SLC agent. */
36 public class DefaultAgent
implements SlcAgent
{
37 private final static Log log
= LogFactory
.getLog(DefaultAgent
.class);
39 private SlcAgentDescriptor agentDescriptor
;
40 private ExecutionModulesManager modulesManager
;
42 private ThreadGroup processesThreadGroup
;
43 private Map
<String
, ProcessThread
> runningProcesses
= Collections
44 .synchronizedMap(new HashMap
<String
, ProcessThread
>());
51 agentDescriptor
= new SlcAgentDescriptor();
52 agentDescriptor
.setUuid(initAgentUuid());
54 agentDescriptor
.setHost(InetAddress
.getLocalHost().getHostName());
55 } catch (UnknownHostException e
) {
56 log
.error("Cannot resolve localhost host name: " + e
.getMessage());
57 agentDescriptor
.setHost("localhost");
59 processesThreadGroup
= new ThreadGroup("SLC Processes of Agent #"
60 + agentDescriptor
.getUuid());
61 // modulesManager.registerProcessNotifier(this,
62 // new HashMap<String, String>());
64 // final String module = System
65 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY);
66 // final String flow = System
67 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY);
68 // if (module != null) {
69 // // launch a flow and stops
70 // new Thread("Unique Flow") {
72 // public void run() {
73 // executeFlowAndExit(module, null, flow);
79 /** Clean up (needs to be called by overriding method) */
80 public void destroy() {
81 // modulesManager.unregisterProcessNotifier(this,
82 // new HashMap<String, String>());
86 * Called during initialization in order to determines the agent UUID. To be
87 * overridden. By default creates a new one per instance.
89 protected String
initAgentUuid() {
90 return UUID
.randomUUID().toString();
96 // protected void executeFlowAndExit(final String module,
97 // final String version, final String flow) {
103 public void process(ExecutionProcess process
) {
104 ProcessThread processThread
= createProcessThread(processesThreadGroup
,
105 modulesManager
, process
);
106 processThread
.start();
107 runningProcesses
.put(process
.getUuid(), processThread
);
109 // clean up old processes
110 Iterator
<ProcessThread
> it
= runningProcesses
.values().iterator();
111 while (it
.hasNext()) {
112 ProcessThread pThread
= it
.next();
113 if (!pThread
.isAlive())
118 public void kill(ExecutionProcess process
) {
119 String processUuid
= process
.getUuid();
120 if (runningProcesses
.containsKey(processUuid
)) {
121 runningProcesses
.get(processUuid
).interrupt();
125 /** Creates the thread which will coordinate the execution for this agent. */
126 protected ProcessThread
createProcessThread(
127 ThreadGroup processesThreadGroup
,
128 ExecutionModulesManager modulesManager
, ExecutionProcess process
) {
129 ProcessThread processThread
= new ProcessThread(processesThreadGroup
,
130 modulesManager
, process
);
131 return processThread
;
134 public ExecutionModuleDescriptor
getExecutionModuleDescriptor(
135 String moduleName
, String version
) {
136 return modulesManager
.getExecutionModuleDescriptor(moduleName
, version
);
139 public List
<ExecutionModuleDescriptor
> listExecutionModuleDescriptors() {
140 return modulesManager
.listExecutionModules();
143 public boolean ping() {
150 // public void updateStatus(ExecutionProcess process, String oldStatus,
151 // String newStatus) {
152 // if (newStatus.equals(ExecutionProcess.COMPLETED)
153 // || newStatus.equals(ExecutionProcess.ERROR)
154 // || newStatus.equals(ExecutionProcess.KILLED)) {
155 // runningProcesses.remove(process.getUuid());
159 // public void addSteps(ExecutionProcess process, List<ExecutionStep> steps)
166 public void setModulesManager(ExecutionModulesManager modulesManager
) {
167 this.modulesManager
= modulesManager
;
170 protected SlcAgentDescriptor
getAgentDescriptor() {
171 return agentDescriptor
;
174 public String
getAgentUuid() {
175 return agentDescriptor
.getUuid();
179 public String
toString() {
180 return agentDescriptor
.toString();