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