From: Mathieu Baudier Date: Sat, 3 Feb 2018 22:03:13 +0000 (+0100) Subject: Improve scripting X-Git-Tag: argeo-commons-2.1.71~30 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=0e141e75188e84fde6ac573e24405f1825e66f0a Improve scripting --- diff --git a/demo/argeo_node.js b/demo/argeo_node.js index 4dc012b76..e2af03d17 100755 --- a/demo/argeo_node.js +++ b/demo/argeo_node.js @@ -1,16 +1,25 @@ #!/home/mbaudier/dev/git/apache2/argeo-commons/dist/osgi-boot/src/main/rpm/usr/bin/a2jjs - // demo specific var app = "node"; var demoHome = $ENV.HOME + "/dev/git/apache2/argeo-commons/demo"; var appHome = demoHome + "/exec/argeo_node.js"; var appConf = demoHome; var policyFile = "all.policy"; + +// CMS config load("../dist/argeo-node/rpm/usr/share/node/jjs/cms.js"); -var distribution = "org.argeo.commons:org.argeo.dep.cms.platform:2.1.71-SNAPSHOT"; -osgi.install(distribution); -osgi.conf("org.osgi.service.http.port", "7070"); +osgi.baseUrl = "http://forge.argeo.org/data/java/argeo-2.1/"; +osgi.install("org.argeo.commons:org.argeo.dep.cms.platform:2.1.70"); +osgi.conf("org.osgi.service.http.port", 0); // osgi.conf("osgi.clean", true); osgi.launch(); + +// wait for UI +osgi.spring("org.argeo.cms.ui.workbench.rap"); +var appUrl = "http://localhost:" + osgi.httpPort + "/ui/node"; +$EXEC("/usr/bin/chromium-browser --app=" + appUrl); + +// shutdown when the windows is closed +osgi.shutdown(); \ No newline at end of file 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 7a8fd96bf..3dfe486d0 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 @@ -11,17 +11,26 @@ import java.util.Set; import java.util.TreeMap; import org.eclipse.osgi.launch.EquinoxFactory; +import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; +import org.osgi.framework.BundleEvent; import org.osgi.framework.BundleException; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; import org.osgi.framework.launch.Framework; import org.osgi.framework.launch.FrameworkFactory; +import org.osgi.util.tracker.BundleTracker; +import org.osgi.util.tracker.ServiceTracker; +/** OSGi builder, focusing on ease of use for scripting. */ public class OsgiBuilder { private Map startLevels = new TreeMap<>(); private List distributionBundles = new ArrayList<>(); private Map configuration = new HashMap(); private Framework framework; + private String baseUrl = null; public OsgiBuilder() { // configuration.put("osgi.clean", "true"); @@ -49,7 +58,7 @@ public class OsgiBuilder { OsgiBoot osgiBoot = new OsgiBoot(framework.getBundleContext()); // install bundles for (String distributionBundle : distributionBundles) { - List bundleUrls = osgiBoot.getDistributionUrls(distributionBundle, null); + List bundleUrls = osgiBoot.getDistributionUrls(distributionBundle, baseUrl); osgiBoot.installUrls(bundleUrls); } @@ -88,11 +97,109 @@ public class OsgiBuilder { return this; } + public OsgiBuilder waitForBundle(String bundles) { + List lst = new ArrayList<>(); + Collections.addAll(lst, bundles.split(",")); + BundleTracker bt = new BundleTracker(getBc(), Bundle.ACTIVE, null) { + + @Override + public Object addingBundle(Bundle bundle, BundleEvent event) { + if (lst.contains(bundle.getSymbolicName())) { + return bundle.getSymbolicName(); + } else { + return null; + } + } + }; + bt.open(); + while (bt.getTrackingCount() != lst.size()) { + try { + Thread.sleep(500l); + } catch (InterruptedException e) { + break; + } + } + bt.close(); + return this; + + } + + public Object service(String service) { + return service(service, 0); + } + + public Object service(String service, long timeout) { + ServiceTracker st; + if (service.contains("(")) { + try { + st = new ServiceTracker<>(getBc(), FrameworkUtil.createFilter(service), null); + } catch (InvalidSyntaxException e) { + throw new IllegalArgumentException("Badly formatted filter", e); + } + } else { + st = new ServiceTracker<>(getBc(), service, null); + } + st.open(); + try { + return st.waitForService(timeout); + } catch (InterruptedException e) { + OsgiBootUtils.error("Interrupted", e); + return null; + } finally { + st.close(); + } + + } + + public void shutdown() { + checkLaunched(); + try { + framework.stop(); + } catch (BundleException e) { + e.printStackTrace(); + System.exit(1); + } + System.exit(0); + } + + 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()); + } + + 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()); + } + + public Object spring(String bundle) { + return service("(&(Bundle-SymbolicName=" + bundle + ")" + + "(objectClass=org.springframework.context.ApplicationContext))"); + } + + // + // BEAN + // + public BundleContext getBc() { checkLaunched(); return framework.getBundleContext(); } + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + // // UTILITIES //