]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/runtime/DefaultAgent.java
JCR UI can run processes
[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.List;
22 import java.util.UUID;
23
24 import org.argeo.slc.SlcException;
25 import org.argeo.slc.core.execution.ProcessThread;
26 import org.argeo.slc.execution.ExecutionModuleDescriptor;
27 import org.argeo.slc.execution.ExecutionModulesManager;
28 import org.argeo.slc.execution.ExecutionProcess;
29 import org.argeo.slc.process.SlcExecution;
30 import org.argeo.slc.runtime.SlcAgent;
31 import org.argeo.slc.runtime.SlcAgentDescriptor;
32
33 /** Implements the base methods of an SLC agent. */
34 public class DefaultAgent implements SlcAgent {
35 private SlcAgentDescriptor agentDescriptor;
36 private ExecutionModulesManager modulesManager;
37
38 /*
39 * LIFECYCLE
40 */
41 public void init() {
42 try {
43 agentDescriptor = new SlcAgentDescriptor();
44 agentDescriptor.setUuid(initAgentUuid());
45 agentDescriptor.setHost(InetAddress.getLocalHost().getHostName());
46 } catch (UnknownHostException e) {
47 throw new SlcException("Unable to create agent descriptor.", e);
48 }
49 }
50
51 public void dispose() {
52
53 }
54
55 /**
56 * Called during initialization in order to determines the agent UUID. To be
57 * overridden. By default creates a new one per instance.
58 */
59 protected String initAgentUuid() {
60 return UUID.randomUUID().toString();
61 }
62
63 /*
64 * SLC AGENT
65 */
66 public void runSlcExecution(SlcExecution slcExecution) {
67 process(slcExecution);
68 }
69
70 public void process(ExecutionProcess process) {
71 ProcessThread processThread = createProcessThread(modulesManager,
72 process);
73 processThread.start();
74 }
75
76 /** Creates the thread which will coordinate the execution for this agent. */
77 protected ProcessThread createProcessThread(
78 ExecutionModulesManager modulesManager, ExecutionProcess process) {
79 if (!(process instanceof SlcExecution))
80 throw new SlcException("Unsupported process type "
81 + process.getClass());
82 ProcessThread processThread = new ProcessThread(modulesManager,
83 (SlcExecution) process);
84 return processThread;
85 }
86
87 public ExecutionModuleDescriptor getExecutionModuleDescriptor(
88 String moduleName, String version) {
89 return modulesManager.getExecutionModuleDescriptor(moduleName, version);
90 }
91
92 public List<ExecutionModuleDescriptor> listExecutionModuleDescriptors() {
93 return modulesManager.listExecutionModules();
94 }
95
96 public boolean ping() {
97 return true;
98 }
99
100 /*
101 * BEAN
102 */
103 public void setModulesManager(ExecutionModulesManager modulesManager) {
104 this.modulesManager = modulesManager;
105 }
106
107 protected SlcAgentDescriptor getAgentDescriptor() {
108 return agentDescriptor;
109 }
110
111 public String getAgentUuid() {
112 return agentDescriptor.getUuid();
113 }
114
115 @Override
116 public String toString() {
117 return agentDescriptor.toString();
118 }
119 }