]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
2df18bf24c5597af383b4fd18881f44b4ad4d126
[gpl/argeo-slc.git] / runtime / org.argeo.slc.launcher / src / main / java / org / argeo / slc / cli / SlcMain.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.slc.cli;
17
18 import java.io.FileInputStream;
19 import java.util.Properties;
20
21 import org.apache.commons.cli.CommandLine;
22 import org.apache.commons.cli.CommandLineParser;
23 import org.apache.commons.cli.GnuParser;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Option;
26 import org.apache.commons.cli.OptionBuilder;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.apache.commons.io.IOUtils;
30 import org.argeo.slc.SlcException;
31
32 @SuppressWarnings("static-access")
33 public class SlcMain {
34 public enum Type {
35 standalone, agent, server
36 }
37
38 private static Boolean debug = true;
39
40 // private final static String BOOTSTRAP_LOG4J_CONFIG =
41 // "org/argeo/slc/cli/bootstrapLog4j.properties";
42 // private final static String DEFAULT_AGENT_CONTEXT =
43 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
44
45 private final static Option typeOpt = OptionBuilder.withLongOpt("mode")
46 .withArgName("mode").hasArg()
47 .withDescription("Execution type, one of: " + listTypeValues())
48 .create('t');
49
50 private final static Option propertyOpt = OptionBuilder
51 .withLongOpt("property").withArgName("prop1=val1,prop2=val2")
52 .hasArgs().withValueSeparator(',')
53 .withDescription("use value for given property").create('p');
54
55 private final static Option propertiesOpt = OptionBuilder
56 .withLongOpt("properties").withArgName("properties file").hasArgs()
57 .withValueSeparator(',')
58 .withDescription("load properties from file (-p has priority)")
59 .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
70 .withLongOpt("runtime").withArgName("runtime").hasArg()
71 .withDescription("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 }
173
174 public static void printUsage() {
175 new HelpFormatter().printHelp(commandName, options, true);
176 }
177
178 private static String listTypeValues() {
179 StringBuffer buf = new StringBuffer("");
180 for (Type mode : Type.values()) {
181 buf.append(mode).append(", ");
182 }
183 String str = buf.toString();
184 // unsafe, but there will be at least one value in the enum
185 return str.substring(0, str.length() - 2);
186 }
187
188 protected static void addProperty(Properties properties, String property) {
189 int eqIndex = property.indexOf('=');
190 if (eqIndex == 0)
191 throw new SlcException("Badly formatted property " + property);
192
193 if (eqIndex > 0) {
194 String key = property.substring(0, eqIndex);
195 String value = property.substring(eqIndex + 1);
196 properties.setProperty(key, value);
197
198 } else {
199 properties.setProperty(property, "true");
200 }
201 }
202
203 protected static void loadPropertyFile(Properties properties,
204 String propertyFile) {
205 FileInputStream in = null;
206 try {
207 in = new FileInputStream(propertyFile);
208 properties.load(in);
209 } catch (Exception e) {
210 throw new SlcException("Could not load proeprty file "
211 + propertyFile);
212 } finally {
213 IOUtils.closeQuietly(in);
214 }
215 }
216
217 private static void badExit() {
218 printUsage();
219 System.exit(1);
220 }
221
222 protected static void info(Object msg) {
223 System.out.println(msg);
224 }
225
226 protected static void debug(Object msg) {
227 System.out.println(msg);
228 }
229 }