]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java
Adapt to new build number approach
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / runtime / DefaultAgent.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.core.runtime;
18
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;
24 import java.util.Map;
25 import java.util.UUID;
26
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;
37
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;
43
44 private ThreadGroup processesThreadGroup;
45 private Map<String, ProcessThread> runningProcesses = Collections
46 .synchronizedMap(new HashMap<String, ProcessThread>());
47
48 /*
49 * LIFECYCLE
50 */
51 /** Initialization */
52 public void init() {
53 try {
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);
59 }
60 processesThreadGroup = new ThreadGroup("SLC Processes of Agent #"
61 + agentDescriptor.getUuid());
62 modulesManager.registerProcessNotifier(this,
63 new HashMap<String, String>());
64 }
65
66 /** Clean up (needs to be called by overriding method) */
67 public void destroy() {
68 modulesManager.unregisterProcessNotifier(this,
69 new HashMap<String, String>());
70 }
71
72 /**
73 * Called during initialization in order to determines the agent UUID. To be
74 * overridden. By default creates a new one per instance.
75 */
76 protected String initAgentUuid() {
77 return UUID.randomUUID().toString();
78 }
79
80 /*
81 * SLC AGENT
82 */
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
89 }
90
91 public void kill(ExecutionProcess process) {
92 String processUuid = process.getUuid();
93 if (runningProcesses.containsKey(processUuid)) {
94 runningProcesses.get(processUuid).interrupt();
95 }
96 }
97
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;
108 }
109
110 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
111 String moduleName, String version) {
112 return modulesManager.getExecutionModuleDescriptor(moduleName, version);
113 }
114
115 public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
116 return modulesManager.listExecutionModules();
117 }
118
119 public boolean ping() {
120 return true;
121 }
122
123 /*
124 * PROCESS NOTIFIER
125 */
126 public void updateStatus(ExecutionProcess process, String oldStatus,
127 String newStatus) {
128 if (newStatus.equals(ExecutionProcess.COMPLETED)
129 || newStatus.equals(ExecutionProcess.ERROR)
130 || newStatus.equals(ExecutionProcess.KILLED)) {
131 runningProcesses.remove(process.getUuid());
132 }
133 }
134
135 public void addSteps(ExecutionProcess process, List<ExecutionStep> steps) {
136 }
137
138 /*
139 * BEAN
140 */
141 public void setModulesManager(ExecutionModulesManager modulesManager) {
142 this.modulesManager = modulesManager;
143 }
144
145 protected SlcAgentDescriptor getAgentDescriptor() {
146 return agentDescriptor;
147 }
148
149 public String getAgentUuid() {
150 return agentDescriptor.getUuid();
151 }
152
153 @Override
154 public String toString() {
155 return agentDescriptor.toString();
156 }
157 }