1 package org
.argeo
.slc
.cli
;
3 import java
.util
.Properties
;
5 import org
.apache
.commons
.cli
.CommandLine
;
6 import org
.apache
.commons
.cli
.CommandLineParser
;
7 import org
.apache
.commons
.cli
.GnuParser
;
8 import org
.apache
.commons
.cli
.HelpFormatter
;
9 import org
.apache
.commons
.cli
.Option
;
10 import org
.apache
.commons
.cli
.OptionBuilder
;
11 import org
.apache
.commons
.cli
.Options
;
12 import org
.apache
.commons
.cli
.ParseException
;
13 import org
.apache
.commons
.logging
.Log
;
14 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.core
.SlcException
;
16 import org
.argeo
.slc
.logging
.Log4jUtils
;
18 public class SlcMain
{
23 private static Log log
= null;
25 private final static String BOOTSTRAP_LOG4J_CONFIG
= "org/argeo/slc/cli/bootstrapLog4j.properties";
27 private final static Option modeOpt
= OptionBuilder
.withLongOpt("mode")
28 .withArgName("mode").hasArg().isRequired().withDescription(
29 "SLC execution mode, one of: " + listModeValues()).create(
32 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
33 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
34 .withValueSeparator(',').withDescription(
35 "use value for given property").create('p');
37 private final static Option scriptOpt
= OptionBuilder
.withLongOpt("script")
38 .withArgName("script").hasArg().withDescription(
39 "SLC script to execute").create('s');
41 private final static Option targetsOpt
= OptionBuilder
.withLongOpt(
42 "targets").withArgName("targets").hasArg().withDescription(
43 "Targets to execute").create('t');
45 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
46 "runtime").withArgName("runtime").hasArg().withDescription(
47 "Runtime to use, either a full path or relative to slc app conf dir: "
48 + "<conf dir>/runtime/<runtime>/.xml").create('r');
50 private final static Options options
;
52 private final static String commandName
= "slc";
55 options
= new Options();
56 options
.addOption(modeOpt
);
57 options
.addOption(scriptOpt
);
58 options
.addOption(targetsOpt
);
59 options
.addOption(propertyOpt
);
60 options
.addOption(runtimeOpt
);
63 public static void main(String
[] args
) {
65 Properties properties
= new Properties();
67 String targets
= null;
68 String runtimeStr
= null;
72 CommandLineParser clParser
= new GnuParser();
73 CommandLine cl
= clParser
.parse(options
, args
);
76 String modeStr
= cl
.getOptionValue(modeOpt
.getOpt());
78 mode
= Mode
.valueOf(modeStr
);
79 } catch (IllegalArgumentException e
) {
80 throw new SlcException("Unrecognized mode '" + modeStr
+ "'", e
);
84 if (mode
.equals(Mode
.single
)) {
85 if (!cl
.hasOption(scriptOpt
.getOpt()))
86 throw new SlcException("Mode " + Mode
.single
87 + " requires option '" + scriptOpt
.getLongOpt()
89 script
= cl
.getOptionValue(scriptOpt
.getOpt());
92 if (cl
.hasOption(targetsOpt
.getOpt()))
93 targets
= cl
.getOptionValue(targetsOpt
.getOpt());
97 if (cl
.hasOption(propertyOpt
.getOpt())) {
98 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
99 addProperty(properties
, property
);
104 if (cl
.hasOption(runtimeOpt
.getOpt())) {
105 runtimeStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
107 runtimeStr
= "default";
110 } catch (ParseException e
) {
111 System
.err
.println("Problem with command line arguments. "
114 } catch (SlcException e
) {
115 System
.err
.println(e
.getMessage());
117 } catch (Exception e
) {
118 System
.err
.println("Unexpected exception when bootstrapping.");
123 // Initializes logging and log arguments
124 initLogging(properties
);
125 if (log
.isDebugEnabled()) {
126 log
.debug("Mode: " + mode
);
127 log
.debug("Runtime: " + runtimeStr
);
128 log
.debug("User properties: " + properties
);
130 log
.debug("Script: " + script
);
132 log
.debug("Targets: " + targets
);
136 if (mode
.equals(Mode
.single
)) {
137 DefaultSlcRuntime runtime
= new DefaultSlcRuntime();
138 runtime
.executeScript(runtimeStr
, script
, targets
, properties
,
143 public static void printUsage() {
144 new HelpFormatter().printHelp(commandName
, options
, true);
147 private static String
listModeValues() {
148 StringBuffer buf
= new StringBuffer("");
149 for (Mode mode
: Mode
.values()) {
150 buf
.append(mode
).append(", ");
152 String str
= buf
.toString();
153 // unsafe, but there will be at least one value in the enum
154 return str
.substring(0, str
.length() - 2);
157 private static void addProperty(Properties properties
, String property
) {
158 int eqIndex
= property
.indexOf('=');
160 throw new SlcException("Badly formatted property " + property
);
163 String key
= property
.substring(0, eqIndex
);
164 String value
= property
.substring(eqIndex
+ 1);
165 properties
.setProperty(key
, value
);
168 properties
.setProperty(property
, "true");
173 private static void initLogging(Properties userProperties
) {
174 System
.setProperty("log4j.defaultInitOverride", "true");
176 // Add log4j user properties to System properties
177 for (String key
: userProperties
.stringPropertyNames()) {
178 if (key
.startsWith("log4j.")) {
179 System
.setProperty(key
, userProperties
.getProperty(key
));
182 Log4jUtils
.initLog4j(System
.getProperty("log4j.configuration",
183 "classpath:" + BOOTSTRAP_LOG4J_CONFIG
));
184 log
= LogFactory
.getLog(SlcMain
.class);
188 private static void badExit() {