]> git.argeo.org Git - gpl/argeo-slc.git/blob - DefaultAgent.java
909fa17603382be0c74c21bdd4f757b0d3ae95ae
[gpl/argeo-slc.git] / 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.List;
23 import java.util.Map;
24 import java.util.UUID;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.argeo.slc.execution.ExecutionModuleDescriptor;
29 import org.argeo.slc.execution.ExecutionModulesManager;
30 import org.argeo.slc.execution.ExecutionProcess;
31 import org.argeo.slc.execution.ExecutionProcessNotifier;
32 import org.argeo.slc.execution.ExecutionStep;
33 import org.argeo.slc.execution.SlcAgent;
34 import org.argeo.slc.execution.SlcAgentDescriptor;
35
36 /** Implements the base methods of an SLC agent. */
37 public class DefaultAgent implements SlcAgent, ExecutionProcessNotifier {
38 private final static Log log = LogFactory.getLog(DefaultAgent.class);
39
40 private SlcAgentDescriptor agentDescriptor;
41 private ExecutionModulesManager modulesManager;
42
43 private ThreadGroup processesThreadGroup;
44 private Map<String, ProcessThread> runningProcesses = Collections
45 .synchronizedMap(new HashMap<String, ProcessThread>());
46
47 /*
48 * LIFECYCLE
49 */
50 /** Initialization */
51 public void init() {
52 agentDescriptor = new SlcAgentDescriptor();
53 agentDescriptor.setUuid(initAgentUuid());
54 try {
55 agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
56 } catch (UnknownHostException e) {
57 log.error("Cannot resolve localhost host name: " + e.getMessage());
58 agentDescriptor.setHost("localhost");
59 }
60 processesThreadGroup = new ThreadGroup("SLC Processes of Agent #"
61 + agentDescriptor.getUuid());
62 modulesManager.registerProcessNotifier(this,
63 new HashMap<String, String>());
64
65 // final String module = System
66 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY);
67 // final String flow = System
68 // .getProperty(ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY);
69 // if (module != null) {
70 // // launch a flow and stops
71 // new Thread("Unique Flow") {
72 // @Override
73 // public void run() {
74 // executeFlowAndExit(module, null, flow);
75 // }
76 // }.start();
77 // }
78 }
79
80 /** Clean up (needs to be called by overriding method) */
81 public void destroy() {
82 modulesManager.unregisterProcessNotifier(this,
83 new HashMap<String, String>());
84 }
85
86 /**
87 * Called during initialization in order to determines the agent UUID. To be
88 * overridden. By default creates a new one per instance.
89 */
90 protected String initAgentUuid() {
91 return UUID.randomUUID().toString();
92 }
93
94 /*
95 * UNIQUE FLOW
96 */
97 // protected void executeFlowAndExit(final String module,
98 // final String version, final String flow) {
99 // }
100
101 /*
102 * SLC AGENT
103 */
104 public void process(ExecutionProcess process) {
105 ProcessThread processThread = createProcessThread(processesThreadGroup,
106 modulesManager, process);
107 processThread.start();
108 runningProcesses.put(process.getUuid(), processThread);
109 // FIXME find a way to remove them from this register
110 }
111
112 public void kill(ExecutionProcess process) {
113 String processUuid = process.getUuid();
114 if (runningProcesses.containsKey(processUuid)) {
115 runningProcesses.get(processUuid).interrupt();
116 }
117 }
118
119 /** Creates the thread which will coordinate the execution for this agent. */
120 protected ProcessThread createProcessThread(
121 ThreadGroup processesThreadGroup,
122 ExecutionModulesManager modulesManager, ExecutionProcess process) {
123 ProcessThread processThread = new ProcessThread(processesThreadGroup,
124 modulesManager, process);
125 return processThread;
126 }
127
128 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
129 String moduleName, String version) {
130 return modulesManager.getExecutionModuleDescriptor(moduleName, version);
131 }
132
133 public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
134 return modulesManager.listExecutionModules();
135 }
136
137 public boolean ping() {
138 return true;
139 }
140
141 /*
142 * PROCESS NOTIFIER
143 */
144 public void updateStatus(ExecutionProcess process, String oldStatus,
145 String newStatus) {
146 if (newStatus.equals(ExecutionProcess.COMPLETED)
147 || newStatus.equals(ExecutionProcess.ERROR)
148 || newStatus.equals(ExecutionProcess.KILLED)) {
149 runningProcesses.remove(process.getUuid());
150 }
151 }
152
153 public void addSteps(ExecutionProcess process, List<ExecutionStep> steps) {
154 }
155
156 /*
157 * BEAN
158 */
159 public void setModulesManager(ExecutionModulesManager modulesManager) {
160 this.modulesManager = modulesManager;
161 }
162
163 protected SlcAgentDescriptor getAgentDescriptor() {
164 return agentDescriptor;
165 }
166
167 public String getAgentUuid() {
168 return agentDescriptor.getUuid();
169 }
170
171 @Override
172 public String toString() {
173 return agentDescriptor.toString();
174 }
175 }