1 package org
.argeo
.slc
.core
.execution
;
4 import java
.net
.URLEncoder
;
5 import java
.util
.ArrayList
;
6 import java
.util
.Arrays
;
7 import java
.util
.Iterator
;
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
;
28 * Authenticates thread and executes synchronously a command line execution.
29 * Reference implementation of args to URIs algorithm.
31 public class DefaultAgentCli
implements SlcAgentCli
{
32 private final static Log log
= LogFactory
.getLog(DefaultAgentCli
.class);
34 private final static String UTF8
= "UTF-8";
35 private SlcAgent agent
;
36 private AuthenticationManager authenticationManager
;
38 private Long timeout
= 24 * 60 * 60 * 1000l;
40 public String
process(String
[] args
) {
41 if (SecurityContextHolder
.getContext().getAuthentication() == null) {
42 OsAuthenticationToken oat
= new OsAuthenticationToken();
43 Authentication authentication
= authenticationManager
45 SecurityContextHolder
.getContext()
46 .setAuthentication(authentication
);
49 if (args
.length
> 0 && args
[0].equals("help")) {
50 StringBuilder buf
= new StringBuilder();
53 return buf
.toString();
55 List
<URI
> uris
= asURIs(args
);
56 String processUuid
= agent
.process(uris
);
57 agent
.waitFor(processUuid
, timeout
);
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
);
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
);
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());
85 List
<URI
> uris
= asURIs(args
);
86 for (URI uri
: uris
) {
87 appendUriHelp(uri
, buf
);
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());
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
);
104 throw new SlcException("Flow " + uri
+ " not found");
106 appendModule(emd
, buf
);
108 buf
.append(" ").append(efd
.getName());
109 if (efd
.getDescription() != null
110 && !efd
.getDescription().trim().equals(""))
111 buf
.append(" : ").append(" ").append(efd
.getDescription());
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(')');
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(")");
136 public static List
<URI
> asURIs(String
[] args
) {
138 List
<URI
> uris
= new ArrayList
<URI
>();
139 List
<String
> leftOvers
= new ArrayList
<String
>();
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
));
152 if (currUri
!= null) {
153 uris
.add(new URI(currUri
.toString()));
155 currUri
= new StringBuilder("flow:");
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
);
170 if (currUri
== null) {// first args
174 if (arg
.startsWith("--"))
175 key
= arg
.substring(2);
176 else if (arg
.startsWith("-"))
177 key
= arg
.substring(1);
179 throw new SlcException("Cannot intepret key: "
190 // deal with boolean keys
191 if (currKey
!= null) {// value
192 currUri
.append(URLEncoder
.encode("true", UTF8
));
197 currUri
.append(URLEncoder
.encode(key
, UTF8
))
203 uris
.add(new URI(currUri
.toString()));
205 } catch (Exception e
) {
206 throw new SlcException("Cannot convert " + Arrays
.toString(args
)
207 + " to flow URI", e
);
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("/"))
219 if (name
.endsWith("/"))
220 name
= name
.substring(0, name
.length() - 1);
221 if (name
.equals(flowPath
)) {
222 flowDescriptor
= efd
;
226 return flowDescriptor
;
229 public void setAgent(SlcAgent agent
) {
233 public void setAuthenticationManager(
234 AuthenticationManager authenticationManager
) {
235 this.authenticationManager
= authenticationManager
;
238 public void setTimeout(Long timeout
) {
239 this.timeout
= timeout
;