]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.runtime/src/org/argeo/slc/runtime/DefaultAgentCli.java
Introduce mbox support
[gpl/argeo-slc.git] / org.argeo.slc.runtime / src / org / argeo / slc / runtime / DefaultAgentCli.java
1 package org.argeo.slc.runtime;
2
3 import java.net.URI;
4 import java.net.URLEncoder;
5 import java.util.ArrayList;
6 import java.util.Arrays;
7 import java.util.Iterator;
8 import java.util.List;
9 import java.util.Map;
10
11 import org.argeo.slc.DefaultNameVersion;
12 import org.argeo.slc.NameVersion;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.execution.ExecutionFlowDescriptor;
15 import org.argeo.slc.execution.ExecutionModuleDescriptor;
16 import org.argeo.slc.execution.ExecutionSpec;
17 import org.argeo.slc.execution.ExecutionSpecAttribute;
18 import org.argeo.slc.execution.SlcAgent;
19 import org.argeo.slc.execution.SlcAgentCli;
20
21 /**
22 * Authenticates thread and executes synchronously a command line execution.
23 * Reference implementation of args to URIs algorithm.
24 */
25 public class DefaultAgentCli implements SlcAgentCli {
26 private final static String UTF8 = "UTF-8";
27 private SlcAgent agent;
28 // private AuthenticationManager authenticationManager;
29
30 private Long timeout = 24 * 60 * 60 * 1000l;
31
32 public String process(String[] args) {
33 // if (SecurityContextHolder.getContext().getAuthentication() == null) {
34 // OsAuthenticationToken oat = new OsAuthenticationToken();
35 // Authentication authentication = authenticationManager
36 // .authenticate(oat);
37 // SecurityContextHolder.getContext()
38 // .setAuthentication(authentication);
39 // }
40
41 if (args.length > 0 && args[0].equals("help")) {
42 StringBuilder buf = new StringBuilder();
43 help(args, buf);
44 // log.info("\n" + buf);
45 return buf.toString();
46 } else {
47 List<URI> uris = asURIs(args);
48 String processUuid = agent.process(uris);
49 agent.waitFor(processUuid, timeout);
50 return processUuid;
51 }
52 }
53
54 protected void help(String[] rawArgs, StringBuilder buf) {
55 String[] args = Arrays.copyOfRange(rawArgs, 1, rawArgs.length);
56 if (args.length == 0) {// modules
57 for (ExecutionModuleDescriptor emd : agent
58 .listExecutionModuleDescriptors()) {
59 appendModule(emd, buf);
60 }
61 } else if (args.length == 1 && !args[0].contains("/")) {// single module
62 NameVersion nameVersion = new DefaultNameVersion(args[0]);
63 ExecutionModuleDescriptor emd = agent.getExecutionModuleDescriptor(
64 nameVersion.getName(), nameVersion.getVersion());
65 appendModule(emd, buf);
66
67 // flows
68 for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) {
69 buf.append(" ").append(efd.getName());
70 if (efd.getDescription() != null
71 && !efd.getDescription().trim().equals(""))
72 buf.append(" : ").append(" ").append(efd.getDescription());
73 buf.append('\n');
74 }
75 return;
76 } else {
77 List<URI> uris = asURIs(args);
78 for (URI uri : uris) {
79 appendUriHelp(uri, buf);
80 }
81 }
82 }
83
84 protected void appendUriHelp(URI uri, StringBuilder buf) {
85 String[] path = uri.getPath().split("/");
86 NameVersion nameVersion = new DefaultNameVersion(path[1]);
87 ExecutionModuleDescriptor emd = agent.getExecutionModuleDescriptor(
88 nameVersion.getName(), nameVersion.getVersion());
89
90 StringBuilder flow = new StringBuilder();
91 for (int i = 2; i < path.length; i++)
92 flow.append('/').append(path[i]);
93 String flowPath = flow.toString();
94 ExecutionFlowDescriptor efd = findExecutionFlowDescriptor(emd, flowPath);
95 if (efd == null)
96 throw new SlcException("Flow " + uri + " not found");
97
98 appendModule(emd, buf);
99
100 buf.append(" ").append(efd.getName());
101 if (efd.getDescription() != null
102 && !efd.getDescription().trim().equals(""))
103 buf.append(" : ").append(" ").append(efd.getDescription());
104 buf.append('\n');
105 Map<String, Object> values = DefaultAgent.getQueryMap(uri.getQuery());
106 ExecutionSpec spec = efd.getExecutionSpec();
107 for (String attrKey : spec.getAttributes().keySet()) {
108 ExecutionSpecAttribute esa = spec.getAttributes().get(attrKey);
109 buf.append(" --").append(attrKey);
110 if (values.containsKey(attrKey))
111 buf.append(" ").append(values.get(attrKey));
112 if (esa.getValue() != null)
113 buf.append(" (").append(esa.getValue()).append(')');
114 buf.append('\n');
115 }
116 }
117
118 private void appendModule(ExecutionModuleDescriptor emd, StringBuilder buf) {
119 buf.append("# ").append(emd.getName());
120 if (emd.getDescription() != null
121 && !emd.getDescription().trim().equals(""))
122 buf.append(" : ").append(emd.getDescription());
123 if (emd.getVersion() != null)
124 buf.append(" (v").append(emd.getVersion()).append(")");
125 buf.append('\n');
126 }
127
128 public static List<URI> asURIs(String[] args) {
129 try {
130 List<URI> uris = new ArrayList<URI>();
131 List<String> leftOvers = new ArrayList<String>();
132
133 Boolean hasArgs = false;
134 String currKey = null;
135 StringBuilder currUri = null;
136 Iterator<String> argIt = Arrays.asList(args).iterator();
137 while (argIt.hasNext()) {
138 String arg = argIt.next();
139 if (!arg.startsWith("-")) {
140 if (currKey != null) {// value
141 currUri.append(URLEncoder.encode(arg, UTF8));
142 currKey = null;
143 } else { // module
144 if (currUri != null) {
145 uris.add(new URI(currUri.toString()));
146 }
147 currUri = new StringBuilder("flow:");
148
149 String currModule = arg;
150 currUri.append('/').append(currModule);
151 if (!arg.contains("/")) {
152 // flow path not in arg go to next arg
153 if (!argIt.hasNext())
154 throw new SlcException("No flow found");
155 String currFlow = argIt.next();
156 if (!currFlow.startsWith("/"))
157 currFlow = "/" + currFlow;
158 currUri.append(currFlow);
159 }
160 }
161 } else {
162 if (currUri == null) {// first args
163 leftOvers.add(arg);
164 } else {
165 String key;
166 if (arg.startsWith("--"))
167 key = arg.substring(2);
168 else if (arg.startsWith("-"))
169 key = arg.substring(1);
170 else {
171 throw new SlcException("Cannot intepret key: "
172 + arg);
173 }
174
175 if (!hasArgs) {
176 currUri.append('?');
177 hasArgs = true;
178 } else {
179 currUri.append('&');
180 }
181
182 // deal with boolean keys
183 if (currKey != null) {// value
184 currUri.append(URLEncoder.encode("true", UTF8));
185 currKey = null;
186 }
187
188 currKey = key;
189 currUri.append(URLEncoder.encode(key, UTF8))
190 .append('=');
191 }
192 }
193 }
194 if (currUri != null)
195 uris.add(new URI(currUri.toString()));
196 return uris;
197 } catch (Exception e) {
198 throw new SlcException("Cannot convert " + Arrays.toString(args)
199 + " to flow URI", e);
200 }
201 }
202
203 private ExecutionFlowDescriptor findExecutionFlowDescriptor(
204 ExecutionModuleDescriptor emd, String flowPath) {
205 ExecutionFlowDescriptor flowDescriptor = null;
206 for (ExecutionFlowDescriptor efd : emd.getExecutionFlows()) {
207 String name = efd.getName();
208 // normalize name as flow path
209 if (!name.startsWith("/"))
210 name = "/" + name;
211 if (name.endsWith("/"))
212 name = name.substring(0, name.length() - 1);
213 if (name.equals(flowPath)) {
214 flowDescriptor = efd;
215 break;
216 }
217 }
218 return flowDescriptor;
219 }
220
221 public void setAgent(SlcAgent agent) {
222 this.agent = agent;
223 }
224
225 // public void setAuthenticationManager(
226 // AuthenticationManager authenticationManager) {
227 // this.authenticationManager = authenticationManager;
228 // }
229
230 public void setTimeout(Long timeout) {
231 this.timeout = timeout;
232 }
233
234 }