]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcMain.java
2a7aab55c081f0f9de56b94520553f853692f723
[gpl/argeo-slc.git] / SlcMain.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.cli;
18
19 import java.io.FileInputStream;
20 import java.util.Properties;
21
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.CommandLineParser;
24 import org.apache.commons.cli.GnuParser;
25 import org.apache.commons.cli.HelpFormatter;
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.OptionBuilder;
28 import org.apache.commons.cli.Options;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.commons.io.IOUtils;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.server.client.impl.SlcServerHttpClientImpl;
33
34 @SuppressWarnings("static-access")
35 public class SlcMain {
36 public enum Type {
37 standalone, agent, server
38 }
39
40 private static Boolean debug = true;
41
42 // private final static String BOOTSTRAP_LOG4J_CONFIG =
43 // "org/argeo/slc/cli/bootstrapLog4j.properties";
44 // private final static String DEFAULT_AGENT_CONTEXT =
45 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
46
47 private final static Option typeOpt = OptionBuilder.withLongOpt("mode")
48 .withArgName("mode").hasArg().withDescription(
49 "Execution type, one of: " + listTypeValues()).create('t');
50
51 private final static Option propertyOpt = OptionBuilder.withLongOpt(
52 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
53 .withValueSeparator(',').withDescription(
54 "use value for given property").create('p');
55
56 private final static Option propertiesOpt = OptionBuilder.withLongOpt(
57 "properties").withArgName("properties file").hasArgs()
58 .withValueSeparator(',').withDescription(
59 "load properties from file (-p has priority)").create('P');
60
61 private final static Option moduleOpt = OptionBuilder.withLongOpt("module")
62 .withArgName("module").hasArg().withDescription("Execution module")
63 .create('m');
64
65 private final static Option flowsOpt = OptionBuilder.withLongOpt("flows")
66 .withArgName("flows").hasArg().withDescription("Flows to execute")
67 .create('f');
68
69 private final static Option runtimeOpt = OptionBuilder.withLongOpt(
70 "runtime").withArgName("runtime").hasArg().withDescription(
71 "Runtime URL").create('r');
72
73 private final static Options options;
74
75 private final static String commandName = "slc";
76
77 static {
78 options = new Options();
79 options.addOption(typeOpt);
80 options.addOption(moduleOpt);
81 options.addOption(flowsOpt);
82 options.addOption(propertyOpt);
83 options.addOption(propertiesOpt);
84 options.addOption(runtimeOpt);
85 }
86
87 public static void main(String[] args) {
88 Type type = null;
89 Properties properties = new Properties();
90 String module = null;
91 String flows = null;
92 String urlStr = null;
93
94 try {
95
96 CommandLineParser clParser = new GnuParser();
97 CommandLine cl = clParser.parse(options, args);
98
99 // Mode
100 String typeStr = cl.getOptionValue(typeOpt.getOpt());
101 if (typeStr == null) {
102 type = Type.standalone;
103 } else {
104 try {
105 type = Type.valueOf(typeStr);
106 } catch (IllegalArgumentException e) {
107 throw new SlcException("Unrecognized mode '" + typeStr
108 + "'", e);
109 }
110 }
111
112 // Script
113 if (type.equals(Type.standalone)) {
114 if (!cl.hasOption(moduleOpt.getOpt()))
115 throw new SlcException("Type " + Type.standalone
116 + " requires option '" + moduleOpt.getLongOpt()
117 + "'");
118 module = cl.getOptionValue(moduleOpt.getOpt());
119
120 // Targets
121 if (cl.hasOption(flowsOpt.getOpt()))
122 flows = cl.getOptionValue(flowsOpt.getOpt());
123 }
124
125 // Properties
126 if (cl.hasOption(propertiesOpt.getOpt())) {
127 for (String propertyFile : cl.getOptionValues(propertiesOpt
128 .getOpt())) {
129 loadPropertyFile(properties, propertyFile);
130 }
131 }
132 if (cl.hasOption(propertyOpt.getOpt())) {
133 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
134 addProperty(properties, property);
135 }
136 }
137
138 // Runtime
139 if (cl.hasOption(runtimeOpt.getOpt())) {
140 urlStr = cl.getOptionValue(runtimeOpt.getOpt());
141 }
142 } catch (ParseException e) {
143 System.err.println("Problem with command line arguments. "
144 + e.getMessage());
145 badExit();
146 } catch (SlcException e) {
147 System.err.println(e.getMessage());
148 badExit();
149 } catch (Exception e) {
150 System.err.println("Unexpected exception when bootstrapping.");
151 e.printStackTrace();
152 badExit();
153 }
154
155 if (debug) {
156 debug("Mode: " + type);
157 if (urlStr != null)
158 debug("Runtime: " + urlStr);
159 debug("User properties: " + properties);
160 if (module != null)
161 debug("Module: " + module);
162 if (flows != null)
163 debug("Flows: " + flows);
164 }
165
166 // Standalone
167 if (type.equals(Type.standalone)) {
168 }
169 // Agent
170 else if (type.equals(Type.agent)) {
171 }
172 // Server
173 else if (type.equals(Type.server)) {
174 SlcServerHttpClientImpl slcServerHttpClient = new SlcServerHttpClientImpl();
175 slcServerHttpClient.setBaseUrl(urlStr);
176 }
177 }
178
179 public static void printUsage() {
180 new HelpFormatter().printHelp(commandName, options, true);
181 }
182
183 private static String listTypeValues() {
184 StringBuffer buf = new StringBuffer("");
185 for (Type mode : Type.values()) {
186 buf.append(mode).append(", ");
187 }
188 String str = buf.toString();
189 // unsafe, but there will be at least one value in the enum
190 return str.substring(0, str.length() - 2);
191 }
192
193 protected static void addProperty(Properties properties, String property) {
194 int eqIndex = property.indexOf('=');
195 if (eqIndex == 0)
196 throw new SlcException("Badly formatted property " + property);
197
198 if (eqIndex > 0) {
199 String key = property.substring(0, eqIndex);
200 String value = property.substring(eqIndex + 1);
201 properties.setProperty(key, value);
202
203 } else {
204 properties.setProperty(property, "true");
205 }
206 }
207
208 protected static void loadPropertyFile(Properties properties,
209 String propertyFile) {
210 FileInputStream in = null;
211 try {
212 in = new FileInputStream(propertyFile);
213 properties.load(in);
214 } catch (Exception e) {
215 throw new SlcException("Could not load proeprty file "
216 + propertyFile);
217 } finally {
218 IOUtils.closeQuietly(in);
219 }
220 }
221
222 private static void badExit() {
223 printUsage();
224 System.exit(1);
225 }
226
227 protected static void info(Object msg) {
228 System.out.println(msg);
229 }
230
231 protected static void debug(Object msg) {
232 System.out.println(msg);
233 }
234 }