public static void main(String[] args) {
// Try to load system properties
- String systemPropertiesFilePath = OsgiBootUtils
- .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE);
+ String systemPropertiesFilePath = getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_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);
+ throw new RuntimeException("Cannot load system properties from " + systemPropertiesFilePath, e1);
}
if (in != null) {
try {
}
protected static void startMainClass() {
- String className = OsgiBootUtils
- .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS);
+ String className = getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS);
if (className == null)
return;
- String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS,
- "");
+ String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS, "");
String[] uiArgs = readArgumentsFromLine(line);
return res;
}
+ public static String getProperty(String name, String defaultValue) {
+ final String value;
+ if (defaultValue != null)
+ value = System.getProperty(name, defaultValue);
+ else
+ value = System.getProperty(name);
+
+ if (value == null || value.equals(""))
+ return null;
+ else
+ return value;
+ }
+
+ public static String getProperty(String name) {
+ return getProperty(name, null);
+ }
+
}
--- /dev/null
+package org.argeo.osgi.boot;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import java.util.ServiceLoader;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.launch.Framework;
+import org.osgi.framework.launch.FrameworkFactory;
+
+/** Launch an OSGi framework and deploy a CMS Node into it. */
+public class NodeRunner {
+ private Long timeout = 30 * 1000l;
+ private final Path baseDir;
+ private final Path confDir;
+ private final Path dataDir;
+
+ private String baseUrl = "http://forge.argeo.org/data/java/argeo-2.1/";
+ private String distributionUrl = null;
+
+ private Framework framework = null;
+
+ public NodeRunner(String distributionUrl, Path baseDir) {
+ this.distributionUrl = distributionUrl;
+ Path mavenBase = Paths.get(System.getProperty("user.home") + "/.m2/repository");
+ Path osgiBase = Paths.get("/user/share/osgi");
+ if (Files.exists(mavenBase)) {
+ Path mavenPath = mavenBase.resolve(distributionUrl);
+ if (Files.exists(mavenPath))
+ baseUrl = mavenBase.toUri().toString();
+ } else if (Files.exists(osgiBase)) {
+ Path osgiPath = osgiBase.resolve(distributionUrl);
+ if (Files.exists(osgiPath))
+ baseUrl = osgiBase.toUri().toString();
+ }
+
+ this.baseDir = baseDir;
+ this.confDir = baseDir.resolve("state");
+ this.dataDir = baseDir.resolve("data");
+
+ }
+
+ public void start() {
+ long begin = System.currentTimeMillis();
+ // log4j
+ Path log4jFile = confDir.resolve("log4j.properties");
+ if (!Files.exists(log4jFile))
+ copyResource("/org/argeo/osgi/boot/log4j.properties", log4jFile);
+ System.setProperty("log4j.configuration", "file://" + log4jFile.toAbsolutePath());
+
+ // Start Equinox
+ try {
+ ServiceLoader<FrameworkFactory> ff = ServiceLoader.load(FrameworkFactory.class);
+ FrameworkFactory frameworkFactory = ff.iterator().next();
+ Map<String, String> configuration = new HashMap<String, String>();
+ configuration.put("osgi.configuration.area", confDir.toAbsolutePath().toString());
+ configuration.put("osgi.instance.area", dataDir.toAbsolutePath().toString());
+ defaultConfiguration(configuration);
+
+ framework = frameworkFactory.newFramework(configuration);
+ framework.start();
+ info("## Date : " + new Date());
+ info("## Data : " + dataDir.toAbsolutePath());
+ } catch (Exception e) {
+ throw new IllegalStateException("Cannot start OSGi framework", e);
+ }
+ BundleContext bundleContext = framework.getBundleContext();
+ try {
+
+ // Spring configs currently require System properties
+ // System.getProperties().putAll(configuration);
+
+ // expected by JAAS as System.property FIXME
+ System.setProperty("osgi.instance.area", bundleContext.getProperty("osgi.instance.area"));
+
+ // OSGi bootstrap
+ OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
+
+ osgiBoot.installUrls(osgiBoot.getDistributionUrls(distributionUrl, baseUrl));
+
+ // Start runtime
+ Properties startProperties = new Properties();
+ // TODO make it possible to override it
+ startProperties.put("argeo.osgi.start.2.node",
+ "org.eclipse.equinox.http.servlet,org.eclipse.equinox.http.jetty,"
+ + "org.eclipse.equinox.metatype,org.eclipse.equinox.cm,org.eclipse.rap.rwt.osgi");
+ startProperties.put("argeo.osgi.start.3.node", "org.argeo.cms");
+ startProperties.put("argeo.osgi.start.4.node",
+ "org.eclipse.gemini.blueprint.extender,org.eclipse.equinox.http.registry");
+ osgiBoot.startBundles(startProperties);
+
+ // Find node repository
+ ServiceReference<?> sr = null;
+ while (sr == null) {
+ sr = bundleContext.getServiceReference("javax.jcr.Repository");
+ if (System.currentTimeMillis() - begin > timeout)
+ throw new RuntimeException("Could find node after " + timeout + "ms");
+ Thread.sleep(100);
+ }
+ Object nodeDeployment = bundleContext.getService(sr);
+ info("Node Deployment " + nodeDeployment);
+
+ // Initialization completed
+ long duration = System.currentTimeMillis() - begin;
+ info("## CMS Launcher initialized in " + (duration / 1000) + "s " + (duration % 1000) + "ms");
+ } catch (Exception e) {
+ shutdown();
+ throw new RuntimeException("Cannot start CMS", e);
+ } finally {
+
+ }
+ }
+
+ private void defaultConfiguration(Map<String, String> configuration) {
+ // all permissions to OSGi security manager
+ Path policyFile = confDir.resolve("osgi.policy");
+ if (!Files.exists(policyFile))
+ copyResource("/org/argeo/osgi/boot/osgi.policy", policyFile);
+ configuration.put("java.security.policy", "file://" + policyFile.toAbsolutePath());
+
+ configuration.put("org.eclipse.rap.workbenchAutostart", "false");
+ configuration.put("org.eclipse.equinox.http.jetty.autostart", "false");
+ configuration.put("org.osgi.framework.bootdelegation",
+ "com.sun.jndi.ldap,com.sun.jndi.ldap.sasl,com.sun.security.jgss,com.sun.jndi.dns,"
+ + "com.sun.nio.file,com.sun.nio.sctp");
+
+ // Do clean
+ // configuration.put("osgi.clean", "true");
+ // if (args.length == 0) {
+ // configuration.put("osgi.console", "");
+ // }
+ }
+
+ public void shutdown() {
+ try {
+ framework.stop();
+ framework.waitForStop(15 * 1000);
+ } catch (Exception silent) {
+ }
+ }
+
+ public Path getConfDir() {
+ return confDir;
+ }
+
+ public Path getDataDir() {
+ return dataDir;
+ }
+
+ public Framework getFramework() {
+ return framework;
+ }
+
+ public static void main(String[] args) {
+ try {
+ // Prepare directories
+ Path executionDir = Paths.get(System.getProperty("user.dir"));
+
+ String distributionUrl;
+ if (args.length == 0) {
+ distributionUrl = "org/argeo/commons/org.argeo.dep.cms.sdk/2.1.65/org.argeo.dep.cms.sdk-2.1.65.jar";
+ } else {
+ distributionUrl = args[0];
+ }
+
+ NodeRunner nodeRunner = new NodeRunner(distributionUrl, executionDir);
+ nodeRunner.start();
+ if (args.length != 0)
+ System.exit(0);
+ } catch (Exception e) {
+ e.printStackTrace();
+ System.exit(1);
+ }
+ }
+
+ protected static void info(Object msg) {
+ System.out.println(msg);
+ }
+
+ protected static void err(Object msg) {
+ System.err.println(msg);
+ }
+
+ protected static void debug(Object msg) {
+ System.out.println(msg);
+ }
+
+ protected static void copyResource(String resource, Path targetFile) {
+ InputStream input = null;
+ OutputStream output = null;
+ try {
+ input = NodeRunner.class.getResourceAsStream(resource);
+ Files.createDirectories(targetFile.getParent());
+ output = Files.newOutputStream(targetFile);
+ byte[] buf = new byte[8192];
+ while (true) {
+ int length = input.read(buf);
+ if (length < 0)
+ break;
+ output.write(buf, 0, length);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot write " + resource + " file to " + targetFile, e);
+ } finally {
+ try {
+ input.close();
+ } catch (Exception ignore) {
+ }
+ try {
+ output.close();
+ } catch (Exception ignore) {
+ }
+ }
+
+ }
+
+}
* START
*/
public void startBundles() {
+ startBundles(System.getProperties());
+ }
+
+ public void startBundles(Properties properties) {
FrameworkStartLevel frameworkStartLevel = bundleContext.getBundle(0).adapt(FrameworkStartLevel.class);
// default and active start levels from System properties
Integer defaultStartLevel = new Integer(
- Integer.parseInt(OsgiBootUtils.getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")));
- Integer activeStartLevel = new Integer(OsgiBootUtils.getProperty(PROP_OSGI_STARTLEVEL, "6"));
+ Integer.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")));
+ Integer activeStartLevel = new Integer(getProperty(PROP_OSGI_STARTLEVEL, "6"));
SortedMap<Integer, List<String>> startLevels = new TreeMap<Integer, List<String>>();
- computeStartLevels(startLevels, System.getProperties(), defaultStartLevel);
+ computeStartLevels(startLevels, properties, defaultStartLevel);
// inverts the map for the time being, TODO optimise
Map<String, Integer> bundleStartLevels = new HashMap<>();
for (Integer level : startLevels.keySet()) {
* effects.
*/
public List<String> getBundlesUrls() {
- String bundlePatterns = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BUNDLES);
+ String bundlePatterns = getProperty(PROP_ARGEO_OSGI_BUNDLES);
return getBundlesUrls(bundlePatterns);
}
* default base url
*/
public List<String> getBundlesUrls(String bundlePatterns) {
- String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL, DEFAULT_BASE_URL);
+ String baseUrl = getProperty(PROP_ARGEO_OSGI_BASE_URL, DEFAULT_BASE_URL);
return getBundlesUrls(baseUrl, bundlePatterns);
}
* DISTRIBUTION JAR INSTALLATION
*/
public List<String> getDistributionUrls() {
+ String distributionUrl = getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL);
+ String baseUrl = getProperty(PROP_ARGEO_OSGI_BASE_URL);
+ return getDistributionUrls(distributionUrl, baseUrl);
+ }
+
+ public List<String> getDistributionUrls(String distributionUrl, String baseUrl) {
List<String> urls = new ArrayList<String>();
- String distributionUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_DISTRIBUTION_URL);
if (distributionUrl == null)
return urls;
- String baseUrl = OsgiBootUtils.getProperty(PROP_ARGEO_OSGI_BASE_URL);
DistributionBundle distributionBundle;
if (baseUrl != null && !(distributionUrl.startsWith("http") || distributionUrl.startsWith("file"))) {
// return url;
// }
+ /**
+ * Gets a property value
+ *
+ * @return null when defaultValue is ""
+ */
+ public String getProperty(String name, String defaultValue) {
+ String value = bundleContext.getProperty(name);
+ if (value == null)
+ return defaultValue; // may be null
+ else
+ return value;
+
+// if (defaultValue != null)
+// value = System.getProperty(name, defaultValue);
+// else
+// value = System.getProperty(name);
+//
+// if (value == null || value.equals(""))
+// return null;
+// else
+// return value;
+ }
+
+ public String getProperty(String name) {
+ return getProperty(name, null);
+ }
+
/*
* BEAN METHODS
*/