]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java
Save current state even if not completely stable
[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 public class DefaultAgent implements SlcAgent, ExecutionProcessNotifier {
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 public void init() {
51 try {
52 agentDescriptor = new SlcAgentDescriptor();
53 agentDescriptor.setUuid(initAgentUuid());
54 agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
55 } catch (UnknownHostException e) {
56 throw new SlcException("Unable to create agent descriptor.", e);
57 }
58 processesThreadGroup = new ThreadGroup("SLC Processes of Agent #"
59 + agentDescriptor.getUuid());
60 modulesManager.registerProcessNotifier(this,
61 new HashMap<String, String>());
62 }
63
64 public void dispose() {
65 modulesManager.unregisterProcessNotifier(this,
66 new HashMap<String, String>());
67 }
68
69 /**
70 * Called during initialization in order to determines the agent UUID. To be
71 * overridden. By default creates a new one per instance.
72 */
73 protected String initAgentUuid() {
74 return UUID.randomUUID().toString();
75 }
76
77 /*
78 * SLC AGENT
79 */
80 public void runSlcExecution(SlcExecution slcExecution) {
81 process(slcExecution);
82 }
83
84 public void process(ExecutionProcess process) {
85 ProcessThread processThread = createProcessThread(processesThreadGroup,
86 modulesManager, process);
87 processThread.start();
88 runningProcesses.put(process.getUuid(), processThread);
89 // FIXME find a way to remove them from this register
90 }
91
92 public void kill(ExecutionProcess process) {
93 String processUuid = process.getUuid();
94 if (runningProcesses.containsKey(processUuid)) {
95 runningProcesses.get(processUuid).interrupt();
96 }
97 }
98
99 /** Creates the thread which will coordinate the execution for this agent. */
100 protected ProcessThread createProcessThread(
101 ThreadGroup processesThreadGroup,
102 ExecutionModulesManager modulesManager, ExecutionProcess process) {
103 if (!(process instanceof SlcExecution))
104 throw new SlcException("Unsupported process type "
105 + process.getClass());
106 ProcessThread processThread = new ProcessThread(processesThreadGroup,
107 modulesManager, (SlcExecution) process);
108 return processThread;
109 }
110
111 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
112 String moduleName, String version) {
113 return modulesManager.getExecutionModuleDescriptor(moduleName, version);
114 }
115
116 public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
117 return modulesManager.listExecutionModules();
118 }
119
120 public boolean ping() {
121 return true;
122 }
123
124 /*
125 * PROCESS NOTIFIER
126 */
127 public void updateStatus(ExecutionProcess process, String oldStatus,
128 String newStatus) {
129 if (newStatus.equals(ExecutionProcess.COMPLETED)
130 || newStatus.equals(ExecutionProcess.ERROR)
131 || newStatus.equals(ExecutionProcess.KILLED)) {
132 runningProcesses.remove(process.getUuid());
133 }
134 }
135
136 public void addSteps(ExecutionProcess process, List<ExecutionStep> steps) {
137 }
138
139 /*
140 * BEAN
141 */
142 public void setModulesManager(ExecutionModulesManager modulesManager) {
143 this.modulesManager = modulesManager;
144 }
145
146 protected SlcAgentDescriptor getAgentDescriptor() {
147 return agentDescriptor;
148 }
149
150 public String getAgentUuid() {
151 return agentDescriptor.getUuid();
152 }
153
154 @Override
155 public String toString() {
156 return agentDescriptor.toString();
157 }
158 }