]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
8c63c9bb80a2578a2fa290526cf4ed8213182ccd
[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.File;
19 import java.io.FileInputStream;
20 import java.util.List;
21 import java.util.Properties;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.CommandLineParser;
25 import org.apache.commons.cli.GnuParser;
26 import org.apache.commons.cli.HelpFormatter;
27 import org.apache.commons.cli.Option;
28 import org.apache.commons.cli.OptionBuilder;
29 import org.apache.commons.cli.Options;
30 import org.apache.commons.cli.ParseException;
31 import org.apache.commons.io.IOUtils;
32 import org.argeo.osgi.boot.OsgiBoot;
33 import org.argeo.slc.SlcException;
34 import org.argeo.slc.execution.ExecutionModulesManager;
35 import org.eclipse.core.runtime.adaptor.EclipseStarter;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.ServiceReference;
39
40 @SuppressWarnings("static-access")
41 public class SlcMain {
42 public enum Type {
43 standalone, agent, server
44 }
45
46 private static Boolean debug = true;
47
48 // private final static String BOOTSTRAP_LOG4J_CONFIG =
49 // "org/argeo/slc/cli/bootstrapLog4j.properties";
50 // private final static String DEFAULT_AGENT_CONTEXT =
51 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
52
53 private final static Option typeOpt = OptionBuilder.withLongOpt("mode")
54 .withArgName("mode").hasArg()
55 .withDescription("Execution type, one of: " + listTypeValues())
56 .create('t');
57
58 private final static Option propertyOpt = OptionBuilder
59 .withLongOpt("property").withArgName("prop1=val1,prop2=val2")
60 .hasArgs().withValueSeparator(',')
61 .withDescription("use value for given property").create('p');
62
63 private final static Option propertiesOpt = OptionBuilder
64 .withLongOpt("properties").withArgName("properties file").hasArgs()
65 .withValueSeparator(',')
66 .withDescription("load properties from file (-p has priority)")
67 .create('P');
68
69 private final static Option moduleOpt = OptionBuilder.withLongOpt("module")
70 .withArgName("module").hasArg().withDescription("Execution module")
71 .create('m');
72
73 private final static Option flowsOpt = OptionBuilder.withLongOpt("flows")
74 .withArgName("flows").hasArg().withDescription("Flows to execute")
75 .create('f');
76
77 private final static Option runtimeOpt = OptionBuilder
78 .withLongOpt("runtime").withArgName("runtime").hasArg()
79 .withDescription("Runtime URL").create('r');
80
81 private final static Options options;
82
83 private final static String commandName = "slc";
84
85 private static String bundlesToInstall = "/usr/share/osgi;in=*.jar";
86
87 private static String bundlesToStart = "org.springframework.osgi.extender,"
88 + "org.argeo.node.repofactory.jackrabbit,"
89 + "org.argeo.node.repo.jackrabbit," + "org.argeo.security.dao.os,"
90 + "org.argeo.slc.node.jackrabbit," + "org.argeo.slc.agent,"
91 + "org.argeo.slc.agent.jcr";
92
93 static {
94 options = new Options();
95 // options.addOption(typeOpt);
96 // options.addOption(moduleOpt);
97 // options.addOption(flowsOpt);
98 // options.addOption(propertyOpt);
99 // options.addOption(propertiesOpt);
100 // options.addOption(runtimeOpt);
101 }
102
103 public static void main(String[] args) {
104 // Type type = null;
105 // Properties properties = new Properties();
106 // String flows = null;
107 // String urlStr = null;
108
109 String module = null;
110 String moduleUrl = null;
111 String flow = null;
112
113 try {
114
115 CommandLineParser clParser = new GnuParser();
116 CommandLine cl = clParser.parse(options, args);
117
118 List<String> arguments = cl.getArgList();
119 if (arguments.size() == 0) {
120 // TODO default behaviour
121 } else {
122 module = arguments.get(0);
123 File moduleFile = new File(module);
124 if (moduleFile.exists()) {
125 if (moduleFile.isDirectory()) {
126 moduleUrl = "reference:file:"
127 + moduleFile.getCanonicalPath();
128 } else {
129 moduleUrl = "file:" + moduleFile.getCanonicalPath();
130 }
131 }
132
133 if (arguments.size() == 1) {
134 // TODO module info
135 } else {
136 flow = arguments.get(1);
137 }
138 }
139
140 // System.setProperty(
141 // ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY,
142 // module);
143 // System.setProperty(
144 // ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY, flow);
145
146 String executionDir = System.getProperty("user.dir");
147 File slcDir = new File(executionDir, ".slc");
148 File dataDir = new File(slcDir, "data");
149 if (!dataDir.exists())
150 dataDir.mkdirs();
151 File confDir = new File(slcDir, "conf");
152 if (!confDir.exists())
153 confDir.mkdirs();
154
155 BundleContext bundleContext = null;
156 try {
157 String[] osgiRuntimeArgs = { "-configuration",
158 confDir.getCanonicalPath(), "-data",
159 dataDir.getCanonicalPath(), "-console", "-clean" };
160 bundleContext = EclipseStarter.startup(osgiRuntimeArgs, null);
161 } catch (Exception e) {
162 throw new RuntimeException("Cannot start Equinox.", e);
163 }
164
165 // OSGi bootstrap
166 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
167 osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall));
168 osgiBoot.startBundles(bundlesToStart);
169
170 if (moduleUrl != null) {
171 Bundle bundle = osgiBoot.installUrl(moduleUrl);
172 module = bundle.getSymbolicName();
173 // TODO deal with version
174 }
175
176 // retrieve modulesManager
177 ServiceReference sr = bundleContext
178 .getServiceReference(ExecutionModulesManager.class
179 .getName());
180 ExecutionModulesManager modulesManager = (ExecutionModulesManager) bundleContext
181 .getService(sr);
182
183
184 modulesManager.execute(null);
185
186 // osgiBoot.bootstrap();
187 // osgiBoot.bootstrap();
188
189 // Mode
190 // String typeStr = cl.getOptionValue(typeOpt.getOpt());
191 // if (typeStr == null) {
192 // type = Type.standalone;
193 // } else {
194 // try {
195 // type = Type.valueOf(typeStr);
196 // } catch (IllegalArgumentException e) {
197 // throw new SlcException("Unrecognized mode '" + typeStr
198 // + "'", e);
199 // }
200 // }
201 //
202 // // Script
203 // if (type.equals(Type.standalone)) {
204 // if (!cl.hasOption(moduleOpt.getOpt()))
205 // throw new SlcException("Type " + Type.standalone
206 // + " requires option '" + moduleOpt.getLongOpt()
207 // + "'");
208 // module = cl.getOptionValue(moduleOpt.getOpt());
209 //
210 // // Targets
211 // if (cl.hasOption(flowsOpt.getOpt()))
212 // flows = cl.getOptionValue(flowsOpt.getOpt());
213 // }
214 //
215 // // Properties
216 // if (cl.hasOption(propertiesOpt.getOpt())) {
217 // for (String propertyFile : cl.getOptionValues(propertiesOpt
218 // .getOpt())) {
219 // loadPropertyFile(properties, propertyFile);
220 // }
221 // }
222 // if (cl.hasOption(propertyOpt.getOpt())) {
223 // for (String property : cl.getOptionValues(propertyOpt.getOpt()))
224 // {
225 // addProperty(properties, property);
226 // }
227 // }
228 //
229 // // Runtime
230 // if (cl.hasOption(runtimeOpt.getOpt())) {
231 // urlStr = cl.getOptionValue(runtimeOpt.getOpt());
232 // }
233 } catch (ParseException e) {
234 System.err.println("Problem with command line arguments. "
235 + e.getMessage());
236 badExit();
237 } catch (SlcException e) {
238 System.err.println(e.getMessage());
239 badExit();
240 } catch (Exception e) {
241 System.err.println("Unexpected exception when bootstrapping.");
242 e.printStackTrace();
243 badExit();
244 }
245
246 // if (debug) {
247 // debug("Mode: " + type);
248 // if (urlStr != null)
249 // debug("Runtime: " + urlStr);
250 // debug("User properties: " + properties);
251 // if (module != null)
252 // debug("Module: " + module);
253 // if (flows != null)
254 // debug("Flows: " + flows);
255 // }
256 //
257 // // Standalone
258 // if (type.equals(Type.standalone)) {
259 // }
260 // // Agent
261 // else if (type.equals(Type.agent)) {
262 // }
263 }
264
265 public static void printUsage() {
266 new HelpFormatter().printHelp(commandName, options, true);
267 }
268
269 private static String listTypeValues() {
270 StringBuffer buf = new StringBuffer("");
271 for (Type mode : Type.values()) {
272 buf.append(mode).append(", ");
273 }
274 String str = buf.toString();
275 // unsafe, but there will be at least one value in the enum
276 return str.substring(0, str.length() - 2);
277 }
278
279 protected static void addProperty(Properties properties, String property) {
280 int eqIndex = property.indexOf('=');
281 if (eqIndex == 0)
282 throw new SlcException("Badly formatted property " + property);
283
284 if (eqIndex > 0) {
285 String key = property.substring(0, eqIndex);
286 String value = property.substring(eqIndex + 1);
287 properties.setProperty(key, value);
288
289 } else {
290 properties.setProperty(property, "true");
291 }
292 }
293
294 protected static void loadPropertyFile(Properties properties,
295 String propertyFile) {
296 FileInputStream in = null;
297 try {
298 in = new FileInputStream(propertyFile);
299 properties.load(in);
300 } catch (Exception e) {
301 throw new SlcException("Could not load proeprty file "
302 + propertyFile);
303 } finally {
304 IOUtils.closeQuietly(in);
305 }
306 }
307
308 private static void badExit() {
309 printUsage();
310 System.exit(1);
311 }
312
313 protected static void info(Object msg) {
314 System.out.println(msg);
315 }
316
317 protected static void debug(Object msg) {
318 System.out.println(msg);
319 }
320 }