1 package org
.argeo
.slc
.cli
;
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
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.slc
.core
.SlcException
;
17 import org
.argeo
.slc
.logging
.Log4jUtils
;
18 import org
.springframework
.core
.io
.DefaultResourceLoader
;
19 import org
.springframework
.core
.io
.FileSystemResource
;
20 import org
.springframework
.core
.io
.Resource
;
22 public class SlcMain
{
27 private static Log log
= null;
29 private final static String BOOTSTRAP_LOG4J_CONFIG
= "org/argeo/slc/cli/bootstrapLog4j.properties";
31 private final static Option modeOpt
= OptionBuilder
.withLongOpt("mode")
32 .withArgName("mode").hasArg().isRequired().withDescription(
33 "SLC execution mode, one of: " + listModeValues()).create(
36 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
37 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
38 .withValueSeparator(',').withDescription(
39 "use value for given property").create('p');
41 private final static Option scriptOpt
= OptionBuilder
.withLongOpt("script")
42 .withArgName("script").hasArg().withDescription(
43 "SLC script to execute").create('s');
45 private final static Option targetsOpt
= OptionBuilder
.withLongOpt(
46 "targets").withArgName("targets").hasArg().withDescription(
47 "Targets to execute").create('t');
49 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
50 "runtime").withArgName("runtime").hasArg().withDescription(
51 "Runtime to use, either a full path or relative to slc app conf dir: "
52 + "<conf dir>/runtime/<runtime>/.xml").create('r');
54 private final static Options options
;
56 private final static String commandName
= "slc";
59 options
= new Options();
60 options
.addOption(modeOpt
);
61 options
.addOption(scriptOpt
);
62 options
.addOption(targetsOpt
);
63 options
.addOption(propertyOpt
);
64 options
.addOption(runtimeOpt
);
67 public static void main(String
[] args
) {
69 Properties properties
= new Properties();
71 String targets
= null;
72 String runtimeStr
= null;
76 CommandLineParser clParser
= new GnuParser();
77 CommandLine cl
= clParser
.parse(options
, args
);
80 String modeStr
= cl
.getOptionValue(modeOpt
.getOpt());
82 mode
= Mode
.valueOf(modeStr
);
83 } catch (IllegalArgumentException e
) {
84 throw new SlcException("Unrecognized mode '" + modeStr
+ "'", e
);
88 if (mode
.equals(Mode
.single
)) {
89 if (!cl
.hasOption(scriptOpt
.getOpt()))
90 throw new SlcException("Mode " + Mode
.single
91 + " requires option '" + scriptOpt
.getLongOpt()
93 script
= cl
.getOptionValue(scriptOpt
.getOpt());
96 if (cl
.hasOption(targetsOpt
.getOpt()))
97 targets
= cl
.getOptionValue(targetsOpt
.getOpt());
101 if (cl
.hasOption(propertyOpt
.getOpt())) {
102 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
103 addProperty(properties
, property
);
108 if (cl
.hasOption(runtimeOpt
.getOpt())) {
109 runtimeStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
111 runtimeStr
= "default";
114 } catch (ParseException e
) {
115 System
.err
.println("Problem with command line arguments. "
118 } catch (SlcException e
) {
119 System
.err
.println(e
.getMessage());
121 } catch (Exception e
) {
122 System
.err
.println("Unexpected exception when bootstrapping.");
127 // Initializes logging and log arguments
128 initLogging(properties
);
129 if (log
.isDebugEnabled()) {
130 log
.debug("Mode: " + mode
);
131 log
.debug("Runtime: " + runtimeStr
);
132 log
.debug("User properties: " + properties
);
134 log
.debug("Script: " + script
);
136 log
.debug("Targets: " + targets
);
140 if (mode
.equals(Mode
.single
)) {
142 if (new File(script
).exists()) {
143 scriptRes
= new FileSystemResource(script
);
145 scriptRes
= new DefaultResourceLoader(SlcMain
.class
146 .getClassLoader()).getResource(script
);
149 DefaultSlcRuntime runtime
= new DefaultSlcRuntime();
150 runtime
.executeScript(runtimeStr
, scriptRes
, targets
, properties
,
155 public static void printUsage() {
156 new HelpFormatter().printHelp(commandName
, options
, true);
159 private static String
listModeValues() {
160 StringBuffer buf
= new StringBuffer("");
161 for (Mode mode
: Mode
.values()) {
162 buf
.append(mode
).append(", ");
164 String str
= buf
.toString();
165 // unsafe, but there will be at least one value in the enum
166 return str
.substring(0, str
.length() - 2);
169 private static void addProperty(Properties properties
, String property
) {
170 int eqIndex
= property
.indexOf('=');
172 throw new SlcException("Badly formatted property " + property
);
175 String key
= property
.substring(0, eqIndex
);
176 String value
= property
.substring(eqIndex
+ 1);
177 properties
.setProperty(key
, value
);
180 properties
.setProperty(property
, "true");
185 private static void initLogging(Properties userProperties
) {
186 System
.setProperty("log4j.defaultInitOverride", "true");
188 // Add log4j user properties to System properties
189 for (String key
: userProperties
.stringPropertyNames()) {
190 if (key
.startsWith("log4j.")) {
191 System
.setProperty(key
, userProperties
.getProperty(key
));
194 Log4jUtils
.initLog4j(System
.getProperty("log4j.configuration",
195 "classpath:" + BOOTSTRAP_LOG4J_CONFIG
));
196 log
= LogFactory
.getLog(SlcMain
.class);
200 private static void badExit() {