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