X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.launcher%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcli%2FSlcMain.java;h=b74d5f0949d178866345cfc4644274a3c36f4b52;hb=736b4bfbc05c56916453cfc248082182caf1c211;hp=2a7aab55c081f0f9de56b94520553f853692f723;hpb=1fdb1b4e7b1d2b0cabb6483238301b857a6392fa;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java index 2a7aab55c..b74d5f094 100644 --- a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java +++ b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010 Mathieu Baudier + * Copyright (C) 2007-2012 Argeo GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,132 +13,124 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.argeo.slc.cli; +import java.io.File; import java.io.FileInputStream; +import java.util.List; import java.util.Properties; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.GnuParser; import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.io.IOUtils; +import org.argeo.osgi.boot.OsgiBoot; import org.argeo.slc.SlcException; -import org.argeo.slc.server.client.impl.SlcServerHttpClientImpl; +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; -@SuppressWarnings("static-access") +@SuppressWarnings("restriction") public class SlcMain { - public enum Type { - standalone, agent, server - } - - private static Boolean debug = true; - - // private final static String BOOTSTRAP_LOG4J_CONFIG = - // "org/argeo/slc/cli/bootstrapLog4j.properties"; - // private final static String DEFAULT_AGENT_CONTEXT = - // "classpath:org/argeo/slc/cli/spring-agent-default.xml"; - - private final static Option typeOpt = OptionBuilder.withLongOpt("mode") - .withArgName("mode").hasArg().withDescription( - "Execution type, one of: " + listTypeValues()).create('t'); - - private final static Option propertyOpt = OptionBuilder.withLongOpt( - "property").withArgName("prop1=val1,prop2=val2").hasArgs() - .withValueSeparator(',').withDescription( - "use value for given property").create('p'); - - private final static Option propertiesOpt = OptionBuilder.withLongOpt( - "properties").withArgName("properties file").hasArgs() - .withValueSeparator(',').withDescription( - "load properties from file (-p has priority)").create('P'); - - private final static Option moduleOpt = OptionBuilder.withLongOpt("module") - .withArgName("module").hasArg().withDescription("Execution module") - .create('m'); + /** Unique launch module */ + public static String UNIQUE_LAUNCH_MODULE_PROPERTY = "slc.launch.module"; - private final static Option flowsOpt = OptionBuilder.withLongOpt("flows") - .withArgName("flows").hasArg().withDescription("Flows to execute") - .create('f'); - - private final static Option runtimeOpt = OptionBuilder.withLongOpt( - "runtime").withArgName("runtime").hasArg().withDescription( - "Runtime URL").create('r'); + /** Unique launch flow */ + public static String UNIQUE_LAUNCH_FLOW_PROPERTY = "slc.launch.flow"; private final static Options options; private final static String commandName = "slc"; + // private static String bundlesToInstall = "/usr/share/osgi;in=*.jar"; + private static String bundlesToInstall = System.getProperty("user.home") + + "/dev/src/slc/runtime/org.argeo.slc.launcher/target/dependency;in=*.jar"; + + // private static String bundlesToStart = + // "org.springframework.osgi.extender," + // + "org.argeo.node.repofactory.jackrabbit," + // + "org.argeo.node.repo.jackrabbit," + "org.argeo.security.dao.os," + // + "org.argeo.slc.node.jackrabbit," + "org.argeo.slc.agent," + // + "org.argeo.slc.agent.jcr"; + private static String bundlesToStart = "org.springframework.osgi.extender," + + "org.argeo.slc.agent"; + static { options = new Options(); - options.addOption(typeOpt); - options.addOption(moduleOpt); - options.addOption(flowsOpt); - options.addOption(propertyOpt); - options.addOption(propertiesOpt); - options.addOption(runtimeOpt); } + @SuppressWarnings({ "unchecked" }) public static void main(String[] args) { - Type type = null; - Properties properties = new Properties(); String module = null; - String flows = null; - String urlStr = null; + String moduleUrl = null; + String flow = null; try { CommandLineParser clParser = new GnuParser(); CommandLine cl = clParser.parse(options, args); - // Mode - String typeStr = cl.getOptionValue(typeOpt.getOpt()); - if (typeStr == null) { - type = Type.standalone; + List arguments = cl.getArgList(); + if (arguments.size() == 0) { + // TODO default behaviour } else { - try { - type = Type.valueOf(typeStr); - } catch (IllegalArgumentException e) { - throw new SlcException("Unrecognized mode '" + typeStr - + "'", e); + module = arguments.get(0); + File moduleFile = new File(module); + if (moduleFile.exists()) { + if (moduleFile.isDirectory()) { + moduleUrl = "reference:file:" + + moduleFile.getCanonicalPath(); + } else { + moduleUrl = "file:" + moduleFile.getCanonicalPath(); + } } - } - // Script - if (type.equals(Type.standalone)) { - if (!cl.hasOption(moduleOpt.getOpt())) - throw new SlcException("Type " + Type.standalone - + " requires option '" + moduleOpt.getLongOpt() - + "'"); - module = cl.getOptionValue(moduleOpt.getOpt()); - - // Targets - if (cl.hasOption(flowsOpt.getOpt())) - flows = cl.getOptionValue(flowsOpt.getOpt()); - } - - // Properties - if (cl.hasOption(propertiesOpt.getOpt())) { - for (String propertyFile : cl.getOptionValues(propertiesOpt - .getOpt())) { - loadPropertyFile(properties, propertyFile); + if (arguments.size() == 1) { + // TODO module info + } else { + flow = arguments.get(1); } } - if (cl.hasOption(propertyOpt.getOpt())) { - for (String property : cl.getOptionValues(propertyOpt.getOpt())) { - addProperty(properties, property); - } + + String executionDir = System.getProperty("user.dir"); + File slcDir = new File(executionDir, "target/.slc"); + File dataDir = new File(slcDir, "data"); + if (!dataDir.exists()) + dataDir.mkdirs(); + File confDir = new File(slcDir, "conf"); + if (!confDir.exists()) + confDir.mkdirs(); + + BundleContext bundleContext = null; + try { + String[] osgiRuntimeArgs = { "-configuration", + confDir.getCanonicalPath(), "-data", + dataDir.getCanonicalPath(), "-console", "-clean" }; + bundleContext = EclipseStarter.startup(osgiRuntimeArgs, null); + } catch (Exception e) { + throw new RuntimeException("Cannot start Equinox.", e); } - // Runtime - if (cl.hasOption(runtimeOpt.getOpt())) { - urlStr = cl.getOptionValue(runtimeOpt.getOpt()); + // OSGi bootstrap + OsgiBoot osgiBoot = new OsgiBoot(bundleContext); + osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall)); + + if (moduleUrl != null) { + Bundle bundle = osgiBoot.installUrl(moduleUrl); + module = bundle.getSymbolicName(); + // TODO deal with version } + + System.setProperty(UNIQUE_LAUNCH_MODULE_PROPERTY, module); + System.setProperty(UNIQUE_LAUNCH_FLOW_PROPERTY, flow); + System.setProperty("log4j.configuration", "file:./log4j.properties"); + + // start runtime + osgiBoot.startBundles(bundlesToStart); + } catch (ParseException e) { System.err.println("Problem with command line arguments. " + e.getMessage()); @@ -151,45 +143,12 @@ public class SlcMain { e.printStackTrace(); badExit(); } - - if (debug) { - debug("Mode: " + type); - if (urlStr != null) - debug("Runtime: " + urlStr); - debug("User properties: " + properties); - if (module != null) - debug("Module: " + module); - if (flows != null) - debug("Flows: " + flows); - } - - // Standalone - if (type.equals(Type.standalone)) { - } - // Agent - else if (type.equals(Type.agent)) { - } - // Server - else if (type.equals(Type.server)) { - SlcServerHttpClientImpl slcServerHttpClient = new SlcServerHttpClientImpl(); - slcServerHttpClient.setBaseUrl(urlStr); - } } public static void printUsage() { new HelpFormatter().printHelp(commandName, options, true); } - private static String listTypeValues() { - StringBuffer buf = new StringBuffer(""); - for (Type mode : Type.values()) { - buf.append(mode).append(", "); - } - String str = buf.toString(); - // unsafe, but there will be at least one value in the enum - return str.substring(0, str.length() - 2); - } - protected static void addProperty(Properties properties, String property) { int eqIndex = property.indexOf('='); if (eqIndex == 0)