X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.osgi.boot%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fboot%2FOsgiBuilder.java;h=28a2604a42c6c3fc6766b81ff5a94370b07d4ae8;hb=1091271b89f2d12e9898e01f6639c48831b1bc4b;hp=3dfe486d094816220b5cf448901a42bc91264849;hpb=0e141e75188e84fde6ac573e24405f1825e66f0a;p=lgpl%2Fargeo-commons.git 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 3dfe486d0..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; @@ -25,6 +27,10 @@ import org.osgi.util.tracker.ServiceTracker; /** OSGi builder, focusing on ease of use for scripting. */ public class OsgiBuilder { + private final static String PROP_HTTP_PORT = "org.osgi.service.http.port"; + private final static String PROP_HTTPS_PORT = "org.osgi.service.https.port"; + private final static String PROP_OSGI_CLEAN = "osgi.clean"; + private Map startLevels = new TreeMap<>(); private List distributionBundles = new ArrayList<>(); @@ -36,6 +42,7 @@ public class OsgiBuilder { // configuration.put("osgi.clean", "true"); configuration.put(OsgiBoot.CONFIGURATION_AREA_PROP, System.getProperty(OsgiBoot.CONFIGURATION_AREA_PROP)); configuration.put(OsgiBoot.INSTANCE_AREA_PROP, System.getProperty(OsgiBoot.INSTANCE_AREA_PROP)); + configuration.put(PROP_OSGI_CLEAN, System.getProperty(PROP_OSGI_CLEAN)); } public Framework launch() { @@ -97,6 +104,11 @@ public class OsgiBuilder { return this; } + public OsgiBuilder waitForServlet(String base) { + service("(&(objectClass=javax.servlet.Servlet)(osgi.http.whiteboard.servlet.pattern=" + base + "))"); + return this; + } + public OsgiBuilder waitForBundle(String bundles) { List lst = new ArrayList<>(); Collections.addAll(lst, bundles.split(",")); @@ -124,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); } @@ -159,27 +199,64 @@ public class OsgiBuilder { e.printStackTrace(); System.exit(1); } + try { + framework.waitForStop(10 * 60 * 1000); + } catch (InterruptedException e) { + e.printStackTrace(); + System.exit(1); + } System.exit(0); } + public void setHttpPort(Integer port) { + checkNotLaunched(); + configuration.put(PROP_HTTP_PORT, Integer.toString(port)); + } + + public void setHttpsPort(Integer port) { + checkNotLaunched(); + configuration.put(PROP_HTTPS_PORT, Integer.toString(port)); + } + + public void setClean(boolean clean) { + checkNotLaunched(); + configuration.put(PROP_OSGI_CLEAN, Boolean.toString(clean)); + } + public Integer getHttpPort() { - ServiceReference sr = getBc().getServiceReference("org.osgi.service.http.HttpService"); - if (sr == null) - return -1; - Object port = sr.getProperty("http.port"); - if (port == null) - return -1; - return Integer.parseInt(port.toString()); + if (!isLaunched()) { + if (configuration.containsKey(PROP_HTTP_PORT)) + return Integer.parseInt(configuration.get(PROP_HTTP_PORT)); + else + return -1; + } else { + // TODO wait for service? + ServiceReference sr = getBc().getServiceReference("org.osgi.service.http.HttpService"); + if (sr == null) + return -1; + Object port = sr.getProperty("http.port"); + if (port == null) + return -1; + return Integer.parseInt(port.toString()); + } } public Integer getHttpsPort() { - ServiceReference sr = getBc().getServiceReference("org.osgi.service.http.HttpService"); - if (sr == null) - return -1; - Object port = sr.getProperty("https.port"); - if (port == null) - return -1; - return Integer.parseInt(port.toString()); + if (!isLaunched()) { + if (configuration.containsKey(PROP_HTTPS_PORT)) + return Integer.parseInt(configuration.get(PROP_HTTPS_PORT)); + else + return -1; + } else { + // TODO wait for service? + ServiceReference sr = getBc().getServiceReference("org.osgi.service.http.HttpService"); + if (sr == null) + return -1; + Object port = sr.getProperty("https.port"); + if (port == null) + return -1; + return Integer.parseInt(port.toString()); + } } public Object spring(String bundle) {