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
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.slc
.SlcException
;
18 import org
.argeo
.slc
.runtime
.SlcExecutionContext
;
19 import org
.argeo
.slc
.runtime
.SlcRuntime
;
20 import org
.springframework
.context
.ConfigurableApplicationContext
;
21 import org
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
22 import org
.springframework
.context
.support
.FileSystemXmlApplicationContext
;
24 public class SlcMain
{
29 private static Log log
= null;
31 private final static String BOOTSTRAP_LOG4J_CONFIG
= "org/argeo/slc/cli/bootstrapLog4j.properties";
32 private final static String DEFAULT_AGENT_CONTEXT
= "classpath:org/argeo/slc/cli/spring-agent-default.xml";
34 private final static Option modeOpt
= OptionBuilder
.withLongOpt("mode")
35 .withArgName("mode").hasArg().withDescription(
36 "SLC execution mode, one of: " + listModeValues()).create(
39 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
40 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
41 .withValueSeparator(',').withDescription(
42 "use value for given property").create('p');
44 private final static Option propertiesOpt
= OptionBuilder
.withLongOpt(
45 "properties").withArgName("properties file").hasArgs()
46 .withValueSeparator(',').withDescription(
47 "load properties from file (-p has priority)").create('P');
49 private final static Option scriptOpt
= OptionBuilder
.withLongOpt("script")
50 .withArgName("script").hasArg().withDescription(
51 "SLC script to execute").create('s');
53 private final static Option targetsOpt
= OptionBuilder
.withLongOpt(
54 "targets").withArgName("targets").hasArg().withDescription(
55 "Targets to execute").create('t');
57 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
58 "runtime").withArgName("runtime").hasArg().withDescription(
59 "Runtime to use, either a full path or relative to slc app conf dir: "
60 + "<conf dir>/runtime/<runtime>/.xml").create('r');
62 private final static Options options
;
64 private final static String commandName
= "slc";
67 options
= new Options();
68 options
.addOption(modeOpt
);
69 options
.addOption(scriptOpt
);
70 options
.addOption(targetsOpt
);
71 options
.addOption(propertyOpt
);
72 options
.addOption(propertiesOpt
);
73 options
.addOption(runtimeOpt
);
76 public static void main(String
[] args
) {
78 Properties properties
= new Properties();
80 String targets
= null;
81 String runtimeStr
= null;
85 CommandLineParser clParser
= new GnuParser();
86 CommandLine cl
= clParser
.parse(options
, args
);
89 String modeStr
= cl
.getOptionValue(modeOpt
.getOpt());
90 if (modeStr
== null) {
94 mode
= Mode
.valueOf(modeStr
);
95 } catch (IllegalArgumentException e
) {
96 throw new SlcException("Unrecognized mode '" + modeStr
102 if (mode
.equals(Mode
.single
)) {
103 if (!cl
.hasOption(scriptOpt
.getOpt()))
104 throw new SlcException("Mode " + Mode
.single
105 + " requires option '" + scriptOpt
.getLongOpt()
107 script
= cl
.getOptionValue(scriptOpt
.getOpt());
110 if (cl
.hasOption(targetsOpt
.getOpt()))
111 targets
= cl
.getOptionValue(targetsOpt
.getOpt());
115 if (cl
.hasOption(propertiesOpt
.getOpt())) {
116 for (String propertyFile
: cl
.getOptionValues(propertiesOpt
118 loadPropertyFile(properties
, propertyFile
);
121 if (cl
.hasOption(propertyOpt
.getOpt())) {
122 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
123 addProperty(properties
, property
);
128 if (cl
.hasOption(runtimeOpt
.getOpt())) {
129 runtimeStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
131 } catch (ParseException e
) {
132 System
.err
.println("Problem with command line arguments. "
135 } catch (SlcException e
) {
136 System
.err
.println(e
.getMessage());
138 } catch (Exception e
) {
139 System
.err
.println("Unexpected exception when bootstrapping.");
144 // Initializes logging and log arguments
145 initLogging(properties
);
146 if (log
.isDebugEnabled()) {
147 log
.debug("Mode: " + mode
);
148 if (runtimeStr
!= null)
149 log
.debug("Runtime: " + runtimeStr
);
150 log
.debug("User properties: " + properties
);
152 log
.debug("Script: " + script
);
154 log
.debug("Targets: " + targets
);
158 if (mode
.equals(Mode
.single
)) {
160 // DefaultSlcRuntime runtime = new DefaultSlcRuntime();
161 // FIXME: inject this more cleanly
162 ClassLoader cl
= Thread
.currentThread().getContextClassLoader();
163 Class clss
= cl
.loadClass("org.argeo.slc.ant.AntSlcRuntime");
164 SlcRuntime
<?
extends SlcExecutionContext
> runtime
= (SlcRuntime
<?
extends SlcExecutionContext
>) clss
166 runtime
.executeScript(runtimeStr
, script
, targets
, properties
,
169 } catch (Exception e
) {
170 log
.error("SLC client terminated with an error: ", e
);
175 else if (mode
.equals(Mode
.agent
)) {
176 final ConfigurableApplicationContext applicationContext
;
177 if (runtimeStr
== null) {
178 applicationContext
= new ClassPathXmlApplicationContext(
179 DEFAULT_AGENT_CONTEXT
);
181 applicationContext
= new FileSystemXmlApplicationContext(
184 applicationContext
.registerShutdownHook();
185 applicationContext
.start();
186 log
.info("SLC Agent context started.");
189 else if (mode
.equals(Mode
.osgi
)) {
193 public static void printUsage() {
194 new HelpFormatter().printHelp(commandName
, options
, true);
197 private static String
listModeValues() {
198 StringBuffer buf
= new StringBuffer("");
199 for (Mode mode
: Mode
.values()) {
200 buf
.append(mode
).append(", ");
202 String str
= buf
.toString();
203 // unsafe, but there will be at least one value in the enum
204 return str
.substring(0, str
.length() - 2);
207 protected static void addProperty(Properties properties
, String property
) {
208 int eqIndex
= property
.indexOf('=');
210 throw new SlcException("Badly formatted property " + property
);
213 String key
= property
.substring(0, eqIndex
);
214 String value
= property
.substring(eqIndex
+ 1);
215 properties
.setProperty(key
, value
);
218 properties
.setProperty(property
, "true");
222 protected static void loadPropertyFile(Properties properties
,
223 String propertyFile
) {
224 FileInputStream in
= null;
226 in
= new FileInputStream(propertyFile
);
228 } catch (Exception e
) {
229 throw new SlcException("Could not load proeprty file "
232 IOUtils
.closeQuietly(in
);
236 private static void initLogging(Properties userProperties
) {
237 System
.setProperty("log4j.defaultInitOverride", "true");
239 // Add log4j user properties to System properties
240 for (Object obj
: userProperties
.keySet()) {
241 String key
= obj
.toString();
242 if (key
.startsWith("log4j.")) {
243 System
.setProperty(key
, userProperties
.getProperty(key
));
246 Log4jUtils
.initLog4j(System
.getProperty("log4j.configuration",
247 "classpath:" + BOOTSTRAP_LOG4J_CONFIG
));
248 log
= LogFactory
.getLog(SlcMain
.class);
252 private static void badExit() {