X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=osgi%2Fruntime%2Forg.argeo.osgi.boot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fosgi%2Fboot%2FLauncher.java;fp=osgi%2Fruntime%2Forg.argeo.osgi.boot%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fosgi%2Fboot%2FLauncher.java;h=98d7503e31b2f9f6ad0cba21244e137800bc785d;hb=1b452a434924d7628c7b2eace3c5df8e62cdea1c;hp=0000000000000000000000000000000000000000;hpb=4432ffb9f9b4e4d23b2515bde9872910ff8e4e36;p=lgpl%2Fargeo-commons.git diff --git a/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/Launcher.java b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/Launcher.java new file mode 100644 index 000000000..98d7503e3 --- /dev/null +++ b/osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/Launcher.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2010 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.osgi.boot; + +import java.io.FileInputStream; +import java.io.IOException; +import java.lang.reflect.Method; +import java.util.List; +import java.util.Vector; + +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.osgi.framework.BundleContext; + +public class Launcher { + + public static void main(String[] args) { + // Try to load system properties + String systemPropertiesFilePath = OsgiBootUtils.getPropertyCompat( + OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE, + OsgiBoot.PROP_SLC_OSGIBOOT_SYSTEM_PROPERTIES_FILE); + if (systemPropertiesFilePath != null) { + FileInputStream in; + try { + in = new FileInputStream(systemPropertiesFilePath); + System.getProperties().load(in); + } catch (IOException e1) { + throw new RuntimeException( + "Cannot load system properties from " + + systemPropertiesFilePath, e1); + } + if (in != null) { + try { + in.close(); + } catch (Exception e) { + // silent + } + } + } + + // Start main class + startMainClass(); + + // Start Equinox + BundleContext bundleContext = null; + try { + bundleContext = EclipseStarter.startup(args, null); + } catch (Exception e) { + throw new RuntimeException("Cannot start Equinox.", e); + } + + // OSGi bootstrap + OsgiBoot osgiBoot = new OsgiBoot(bundleContext); + osgiBoot.bootstrap(); + } + + protected static void startMainClass() { + // Properties config = System.getProperties(); + // String className = config.getProperty("slc.osgiboot.appclass"); + String className = OsgiBootUtils.getPropertyCompat( + OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS, + OsgiBoot.PROP_SLC_OSGIBOOT_APPCLASS); + if (className == null) + return; + + // should use OsgiBootUtils.getPropertyCompat(), but it does not + // work for "" as default value + // so no warning displayed if PROP_SLC_OSGIBOOT_APPARGS is used + // FIXME: change OsgiBootUtils.getPropertyCompat() + String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS, + System.getProperty(OsgiBoot.PROP_SLC_OSGIBOOT_APPARGS, "")); + + String[] uiArgs = readArgumentsFromLine(line); + + try { + // Launch main method using reflection + Class clss = Class.forName(className); + Class[] mainArgsClasses = new Class[] { uiArgs.getClass() }; + Object[] mainArgs = { uiArgs }; + Method mainMethod = clss.getMethod("main", mainArgsClasses); + mainMethod.invoke(null, mainArgs); + } catch (Exception e) { + throw new RuntimeException("Cannot start main class.", e); + } + + } + + /** + * Transform a line into an array of arguments, taking "" as single + * arguments. (nested \" are not supported) + */ + private static String[] readArgumentsFromLine(String lineOrig) { + + String line = lineOrig.trim();// remove trailing spaces + // System.out.println("line=" + line); + List args = new Vector(); + StringBuffer curr = new StringBuffer(""); + boolean inQuote = false; + char[] arr = line.toCharArray(); + for (int i = 0; i < arr.length; i++) { + char c = arr[i]; + switch (c) { + case '\"': + inQuote = !inQuote; + break; + case ' ': + if (!inQuote) {// otherwise, no break: goes to default + if (curr.length() > 0) { + args.add(curr.toString()); + curr = new StringBuffer(""); + } + break; + } + default: + curr.append(c); + break; + } + } + + // Add last arg + if (curr.length() > 0) { + args.add(curr.toString()); + curr = null; + } + + String[] res = new String[args.size()]; + for (int i = 0; i < args.size(); i++) { + res[i] = args.get(i).toString(); + // System.out.println("res[i]=" + res[i]); + } + return res; + } + +}