1 package org
.argeo
.slc
.cli
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.FileNotFoundException
;
5 import java
.io
.IOException
;
6 import java
.util
.Properties
;
8 import org
.apache
.commons
.cli
.CommandLine
;
9 import org
.apache
.commons
.cli
.CommandLineParser
;
10 import org
.apache
.commons
.cli
.GnuParser
;
11 import org
.apache
.commons
.cli
.HelpFormatter
;
12 import org
.apache
.commons
.cli
.Option
;
13 import org
.apache
.commons
.cli
.OptionBuilder
;
14 import org
.apache
.commons
.cli
.Options
;
15 import org
.apache
.commons
.cli
.ParseException
;
16 import org
.apache
.commons
.io
.IOUtils
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.slc
.ant
.AntConstants
;
20 import org
.argeo
.slc
.core
.SlcException
;
21 import org
.argeo
.slc
.logging
.Log4jUtils
;
23 public class SlcMain
{
28 private static Log log
= null;
30 private final static String BOOTSTRAP_LOG4J_CONFIG
= "org/argeo/slc/cli/bootstrapLog4j.properties";
32 private final static Option modeOpt
= OptionBuilder
.withLongOpt("mode")
33 .withArgName("mode").hasArg().withDescription(
34 "SLC execution mode, one of: " + listModeValues()).create(
37 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
38 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
39 .withValueSeparator(',').withDescription(
40 "use value for given property").create('p');
42 private final static Option propertiesOpt
= OptionBuilder
.withLongOpt(
43 "properties").withArgName("properties file").hasArgs()
44 .withValueSeparator(',').withDescription(
45 "load properties from file (-p has priority)").create('P');
47 private final static Option scriptOpt
= OptionBuilder
.withLongOpt("script")
48 .withArgName("script").hasArg().withDescription(
49 "SLC script to execute").create('s');
51 private final static Option targetsOpt
= OptionBuilder
.withLongOpt(
52 "targets").withArgName("targets").hasArg().withDescription(
53 "Targets to execute").create('t');
55 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
56 "runtime").withArgName("runtime").hasArg().withDescription(
57 "Runtime to use, either a full path or relative to slc app conf dir: "
58 + "<conf dir>/runtime/<runtime>/.xml").create('r');
60 private final static Options options
;
62 private final static String commandName
= "slc";
65 options
= new Options();
66 options
.addOption(modeOpt
);
67 options
.addOption(scriptOpt
);
68 options
.addOption(targetsOpt
);
69 options
.addOption(propertyOpt
);
70 options
.addOption(propertiesOpt
);
71 options
.addOption(runtimeOpt
);
74 public static void main(String
[] args
) {
76 Properties properties
= new Properties();
78 String targets
= null;
79 String runtimeStr
= null;
83 CommandLineParser clParser
= new GnuParser();
84 CommandLine cl
= clParser
.parse(options
, args
);
87 String modeStr
= cl
.getOptionValue(modeOpt
.getOpt());
88 if (modeStr
== null) {
92 mode
= Mode
.valueOf(modeStr
);
93 } catch (IllegalArgumentException e
) {
94 throw new SlcException("Unrecognized mode '" + modeStr
100 if (mode
.equals(Mode
.single
)) {
101 if (!cl
.hasOption(scriptOpt
.getOpt()))
102 throw new SlcException("Mode " + Mode
.single
103 + " requires option '" + scriptOpt
.getLongOpt()
105 script
= cl
.getOptionValue(scriptOpt
.getOpt());
108 if (cl
.hasOption(targetsOpt
.getOpt()))
109 targets
= cl
.getOptionValue(targetsOpt
.getOpt());
113 if (cl
.hasOption(propertiesOpt
.getOpt())) {
114 for (String propertyFile
: cl
.getOptionValues(propertiesOpt
116 loadPropertyFile(properties
, propertyFile
);
119 if (cl
.hasOption(propertyOpt
.getOpt())) {
120 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
121 addProperty(properties
, property
);
126 if (cl
.hasOption(runtimeOpt
.getOpt())) {
127 runtimeStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
129 } catch (ParseException e
) {
130 System
.err
.println("Problem with command line arguments. "
133 } catch (SlcException e
) {
134 System
.err
.println(e
.getMessage());
136 } catch (Exception e
) {
137 System
.err
.println("Unexpected exception when bootstrapping.");
142 // Initializes logging and log arguments
143 initLogging(properties
);
144 if (log
.isDebugEnabled()) {
145 log
.debug("Mode: " + mode
);
146 if (runtimeStr
!= null)
147 log
.debug("Runtime: " + runtimeStr
);
148 log
.debug("User properties: " + properties
);
150 log
.debug("Script: " + script
);
152 log
.debug("Targets: " + targets
);
156 if (mode
.equals(Mode
.single
)) {
158 DefaultSlcRuntime runtime
= new DefaultSlcRuntime();
159 runtime
.executeScript(runtimeStr
, script
, targets
, properties
,
162 } catch (SlcException e
) {
163 log
.error("SLC client terminated with an error: ", e
);
169 public static void printUsage() {
170 new HelpFormatter().printHelp(commandName
, options
, true);
173 private static String
listModeValues() {
174 StringBuffer buf
= new StringBuffer("");
175 for (Mode mode
: Mode
.values()) {
176 buf
.append(mode
).append(", ");
178 String str
= buf
.toString();
179 // unsafe, but there will be at least one value in the enum
180 return str
.substring(0, str
.length() - 2);
183 protected static void addProperty(Properties properties
, String property
) {
184 int eqIndex
= property
.indexOf('=');
186 throw new SlcException("Badly formatted property " + property
);
189 String key
= property
.substring(0, eqIndex
);
190 String value
= property
.substring(eqIndex
+ 1);
191 properties
.setProperty(key
, value
);
194 properties
.setProperty(property
, "true");
198 protected static void loadPropertyFile(Properties properties
,
199 String propertyFile
) {
200 FileInputStream in
= null;
202 in
= new FileInputStream(propertyFile
);
204 } catch (Exception e
) {
205 throw new SlcException("Could not load proeprty file "
208 IOUtils
.closeQuietly(in
);
212 private static void initLogging(Properties userProperties
) {
213 System
.setProperty("log4j.defaultInitOverride", "true");
215 // Add log4j user properties to System properties
216 for (Object obj
: userProperties
.keySet()) {
217 String key
= obj
.toString();
218 if (key
.startsWith("log4j.")) {
219 System
.setProperty(key
, userProperties
.getProperty(key
));
222 Log4jUtils
.initLog4j(System
.getProperty("log4j.configuration",
223 "classpath:" + BOOTSTRAP_LOG4J_CONFIG
));
224 log
= LogFactory
.getLog(SlcMain
.class);
228 private static void badExit() {