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