1 package org
.argeo
.slc
.runtime
;
4 import java
.net
.URLEncoder
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.Iterator
;
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
;
22 * Authenticates thread and executes synchronously a command line execution.
23 * Reference implementation of args to URIs algorithm.
25 public class DefaultAgentCli
implements SlcAgentCli
{
26 private final static String UTF8
= "UTF-8";
27 private SlcAgent agent
;
28 // private AuthenticationManager authenticationManager;
30 private Long timeout
= 24 * 60 * 60 * 1000l;
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);
41 if (args
.length
> 0 && args
[0].equals("help")) {
42 StringBuilder buf
= new StringBuilder();
44 // log.info("\n" + buf);
45 return buf
.toString();
47 List
<URI
> uris
= asURIs(args
);
48 String processUuid
= agent
.process(uris
);
49 agent
.waitFor(processUuid
, timeout
);
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
);
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
);
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());
77 List
<URI
> uris
= asURIs(args
);
78 for (URI uri
: uris
) {
79 appendUriHelp(uri
, buf
);
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());
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
);
96 throw new SlcException("Flow " + uri
+ " not found");
98 appendModule(emd
, buf
);
100 buf
.append(" ").append(efd
.getName());
101 if (efd
.getDescription() != null
102 && !efd
.getDescription().trim().equals(""))
103 buf
.append(" : ").append(" ").append(efd
.getDescription());
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(')');
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(")");
128 public static List
<URI
> asURIs(String
[] args
) {
130 List
<URI
> uris
= new ArrayList
<URI
>();
131 List
<String
> leftOvers
= new ArrayList
<String
>();
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
));
144 if (currUri
!= null) {
145 uris
.add(new URI(currUri
.toString()));
147 currUri
= new StringBuilder("flow:");
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
);
162 if (currUri
== null) {// first args
166 if (arg
.startsWith("--"))
167 key
= arg
.substring(2);
168 else if (arg
.startsWith("-"))
169 key
= arg
.substring(1);
171 throw new SlcException("Cannot intepret key: "
182 // deal with boolean keys
183 if (currKey
!= null) {// value
184 currUri
.append(URLEncoder
.encode("true", UTF8
));
189 currUri
.append(URLEncoder
.encode(key
, UTF8
))
195 uris
.add(new URI(currUri
.toString()));
197 } catch (Exception e
) {
198 throw new SlcException("Cannot convert " + Arrays
.toString(args
)
199 + " to flow URI", e
);
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("/"))
211 if (name
.endsWith("/"))
212 name
= name
.substring(0, name
.length() - 1);
213 if (name
.equals(flowPath
)) {
214 flowDescriptor
= efd
;
218 return flowDescriptor
;
221 public void setAgent(SlcAgent agent
) {
225 // public void setAuthenticationManager(
226 // AuthenticationManager authenticationManager) {
227 // this.authenticationManager = authenticationManager;
230 public void setTimeout(Long timeout
) {
231 this.timeout
= timeout
;