]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Integrate XML-based file exchanges.
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
1 package org.argeo.slc.cli;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.util.Properties;
7
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;
22
23 public class SlcMain {
24 public enum Mode {
25 single, agent
26 }
27
28 private static Log log = null;
29
30 private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
31
32 private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
33 .withArgName("mode").hasArg().withDescription(
34 "SLC execution mode, one of: " + listModeValues()).create(
35 'm');
36
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');
41
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');
46
47 private final static Option scriptOpt = OptionBuilder.withLongOpt("script")
48 .withArgName("script").hasArg().withDescription(
49 "SLC script to execute").create('s');
50
51 private final static Option targetsOpt = OptionBuilder.withLongOpt(
52 "targets").withArgName("targets").hasArg().withDescription(
53 "Targets to execute").create('t');
54
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');
59
60 private final static Options options;
61
62 private final static String commandName = "slc";
63
64 static {
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);
72 }
73
74 public static void main(String[] args) {
75 Mode mode = null;
76 Properties properties = new Properties();
77 String script = null;
78 String targets = null;
79 String runtimeStr = null;
80
81 try {
82
83 CommandLineParser clParser = new GnuParser();
84 CommandLine cl = clParser.parse(options, args);
85
86 // Mode
87 String modeStr = cl.getOptionValue(modeOpt.getOpt());
88 if (modeStr == null) {
89 mode = Mode.single;
90 } else {
91 try {
92 mode = Mode.valueOf(modeStr);
93 } catch (IllegalArgumentException e) {
94 throw new SlcException("Unrecognized mode '" + modeStr
95 + "'", e);
96 }
97 }
98
99 // Script
100 if (mode.equals(Mode.single)) {
101 if (!cl.hasOption(scriptOpt.getOpt()))
102 throw new SlcException("Mode " + Mode.single
103 + " requires option '" + scriptOpt.getLongOpt()
104 + "'");
105 script = cl.getOptionValue(scriptOpt.getOpt());
106
107 // Targets
108 if (cl.hasOption(targetsOpt.getOpt()))
109 targets = cl.getOptionValue(targetsOpt.getOpt());
110 }
111
112 // Properties
113 if (cl.hasOption(propertiesOpt.getOpt())) {
114 for (String propertyFile : cl.getOptionValues(propertiesOpt
115 .getOpt())) {
116 loadPropertyFile(properties, propertyFile);
117 }
118 }
119 if (cl.hasOption(propertyOpt.getOpt())) {
120 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
121 addProperty(properties, property);
122 }
123 }
124
125 // Runtime
126 if (cl.hasOption(runtimeOpt.getOpt())) {
127 runtimeStr = cl.getOptionValue(runtimeOpt.getOpt());
128 }
129 } catch (ParseException e) {
130 System.err.println("Problem with command line arguments. "
131 + e.getMessage());
132 badExit();
133 } catch (SlcException e) {
134 System.err.println(e.getMessage());
135 badExit();
136 } catch (Exception e) {
137 System.err.println("Unexpected exception when bootstrapping.");
138 e.printStackTrace();
139 badExit();
140 }
141
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);
149 if (script != null)
150 log.debug("Script: " + script);
151 if (targets != null)
152 log.debug("Targets: " + targets);
153 }
154
155 // Execution
156 if (mode.equals(Mode.single)) {
157 DefaultSlcRuntime runtime = new DefaultSlcRuntime();
158 runtime.executeScript(runtimeStr, script, targets, properties,
159 null, null);
160 }
161 }
162
163 public static void printUsage() {
164 new HelpFormatter().printHelp(commandName, options, true);
165 }
166
167 private static String listModeValues() {
168 StringBuffer buf = new StringBuffer("");
169 for (Mode mode : Mode.values()) {
170 buf.append(mode).append(", ");
171 }
172 String str = buf.toString();
173 // unsafe, but there will be at least one value in the enum
174 return str.substring(0, str.length() - 2);
175 }
176
177 protected static void addProperty(Properties properties, String property) {
178 int eqIndex = property.indexOf('=');
179 if (eqIndex == 0)
180 throw new SlcException("Badly formatted property " + property);
181
182 if (eqIndex > 0) {
183 String key = property.substring(0, eqIndex);
184 String value = property.substring(eqIndex + 1);
185 properties.setProperty(key, value);
186
187 } else {
188 properties.setProperty(property, "true");
189 }
190 }
191
192 protected static void loadPropertyFile(Properties properties,
193 String propertyFile) {
194 FileInputStream in = null;
195 try {
196 in = new FileInputStream(propertyFile);
197 properties.load(in);
198 } catch (Exception e) {
199 throw new SlcException("Could not load proeprty file "
200 + propertyFile);
201 } finally {
202 IOUtils.closeQuietly(in);
203 }
204 }
205
206 private static void initLogging(Properties userProperties) {
207 System.setProperty("log4j.defaultInitOverride", "true");
208
209 // Add log4j user properties to System properties
210 for (Object obj : userProperties.keySet()) {
211 String key = obj.toString();
212 if (key.startsWith("log4j.")) {
213 System.setProperty(key, userProperties.getProperty(key));
214 }
215 }
216 Log4jUtils.initLog4j(System.getProperty("log4j.configuration",
217 "classpath:" + BOOTSTRAP_LOG4J_CONFIG));
218 log = LogFactory.getLog(SlcMain.class);
219
220 }
221
222 private static void badExit() {
223 printUsage();
224 System.exit(1);
225 }
226 }