]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultAgent.java
7d6ff315e0df40ebc99ded44d13ddc481414b594
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultAgent.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.core.execution;
17
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;
24 import java.util.Map;
25 import java.util.UUID;
26
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;
34
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);
38
39 private SlcAgentDescriptor agentDescriptor;
40 private ExecutionModulesManager modulesManager;
41
42 private ThreadGroup processesThreadGroup;
43 private Map<String, ProcessThread> runningProcesses = Collections
44 .synchronizedMap(new HashMap<String, ProcessThread>());
45
46 /*
47 * LIFECYCLE
48 */
49 /** Initialization */
50 public void init() {
51 agentDescriptor = new SlcAgentDescriptor();
52 agentDescriptor.setUuid(initAgentUuid());
53 try {
54 agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
55 } catch (UnknownHostException e) {
56 log.error("Cannot resolve localhost host name: " + e.getMessage());
57 agentDescriptor.setHost("localhost");
58 }
59 processesThreadGroup = new ThreadGroup("SLC Processes of Agent #"
60 + agentDescriptor.getUuid());
61 // modulesManager.registerProcessNotifier(this,
62 // new HashMap<String, String>());
63
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") {
71 // @Override
72 // public void run() {
73 // executeFlowAndExit(module, null, flow);
74 // }
75 // }.start();
76 // }
77 }
78
79 /** Clean up (needs to be called by overriding method) */
80 public void destroy() {
81 // modulesManager.unregisterProcessNotifier(this,
82 // new HashMap<String, String>());
83 }
84
85 /**
86 * Called during initialization in order to determines the agent UUID. To be
87 * overridden. By default creates a new one per instance.
88 */
89 protected String initAgentUuid() {
90 return UUID.randomUUID().toString();
91 }
92
93 /*
94 * UNIQUE FLOW
95 */
96 // protected void executeFlowAndExit(final String module,
97 // final String version, final String flow) {
98 // }
99
100 /*
101 * SLC AGENT
102 */
103 public void process(ExecutionProcess process) {
104 ProcessThread processThread = createProcessThread(processesThreadGroup,
105 modulesManager, process);
106 processThread.start();
107 runningProcesses.put(process.getUuid(), processThread);
108
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())
114 it.remove();
115 }
116 }
117
118 public void kill(ExecutionProcess process) {
119 String processUuid = process.getUuid();
120 if (runningProcesses.containsKey(processUuid)) {
121 runningProcesses.get(processUuid).interrupt();
122 }
123 }
124
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;
132 }
133
134 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
135 String moduleName, String version) {
136 return modulesManager.getExecutionModuleDescriptor(moduleName, version);
137 }
138
139 public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
140 return modulesManager.listExecutionModules();
141 }
142
143 public boolean ping() {
144 return true;
145 }
146
147 /*
148 * PROCESS NOTIFIER
149 */
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());
156 // }
157 // }
158 //
159 // public void addSteps(ExecutionProcess process, List<ExecutionStep> steps)
160 // {
161 // }
162
163 /*
164 * BEAN
165 */
166 public void setModulesManager(ExecutionModulesManager modulesManager) {
167 this.modulesManager = modulesManager;
168 }
169
170 protected SlcAgentDescriptor getAgentDescriptor() {
171 return agentDescriptor;
172 }
173
174 public String getAgentUuid() {
175 return agentDescriptor.getUuid();
176 }
177
178 @Override
179 public String toString() {
180 return agentDescriptor.toString();
181 }
182 }