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=8c63c9bb80a2578a2fa290526cf4ed8213182ccd;hb=645840383404d5962ca2af85c2020c3d66219518;hp=15b42cd743529d444b775e58030dae2c6321f82e;hpb=fbcd3e375c8f4d3ce265297f2006c46b1fe36a6f;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 15b42cd74..8c63c9bb8 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,6 +1,23 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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; @@ -12,119 +29,207 @@ 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.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.slc.core.SlcException; -import org.argeo.slc.logging.Log4jUtils; -import org.argeo.slc.runtime.SlcExecutionContext; -import org.argeo.slc.runtime.SlcRuntime; - +import org.argeo.osgi.boot.OsgiBoot; +import org.argeo.slc.SlcException; +import org.argeo.slc.execution.ExecutionModulesManager; +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +@SuppressWarnings("static-access") public class SlcMain { - public enum Mode { - single, agent + public enum Type { + standalone, agent, server } - private static Log log = null; + private static Boolean debug = true; - private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties"; + // 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 modeOpt = OptionBuilder.withLongOpt("mode") - .withArgName("mode").hasArg().withDescription( - "SLC execution mode, one of: " + listModeValues()).create( - 'm'); + 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 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 propertiesOpt = OptionBuilder + .withLongOpt("properties").withArgName("properties file").hasArgs() + .withValueSeparator(',') + .withDescription("load properties from file (-p has priority)") + .create('P'); - private final static Option scriptOpt = OptionBuilder.withLongOpt("script") - .withArgName("script").hasArg().withDescription( - "SLC script to execute").create('s'); + private final static Option moduleOpt = OptionBuilder.withLongOpt("module") + .withArgName("module").hasArg().withDescription("Execution module") + .create('m'); - private final static Option targetsOpt = OptionBuilder.withLongOpt( - "targets").withArgName("targets").hasArg().withDescription( - "Targets to execute").create('t'); + 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 to use, either a full path or relative to slc app conf dir: " - + "/runtime//.xml").create('r'); + private final static Option runtimeOpt = OptionBuilder + .withLongOpt("runtime").withArgName("runtime").hasArg() + .withDescription("Runtime URL").create('r'); private final static Options options; private final static String commandName = "slc"; + private static String bundlesToInstall = "/usr/share/osgi;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"; + static { options = new Options(); - options.addOption(modeOpt); - options.addOption(scriptOpt); - options.addOption(targetsOpt); - options.addOption(propertyOpt); - options.addOption(propertiesOpt); - options.addOption(runtimeOpt); + // options.addOption(typeOpt); + // options.addOption(moduleOpt); + // options.addOption(flowsOpt); + // options.addOption(propertyOpt); + // options.addOption(propertiesOpt); + // options.addOption(runtimeOpt); } public static void main(String[] args) { - Mode mode = null; - Properties properties = new Properties(); - String script = null; - String targets = null; - String runtimeStr = null; + // Type type = null; + // Properties properties = new Properties(); + // String flows = null; + // String urlStr = null; + + String module = null; + String moduleUrl = null; + String flow = null; try { CommandLineParser clParser = new GnuParser(); CommandLine cl = clParser.parse(options, args); - // Mode - String modeStr = cl.getOptionValue(modeOpt.getOpt()); - if (modeStr == null) { - mode = Mode.single; + List arguments = cl.getArgList(); + if (arguments.size() == 0) { + // TODO default behaviour } else { - try { - mode = Mode.valueOf(modeStr); - } catch (IllegalArgumentException e) { - throw new SlcException("Unrecognized mode '" + modeStr - + "'", 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 (mode.equals(Mode.single)) { - if (!cl.hasOption(scriptOpt.getOpt())) - throw new SlcException("Mode " + Mode.single - + " requires option '" + scriptOpt.getLongOpt() - + "'"); - script = cl.getOptionValue(scriptOpt.getOpt()); - - // Targets - if (cl.hasOption(targetsOpt.getOpt())) - targets = cl.getOptionValue(targetsOpt.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); - } + + // System.setProperty( + // ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY, + // module); + // System.setProperty( + // ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY, flow); + + String executionDir = System.getProperty("user.dir"); + File slcDir = new File(executionDir, ".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())) { - runtimeStr = cl.getOptionValue(runtimeOpt.getOpt()); + // OSGi bootstrap + OsgiBoot osgiBoot = new OsgiBoot(bundleContext); + osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall)); + osgiBoot.startBundles(bundlesToStart); + + if (moduleUrl != null) { + Bundle bundle = osgiBoot.installUrl(moduleUrl); + module = bundle.getSymbolicName(); + // TODO deal with version } + + // retrieve modulesManager + ServiceReference sr = bundleContext + .getServiceReference(ExecutionModulesManager.class + .getName()); + ExecutionModulesManager modulesManager = (ExecutionModulesManager) bundleContext + .getService(sr); + + + modulesManager.execute(null); + + // osgiBoot.bootstrap(); + // osgiBoot.bootstrap(); + + // Mode + // String typeStr = cl.getOptionValue(typeOpt.getOpt()); + // if (typeStr == null) { + // type = Type.standalone; + // } else { + // try { + // type = Type.valueOf(typeStr); + // } catch (IllegalArgumentException e) { + // throw new SlcException("Unrecognized mode '" + typeStr + // + "'", e); + // } + // } + // + // // 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 (cl.hasOption(propertyOpt.getOpt())) { + // for (String property : cl.getOptionValues(propertyOpt.getOpt())) + // { + // addProperty(properties, property); + // } + // } + // + // // Runtime + // if (cl.hasOption(runtimeOpt.getOpt())) { + // urlStr = cl.getOptionValue(runtimeOpt.getOpt()); + // } } catch (ParseException e) { System.err.println("Problem with command line arguments. " + e.getMessage()); @@ -138,45 +243,32 @@ public class SlcMain { badExit(); } - // Initializes logging and log arguments - initLogging(properties); - if (log.isDebugEnabled()) { - log.debug("Mode: " + mode); - if (runtimeStr != null) - log.debug("Runtime: " + runtimeStr); - log.debug("User properties: " + properties); - if (script != null) - log.debug("Script: " + script); - if (targets != null) - log.debug("Targets: " + targets); - } - - // Execution - if (mode.equals(Mode.single)) { - try { - // DefaultSlcRuntime runtime = new DefaultSlcRuntime(); - // FIXME: inject this more cleanly - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - Class clss = cl.loadClass("org.argeo.slc.ant.AntSlcRuntime"); - SlcRuntime runtime = (SlcRuntime) clss - .newInstance(); - runtime.executeScript(runtimeStr, script, targets, properties, - null, null); - // System.exit(0); - } catch (Exception e) { - log.error("SLC client terminated with an error: ", e); - System.exit(1); - } - } + // 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)) { + // } } public static void printUsage() { new HelpFormatter().printHelp(commandName, options, true); } - private static String listModeValues() { + private static String listTypeValues() { StringBuffer buf = new StringBuffer(""); - for (Mode mode : Mode.values()) { + for (Type mode : Type.values()) { buf.append(mode).append(", "); } String str = buf.toString(); @@ -213,24 +305,16 @@ public class SlcMain { } } - private static void initLogging(Properties userProperties) { - System.setProperty("log4j.defaultInitOverride", "true"); - - // Add log4j user properties to System properties - for (Object obj : userProperties.keySet()) { - String key = obj.toString(); - if (key.startsWith("log4j.")) { - System.setProperty(key, userProperties.getProperty(key)); - } - } - Log4jUtils.initLog4j(System.getProperty("log4j.configuration", - "classpath:" + BOOTSTRAP_LOG4J_CONFIG)); - log = LogFactory.getLog(SlcMain.class); - - } - private static void badExit() { printUsage(); System.exit(1); } + + protected static void info(Object msg) { + System.out.println(msg); + } + + protected static void debug(Object msg) { + System.out.println(msg); + } }