X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.osgi.boot%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fboot%2FOsgiBuilder.java;h=8c460e1161e6cdba0cfe86b59594003e382a64d4;hb=25a31ea46e5de6ce0de366fdb999588c6788c540;hp=81315cf4ede73dc542fa389330f3cb3ac7b21c06;hpb=2af62c95bf8ba23905d703dcb0610bc8025d067a;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 81315cf4e..8c460e116 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() { @@ -56,12 +63,15 @@ public class OsgiBuilder { OsgiBootUtils.debug("OSGi starting - data: " + osgiData + " conf: " + osgiConf); OsgiBoot osgiBoot = new OsgiBoot(framework.getBundleContext()); - // install bundles - for (String distributionBundle : distributionBundles) { - List bundleUrls = osgiBoot.getDistributionUrls(distributionBundle, baseUrl); - osgiBoot.installUrls(bundleUrls); + if (distributionBundles.isEmpty()) { + osgiBoot.getProvisioningManager().install(null); + } else { + // install bundles + for (String distributionBundle : distributionBundles) { + List bundleUrls = osgiBoot.getDistributionUrls(distributionBundle, baseUrl); + osgiBoot.installUrls(bundleUrls); + } } - // start bundles osgiBoot.startBundles(startLevelsToProperties()); @@ -97,6 +107,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 +139,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); } @@ -168,24 +211,55 @@ public class OsgiBuilder { 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) {