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
.apache
.commons
.logging
.Log
;
12 import org
.apache
.commons
.logging
.LogFactory
;
13 import org
.argeo
.slc
.DefaultNameVersion
;
14 import org
.argeo
.slc
.NameVersion
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.execution
.ExecutionFlowDescriptor
;
17 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
18 import org
.argeo
.slc
.execution
.ExecutionSpec
;
19 import org
.argeo
.slc
.execution
.ExecutionSpecAttribute
;
20 import org
.argeo
.slc
.execution
.SlcAgent
;
21 import org
.argeo
.slc
.execution
.SlcAgentCli
;
24 * Authenticates thread and executes synchronously a command line execution.
25 * Reference implementation of args to URIs algorithm.
27 public class DefaultAgentCli
implements SlcAgentCli
{
28 private final static Log log
= LogFactory
.getLog(DefaultAgentCli
.class);
30 private final static String UTF8
= "UTF-8";
31 private SlcAgent agent
;
32 // private AuthenticationManager authenticationManager;
34 private Long timeout
= 24 * 60 * 60 * 1000l;
36 public String
process(String
[] args
) {
37 // if (SecurityContextHolder.getContext().getAuthentication() == null) {
38 // OsAuthenticationToken oat = new OsAuthenticationToken();
39 // Authentication authentication = authenticationManager
40 // .authenticate(oat);
41 // SecurityContextHolder.getContext()
42 // .setAuthentication(authentication);
45 if (args
.length
> 0 && args
[0].equals("help")) {
46 StringBuilder buf
= new StringBuilder();
49 return buf
.toString();
51 List
<URI
> uris
= asURIs(args
);
52 String processUuid
= agent
.process(uris
);
53 agent
.waitFor(processUuid
, timeout
);
58 protected void help(String
[] rawArgs
, StringBuilder buf
) {
59 String
[] args
= Arrays
.copyOfRange(rawArgs
, 1, rawArgs
.length
);
60 if (args
.length
== 0) {// modules
61 for (ExecutionModuleDescriptor emd
: agent
62 .listExecutionModuleDescriptors()) {
63 appendModule(emd
, buf
);
65 } else if (args
.length
== 1 && !args
[0].contains("/")) {// single module
66 NameVersion nameVersion
= new DefaultNameVersion(args
[0]);
67 ExecutionModuleDescriptor emd
= agent
.getExecutionModuleDescriptor(
68 nameVersion
.getName(), nameVersion
.getVersion());
69 appendModule(emd
, buf
);
72 for (ExecutionFlowDescriptor efd
: emd
.getExecutionFlows()) {
73 buf
.append(" ").append(efd
.getName());
74 if (efd
.getDescription() != null
75 && !efd
.getDescription().trim().equals(""))
76 buf
.append(" : ").append(" ").append(efd
.getDescription());
81 List
<URI
> uris
= asURIs(args
);
82 for (URI uri
: uris
) {
83 appendUriHelp(uri
, buf
);
88 protected void appendUriHelp(URI uri
, StringBuilder buf
) {
89 String
[] path
= uri
.getPath().split("/");
90 NameVersion nameVersion
= new DefaultNameVersion(path
[1]);
91 ExecutionModuleDescriptor emd
= agent
.getExecutionModuleDescriptor(
92 nameVersion
.getName(), nameVersion
.getVersion());
94 StringBuilder flow
= new StringBuilder();
95 for (int i
= 2; i
< path
.length
; i
++)
96 flow
.append('/').append(path
[i
]);
97 String flowPath
= flow
.toString();
98 ExecutionFlowDescriptor efd
= findExecutionFlowDescriptor(emd
, flowPath
);
100 throw new SlcException("Flow " + uri
+ " not found");
102 appendModule(emd
, buf
);
104 buf
.append(" ").append(efd
.getName());
105 if (efd
.getDescription() != null
106 && !efd
.getDescription().trim().equals(""))
107 buf
.append(" : ").append(" ").append(efd
.getDescription());
109 Map
<String
, Object
> values
= DefaultAgent
.getQueryMap(uri
.getQuery());
110 ExecutionSpec spec
= efd
.getExecutionSpec();
111 for (String attrKey
: spec
.getAttributes().keySet()) {
112 ExecutionSpecAttribute esa
= spec
.getAttributes().get(attrKey
);
113 buf
.append(" --").append(attrKey
);
114 if (values
.containsKey(attrKey
))
115 buf
.append(" ").append(values
.get(attrKey
));
116 if (esa
.getValue() != null)
117 buf
.append(" (").append(esa
.getValue()).append(')');
122 private void appendModule(ExecutionModuleDescriptor emd
, StringBuilder buf
) {
123 buf
.append("# ").append(emd
.getName());
124 if (emd
.getDescription() != null
125 && !emd
.getDescription().trim().equals(""))
126 buf
.append(" : ").append(emd
.getDescription());
127 if (emd
.getVersion() != null)
128 buf
.append(" (v").append(emd
.getVersion()).append(")");
132 public static List
<URI
> asURIs(String
[] args
) {
134 List
<URI
> uris
= new ArrayList
<URI
>();
135 List
<String
> leftOvers
= new ArrayList
<String
>();
137 Boolean hasArgs
= false;
138 String currKey
= null;
139 StringBuilder currUri
= null;
140 Iterator
<String
> argIt
= Arrays
.asList(args
).iterator();
141 while (argIt
.hasNext()) {
142 String arg
= argIt
.next();
143 if (!arg
.startsWith("-")) {
144 if (currKey
!= null) {// value
145 currUri
.append(URLEncoder
.encode(arg
, UTF8
));
148 if (currUri
!= null) {
149 uris
.add(new URI(currUri
.toString()));
151 currUri
= new StringBuilder("flow:");
153 String currModule
= arg
;
154 currUri
.append('/').append(currModule
);
155 if (!arg
.contains("/")) {
156 // flow path not in arg go to next arg
157 if (!argIt
.hasNext())
158 throw new SlcException("No flow found");
159 String currFlow
= argIt
.next();
160 if (!currFlow
.startsWith("/"))
161 currFlow
= "/" + currFlow
;
162 currUri
.append(currFlow
);
166 if (currUri
== null) {// first args
170 if (arg
.startsWith("--"))
171 key
= arg
.substring(2);
172 else if (arg
.startsWith("-"))
173 key
= arg
.substring(1);
175 throw new SlcException("Cannot intepret key: "
186 // deal with boolean keys
187 if (currKey
!= null) {// value
188 currUri
.append(URLEncoder
.encode("true", UTF8
));
193 currUri
.append(URLEncoder
.encode(key
, UTF8
))
199 uris
.add(new URI(currUri
.toString()));
201 } catch (Exception e
) {
202 throw new SlcException("Cannot convert " + Arrays
.toString(args
)
203 + " to flow URI", e
);
207 private ExecutionFlowDescriptor
findExecutionFlowDescriptor(
208 ExecutionModuleDescriptor emd
, String flowPath
) {
209 ExecutionFlowDescriptor flowDescriptor
= null;
210 for (ExecutionFlowDescriptor efd
: emd
.getExecutionFlows()) {
211 String name
= efd
.getName();
212 // normalize name as flow path
213 if (!name
.startsWith("/"))
215 if (name
.endsWith("/"))
216 name
= name
.substring(0, name
.length() - 1);
217 if (name
.equals(flowPath
)) {
218 flowDescriptor
= efd
;
222 return flowDescriptor
;
225 public void setAgent(SlcAgent agent
) {
229 // public void setAuthenticationManager(
230 // AuthenticationManager authenticationManager) {
231 // this.authenticationManager = authenticationManager;
234 public void setTimeout(Long timeout
) {
235 this.timeout
= timeout
;