1 package org
.argeo
.slc
.cli
;
3 import java
.io
.FileInputStream
;
4 import java
.util
.Properties
;
6 import org
.apache
.commons
.cli
.CommandLine
;
7 import org
.apache
.commons
.cli
.CommandLineParser
;
8 import org
.apache
.commons
.cli
.GnuParser
;
9 import org
.apache
.commons
.cli
.HelpFormatter
;
10 import org
.apache
.commons
.cli
.Option
;
11 import org
.apache
.commons
.cli
.OptionBuilder
;
12 import org
.apache
.commons
.cli
.Options
;
13 import org
.apache
.commons
.cli
.ParseException
;
14 import org
.apache
.commons
.io
.IOUtils
;
15 import org
.argeo
.slc
.SlcException
;
16 import org
.argeo
.slc
.server
.client
.SlcServerHttpClient
;
17 import org
.argeo
.slc
.server
.client
.impl
.SlcServerHttpClientImpl
;
19 public class SlcMain
{
21 standalone
, agent
, server
24 private static Boolean debug
= true;
26 private final static String BOOTSTRAP_LOG4J_CONFIG
= "org/argeo/slc/cli/bootstrapLog4j.properties";
27 private final static String DEFAULT_AGENT_CONTEXT
= "classpath:org/argeo/slc/cli/spring-agent-default.xml";
29 private final static Option typeOpt
= OptionBuilder
.withLongOpt("mode")
30 .withArgName("mode").hasArg().withDescription(
31 "Execution type, one of: " + listTypeValues()).create('t');
33 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
34 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
35 .withValueSeparator(',').withDescription(
36 "use value for given property").create('p');
38 private final static Option propertiesOpt
= OptionBuilder
.withLongOpt(
39 "properties").withArgName("properties file").hasArgs()
40 .withValueSeparator(',').withDescription(
41 "load properties from file (-p has priority)").create('P');
43 private final static Option moduleOpt
= OptionBuilder
.withLongOpt("module")
44 .withArgName("module").hasArg().withDescription("Execution module")
47 private final static Option flowsOpt
= OptionBuilder
.withLongOpt("flows")
48 .withArgName("flows").hasArg().withDescription("Flows to execute")
51 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
52 "runtime").withArgName("runtime").hasArg().withDescription(
53 "Runtime URL").create('r');
55 private final static Options options
;
57 private final static String commandName
= "slc";
60 options
= new Options();
61 options
.addOption(typeOpt
);
62 options
.addOption(moduleOpt
);
63 options
.addOption(flowsOpt
);
64 options
.addOption(propertyOpt
);
65 options
.addOption(propertiesOpt
);
66 options
.addOption(runtimeOpt
);
69 public static void main(String
[] args
) {
71 Properties properties
= new Properties();
78 CommandLineParser clParser
= new GnuParser();
79 CommandLine cl
= clParser
.parse(options
, args
);
82 String typeStr
= cl
.getOptionValue(typeOpt
.getOpt());
83 if (typeStr
== null) {
84 type
= Type
.standalone
;
87 type
= Type
.valueOf(typeStr
);
88 } catch (IllegalArgumentException e
) {
89 throw new SlcException("Unrecognized mode '" + typeStr
95 if (type
.equals(Type
.standalone
)) {
96 if (!cl
.hasOption(moduleOpt
.getOpt()))
97 throw new SlcException("Type " + Type
.standalone
98 + " requires option '" + moduleOpt
.getLongOpt()
100 module
= cl
.getOptionValue(moduleOpt
.getOpt());
103 if (cl
.hasOption(flowsOpt
.getOpt()))
104 flows
= cl
.getOptionValue(flowsOpt
.getOpt());
108 if (cl
.hasOption(propertiesOpt
.getOpt())) {
109 for (String propertyFile
: cl
.getOptionValues(propertiesOpt
111 loadPropertyFile(properties
, propertyFile
);
114 if (cl
.hasOption(propertyOpt
.getOpt())) {
115 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
116 addProperty(properties
, property
);
121 if (cl
.hasOption(runtimeOpt
.getOpt())) {
122 urlStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
124 } catch (ParseException e
) {
125 System
.err
.println("Problem with command line arguments. "
128 } catch (SlcException e
) {
129 System
.err
.println(e
.getMessage());
131 } catch (Exception e
) {
132 System
.err
.println("Unexpected exception when bootstrapping.");
138 debug("Mode: " + type
);
140 debug("Runtime: " + urlStr
);
141 debug("User properties: " + properties
);
143 debug("Module: " + module
);
145 debug("Flows: " + flows
);
149 if (type
.equals(Type
.standalone
)) {
152 else if (type
.equals(Type
.agent
)) {
155 else if (type
.equals(Type
.server
)) {
156 SlcServerHttpClientImpl slcServerHttpClient
= new SlcServerHttpClientImpl();
157 slcServerHttpClient
.setBaseUrl(urlStr
);
161 public static void printUsage() {
162 new HelpFormatter().printHelp(commandName
, options
, true);
165 private static String
listTypeValues() {
166 StringBuffer buf
= new StringBuffer("");
167 for (Type mode
: Type
.values()) {
168 buf
.append(mode
).append(", ");
170 String str
= buf
.toString();
171 // unsafe, but there will be at least one value in the enum
172 return str
.substring(0, str
.length() - 2);
175 protected static void addProperty(Properties properties
, String property
) {
176 int eqIndex
= property
.indexOf('=');
178 throw new SlcException("Badly formatted property " + property
);
181 String key
= property
.substring(0, eqIndex
);
182 String value
= property
.substring(eqIndex
+ 1);
183 properties
.setProperty(key
, value
);
186 properties
.setProperty(property
, "true");
190 protected static void loadPropertyFile(Properties properties
,
191 String propertyFile
) {
192 FileInputStream in
= null;
194 in
= new FileInputStream(propertyFile
);
196 } catch (Exception e
) {
197 throw new SlcException("Could not load proeprty file "
200 IOUtils
.closeQuietly(in
);
204 private static void badExit() {
209 protected static void info(Object msg
) {
210 System
.out
.println(msg
);
213 protected static void debug(Object msg
) {
214 System
.out
.println(msg
);