From: Mathieu Baudier Date: Mon, 9 Apr 2018 07:08:39 +0000 (+0200) Subject: Main commands in OSGi boot X-Git-Tag: argeo-commons-2.1.73~14 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=f9109847d5a1fe8f20014e567f1b816f6d814d5f;p=lgpl%2Fargeo-commons.git Main commands in OSGi boot --- diff --git a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/Main.java b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/Main.java new file mode 100644 index 000000000..fdabd995c --- /dev/null +++ b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/Main.java @@ -0,0 +1,32 @@ +package org.argeo.osgi.boot; + +import java.lang.management.ManagementFactory; + +public class Main { + + public static void main(String[] args) { + String mainClass = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS); + if (mainClass == null) { + throw new IllegalArgumentException( + "System property " + OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS + " must be specified"); + } + + OsgiBuilder osgi = new OsgiBuilder(); + String distributionUrl = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_DISTRIBUTION_URL); + if (distributionUrl != null) + osgi.install(distributionUrl); + // osgi.conf("argeo.node.useradmin.uris", "os:///"); + // osgi.conf("osgi.clean", "true"); + // osgi.conf("osgi.console", "true"); + osgi.launch(); + osgi.main(mainClass, args); + + long jvmUptime = ManagementFactory.getRuntimeMXBean().getUptime(); + String jvmUptimeStr = (jvmUptime / 1000) + "." + (jvmUptime % 1000) + "s"; + System.out.println("Command " + mainClass + " executed in " + jvmUptimeStr); + + osgi.shutdown(); + + } + +} diff --git a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBuilder.java b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBuilder.java index fa4d3790e..28a2604a4 100644 --- a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBuilder.java +++ b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBuilder.java @@ -1,5 +1,7 @@ package org.argeo.osgi.boot; +import java.lang.reflect.Method; +import java.net.URI; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -134,6 +136,34 @@ public class OsgiBuilder { } + public OsgiBuilder main(String clssUri, String[] args) { + + // waitForBundle(bundleSymbolicName); + try { + URI uri = new URI(clssUri); + if (!"bundleclass".equals(uri.getScheme())) + throw new IllegalArgumentException("Unsupported scheme for " + clssUri); + String bundleSymbolicName = uri.getHost(); + String clss = uri.getPath().substring(1); + Bundle bundle = null; + for (Bundle b : getBc().getBundles()) { + if (bundleSymbolicName.equals(b.getSymbolicName())) { + bundle = b; + break; + } + } + if (bundle == null) + throw new OsgiBootException("Bundle " + bundleSymbolicName + " not found"); + Class c = bundle.loadClass(clss); + Object[] mainArgs = { args }; + Method mainMethod = c.getMethod("main", String[].class); + mainMethod.invoke(null, mainArgs); + } catch (Throwable e) { + throw new OsgiBootException("Cannot execute " + clssUri, e); + } + return this; + } + public Object service(String service) { return service(service, 0); }