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
.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
;
23 * Authenticates thread and executes synchronously a command line execution.
24 * Reference implementation of args to URIs algorithm.
26 public class DefaultAgentCli
implements SlcAgentCli
{
27 private final static CmsLog log
= CmsLog
.getLog(DefaultAgentCli
.class);
29 private final static String UTF8
= "UTF-8";
30 private SlcAgent agent
;
31 // private AuthenticationManager authenticationManager;
33 private Long timeout
= 24 * 60 * 60 * 1000l;
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);
44 if (args
.length
> 0 && args
[0].equals("help")) {
45 StringBuilder buf
= new StringBuilder();
48 return buf
.toString();
50 List
<URI
> uris
= asURIs(args
);
51 String processUuid
= agent
.process(uris
);
52 agent
.waitFor(processUuid
, timeout
);
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
);
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
);
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());
80 List
<URI
> uris
= asURIs(args
);
81 for (URI uri
: uris
) {
82 appendUriHelp(uri
, buf
);
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());
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
);
99 throw new SlcException("Flow " + uri
+ " not found");
101 appendModule(emd
, buf
);
103 buf
.append(" ").append(efd
.getName());
104 if (efd
.getDescription() != null
105 && !efd
.getDescription().trim().equals(""))
106 buf
.append(" : ").append(" ").append(efd
.getDescription());
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(')');
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(")");
131 public static List
<URI
> asURIs(String
[] args
) {
133 List
<URI
> uris
= new ArrayList
<URI
>();
134 List
<String
> leftOvers
= new ArrayList
<String
>();
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
));
147 if (currUri
!= null) {
148 uris
.add(new URI(currUri
.toString()));
150 currUri
= new StringBuilder("flow:");
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
);
165 if (currUri
== null) {// first args
169 if (arg
.startsWith("--"))
170 key
= arg
.substring(2);
171 else if (arg
.startsWith("-"))
172 key
= arg
.substring(1);
174 throw new SlcException("Cannot intepret key: "
185 // deal with boolean keys
186 if (currKey
!= null) {// value
187 currUri
.append(URLEncoder
.encode("true", UTF8
));
192 currUri
.append(URLEncoder
.encode(key
, UTF8
))
198 uris
.add(new URI(currUri
.toString()));
200 } catch (Exception e
) {
201 throw new SlcException("Cannot convert " + Arrays
.toString(args
)
202 + " to flow URI", e
);
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("/"))
214 if (name
.endsWith("/"))
215 name
= name
.substring(0, name
.length() - 1);
216 if (name
.equals(flowPath
)) {
217 flowDescriptor
= efd
;
221 return flowDescriptor
;
224 public void setAgent(SlcAgent agent
) {
228 // public void setAuthenticationManager(
229 // AuthenticationManager authenticationManager) {
230 // this.authenticationManager = authenticationManager;
233 public void setTimeout(Long timeout
) {
234 this.timeout
= timeout
;