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