X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.osgi.boot%2Fsrc%2Forg%2Fargeo%2Fosgi%2Fboot%2FOsgiBoot.java;h=4c3ee40f0c2095da9860af6f4ef4316a72ae5c59;hb=f8a1ec1e30180672eb9826ef47924ed43b3c6b3d;hp=300ebb82dd48ba2802b3328cd832fd3a3dc1dbbe;hpb=575dfe19b8516b1213b6d56d9f34fc3bcdb62026;p=lgpl%2Fargeo-commons.git
diff --git a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java
index 300ebb82d..4c3ee40f0 100644
--- a/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java
+++ b/org.argeo.osgi.boot/src/org/argeo/osgi/boot/OsgiBoot.java
@@ -15,11 +15,11 @@
*/
package org.argeo.osgi.boot;
-import java.io.BufferedReader;
+import static org.argeo.osgi.boot.OsgiBootUtils.debug;
+import static org.argeo.osgi.boot.OsgiBootUtils.warn;
+
import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
@@ -30,87 +30,70 @@ import java.util.Set;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
-import java.util.TreeSet;
+import org.argeo.osgi.boot.a2.ProvisioningManager;
import org.argeo.osgi.boot.internal.springutil.AntPathMatcher;
import org.argeo.osgi.boot.internal.springutil.PathMatcher;
import org.argeo.osgi.boot.internal.springutil.SystemPropertyUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.packageadmin.ExportedPackage;
-import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.Version;
+import org.osgi.framework.startlevel.BundleStartLevel;
+import org.osgi.framework.startlevel.FrameworkStartLevel;
+import org.osgi.framework.wiring.FrameworkWiring;
/**
* Basic provisioning of an OSGi runtime via file path patterns and system
- * properties. Java 1.4 compatible.
- * The approach is to generate list of URLs based on various methods, configured
- * via system properties.
+ * properties. The approach is to generate list of URLs based on various
+ * methods, configured via properties.
*/
-public class OsgiBoot {
- public final static String SYMBOLIC_NAME_OSGI_BOOT = "org.argeo.osgi.boot";
- public final static String SYMBOLIC_NAME_EQUINOX = "org.eclipse.osgi";
-
- public final static String PROP_OSGI_STARTLEVEL = "osgi.startLevel";
- public final static String PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL = "osgi.bundles.defaultStartLevel";
-
- public final static String PROP_ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
-
+public class OsgiBoot implements OsgiBootConstants {
public final static String PROP_ARGEO_OSGI_START = "argeo.osgi.start";
+ public final static String PROP_ARGEO_OSGI_SOURCES = "argeo.osgi.sources";
+
public final static String PROP_ARGEO_OSGI_BUNDLES = "argeo.osgi.bundles";
- public final static String PROP_ARGEO_OSGI_LOCATIONS = "argeo.osgi.locations";
public final static String PROP_ARGEO_OSGI_BASE_URL = "argeo.osgi.baseUrl";
- /** @deprecated */
- public final static String PROP_ARGEO_OSGI_MODULES_URL = "argeo.osgi.modulesUrl";
+ public final static String PROP_ARGEO_OSGI_LOCAL_CACHE = "argeo.osgi.localCache";
public final static String PROP_ARGEO_OSGI_DISTRIBUTION_URL = "argeo.osgi.distributionUrl";
// booleans
public final static String PROP_ARGEO_OSGI_BOOT_DEBUG = "argeo.osgi.boot.debug";
- public final static String PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN = "argeo.osgi.boot.excludeSvn";
- public final static String PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER = "argeo.osgi.boot.installInLexicographicOrder";
+ // public final static String PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN =
+ // "argeo.osgi.boot.excludeSvn";
- public final static String PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT = "argeo.osgi.boot.defaultTimeout";
- public final static String PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR = "argeo.osgi.boot.modulesUrlSeparator";
public final static String PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE = "argeo.osgi.boot.systemPropertiesFile";
public final static String PROP_ARGEO_OSGI_BOOT_APPCLASS = "argeo.osgi.boot.appclass";
public final static String PROP_ARGEO_OSGI_BOOT_APPARGS = "argeo.osgi.boot.appargs";
public final static String DEFAULT_BASE_URL = "reference:file:";
- public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
+ // public final static String EXCLUDES_SVN_PATTERN = "**/.svn/**";
// OSGi system properties
- public final static String INSTANCE_AREA_PROP = "osgi.instance.area";
- public final static String INSTANCE_AREA_DEFAULT_PROP = "osgi.instance.area.default";
-
- private boolean debug = Boolean.valueOf(
- System.getProperty(PROP_ARGEO_OSGI_BOOT_DEBUG, "false"))
- .booleanValue();
- /** Exclude svn metadata implicitely(a bit costly) */
- private boolean excludeSvn = Boolean.valueOf(
- System.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN, "false"))
- .booleanValue();
+ final static String PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL = "osgi.bundles.defaultStartLevel";
+ final static String PROP_OSGI_STARTLEVEL = "osgi.startLevel";
+ final static String INSTANCE_AREA_PROP = "osgi.instance.area";
+ final static String CONFIGURATION_AREA_PROP = "osgi.configuration.area";
- /**
- * The {@link #installUrls(List)} methods won't follow the list order but
- * order the urls according to the alphabetical order of the file names
- * (last part of the URL). The goal is to stay closer from Eclipse PDE way
- * of installing target platform bundles.
- */
- private boolean installInLexicographicOrder = Boolean
- .valueOf(
- System.getProperty(
- PROP_ARGEO_OSGI_BOOT_INSTALL_IN_LEXICOGRAPHIC_ORDER,
- "true")).booleanValue();;
+ // Symbolic names
+ public final static String SYMBOLIC_NAME_OSGI_BOOT = "org.argeo.osgi.boot";
+ public final static String SYMBOLIC_NAME_EQUINOX = "org.eclipse.osgi";
- /** Default is 10s (set in constructor) */
- private long defaultTimeout;
+ /** Exclude svn metadata implicitely(a bit costly) */
+ // private boolean excludeSvn =
+ // Boolean.valueOf(System.getProperty(PROP_ARGEO_OSGI_BOOT_EXCLUDE_SVN,
+ // "false"))
+ // .booleanValue();
- /** Default is ',' (set in constructor) */
- private String modulesUrlSeparator = ",";
+ /** Default is 10s */
+ @Deprecated
+ private long defaultTimeout = 10000l;
private final BundleContext bundleContext;
+ private final String localCache;
+
+ private final ProvisioningManager provisioningManager;
/*
* INITIALIZATION
@@ -118,36 +101,22 @@ public class OsgiBoot {
/** Constructor */
public OsgiBoot(BundleContext bundleContext) {
this.bundleContext = bundleContext;
- defaultTimeout = Long.parseLong(OsgiBootUtils.getProperty(
- PROP_ARGEO_OSGI_BOOT_DEFAULT_TIMEOUT, "10000"));
- modulesUrlSeparator = OsgiBootUtils.getProperty(
- PROP_ARGEO_OSGI_BOOT_MODULES_URL_SEPARATOR, ",");
- initSystemProperties();
- }
+ String homeUri = Paths.get(System.getProperty("user.home")).toUri().toString();
+ localCache = getProperty(PROP_ARGEO_OSGI_LOCAL_CACHE, homeUri + ".m2/repository/");
- /**
- * Set additional system properties, especially ${argeo.osgi.data.dir} as an
- * OS file path (and not a file:// URL)
- */
- protected void initSystemProperties() {
- String osgiInstanceArea = System.getProperty(INSTANCE_AREA_PROP);
- String osgiInstanceAreaDefault = System
- .getProperty(INSTANCE_AREA_DEFAULT_PROP);
- String tempDir = System.getProperty("java.io.tmpdir");
-
- File dataDir = null;
- if (osgiInstanceArea != null) {
- // within OSGi with -data specified
- osgiInstanceArea = removeFilePrefix(osgiInstanceArea);
- dataDir = new File(osgiInstanceArea);
- } else if (osgiInstanceAreaDefault != null) {
- // within OSGi without -data specified
- osgiInstanceAreaDefault = removeFilePrefix(osgiInstanceAreaDefault);
- dataDir = new File(osgiInstanceAreaDefault);
- } else {// outside OSGi
- dataDir = new File(tempDir + File.separator + "argeoOsgiData");
+ provisioningManager = new ProvisioningManager(bundleContext);
+ String sources = getProperty(PROP_ARGEO_OSGI_SOURCES);
+ if (sources == null) {
+ provisioningManager.registerDefaultSource();
+ } else {
+ for (String source : sources.split(",")) {
+ provisioningManager.registerSource(source);
+ }
}
- System.setProperty(PROP_ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
+ }
+
+ ProvisioningManager getProvisioningManager() {
+ return provisioningManager;
}
/*
@@ -155,115 +124,137 @@ public class OsgiBoot {
*/
/** Bootstraps the OSGi runtime */
public void bootstrap() {
- long begin = System.currentTimeMillis();
- System.out.println();
- OsgiBootUtils.info("OSGi bootstrap starting...");
- OsgiBootUtils.info("Writable data directory : "
- + System.getProperty(PROP_ARGEO_OSGI_DATA_DIR)
- + " (set as system property " + PROP_ARGEO_OSGI_DATA_DIR + ")");
- installUrls(getBundlesUrls());
- installUrls(getLocationsUrls());
- installUrls(getModulesUrls());
- installUrls(getDistributionUrls());
- checkUnresolved();
- startBundles();
- long duration = System.currentTimeMillis() - begin;
- OsgiBootUtils.info("OSGi bootstrap completed in "
- + Math.round(((double) duration) / 1000) + "s (" + duration
- + "ms), " + bundleContext.getBundles().length + " bundles");
-
- // display packages exported twice
- if (debug) {
- Map /* > */duplicatePackages = findPackagesExportedTwice();
+ try {
+ long begin = System.currentTimeMillis();
+ System.out.println();
+ String osgiInstancePath = bundleContext.getProperty(INSTANCE_AREA_PROP);
+ OsgiBootUtils
+ .info("OSGi bootstrap starting" + (osgiInstancePath != null ? " (" + osgiInstancePath + ")" : ""));
+ installUrls(getBundlesUrls());
+ installUrls(getDistributionUrls());
+ provisioningManager.install(null);
+ startBundles();
+ long duration = System.currentTimeMillis() - begin;
+ OsgiBootUtils.info("OSGi bootstrap completed in " + Math.round(((double) duration) / 1000) + "s ("
+ + duration + "ms), " + bundleContext.getBundles().length + " bundles");
+ } catch (RuntimeException e) {
+ OsgiBootUtils.error("OSGi bootstrap FAILED", e);
+ throw e;
+ }
+
+ // diagnostics
+ if (OsgiBootUtils.debug) {
+ OsgiBootDiagnostics diagnostics = new OsgiBootDiagnostics(bundleContext);
+ diagnostics.checkUnresolved();
+ Map> duplicatePackages = diagnostics.findPackagesExportedTwice();
if (duplicatePackages.size() > 0) {
OsgiBootUtils.info("Packages exported twice:");
- Iterator it = duplicatePackages.keySet().iterator();
+ Iterator it = duplicatePackages.keySet().iterator();
while (it.hasNext()) {
- String pkgName = it.next().toString();
+ String pkgName = it.next();
OsgiBootUtils.info(pkgName);
- Set bdles = (Set) duplicatePackages.get(pkgName);
- Iterator bdlesIt = bdles.iterator();
+ Set bdles = duplicatePackages.get(pkgName);
+ Iterator bdlesIt = bdles.iterator();
while (bdlesIt.hasNext())
OsgiBootUtils.info(" " + bdlesIt.next());
}
}
}
-
System.out.println();
}
+ public void update() {
+ provisioningManager.update();
+ }
+
/*
* INSTALLATION
*/
/** Install a single url. Convenience method. */
public Bundle installUrl(String url) {
- List urls = new ArrayList();
+ List urls = new ArrayList();
urls.add(url);
installUrls(urls);
return (Bundle) getBundlesByLocation().get(url);
}
/** Install the bundles at this URL list. */
- public void installUrls(List urls) {
- Map installedBundles = getBundlesByLocation();
-
- if (installInLexicographicOrder) {
- SortedMap map = new TreeMap();
- // reorder
- for (int i = 0; i < urls.size(); i++) {
- String url = (String) urls.get(i);
- int index = url.lastIndexOf('/');
- String fileName;
- if (index >= 0)
- fileName = url.substring(index + 1);
- else
- fileName = url;
- map.put(fileName, url);
- }
-
- // install
- Iterator keys = map.keySet().iterator();
- while (keys.hasNext()) {
- Object key = keys.next();
- String url = map.get(key).toString();
- installUrl(url, installedBundles);
- }
- } else {
- for (int i = 0; i < urls.size(); i++) {
- String url = (String) urls.get(i);
- installUrl(url, installedBundles);
- }
+ public void installUrls(List urls) {
+ Map installedBundles = getBundlesByLocation();
+ for (int i = 0; i < urls.size(); i++) {
+ String url = (String) urls.get(i);
+ installUrl(url, installedBundles);
}
-
+ refreshFramework();
}
/** Actually install the provided URL */
- protected void installUrl(String url, Map installedBundles) {
+ protected void installUrl(String url, Map installedBundles) {
try {
if (installedBundles.containsKey(url)) {
Bundle bundle = (Bundle) installedBundles.get(url);
- // bundle.update();
- if (debug)
- debug("Bundle " + bundle.getSymbolicName()
- + " already installed from " + url);
+ if (OsgiBootUtils.debug)
+ debug("Bundle " + bundle.getSymbolicName() + " already installed from " + url);
+ } else if (url.contains("/" + SYMBOLIC_NAME_EQUINOX + "/")
+ || url.contains("/" + SYMBOLIC_NAME_OSGI_BOOT + "/")) {
+ if (OsgiBootUtils.debug)
+ warn("Skip " + url);
+ return;
} else {
Bundle bundle = bundleContext.installBundle(url);
- if (debug)
- debug("Installed bundle " + bundle.getSymbolicName()
- + " from " + url);
+ if (url.startsWith("http"))
+ OsgiBootUtils
+ .info("Installed " + bundle.getSymbolicName() + "-" + bundle.getVersion() + " from " + url);
+ else if (OsgiBootUtils.debug)
+ OsgiBootUtils.debug(
+ "Installed " + bundle.getSymbolicName() + "-" + bundle.getVersion() + " from " + url);
+ assert bundle.getSymbolicName() != null;
+ // uninstall previous versions
+ bundles: for (Bundle b : bundleContext.getBundles()) {
+ if (b.getSymbolicName() == null)
+ continue bundles;
+ if (bundle.getSymbolicName().equals(b.getSymbolicName())) {
+ Version bundleV = bundle.getVersion();
+ Version bV = b.getVersion();
+ if (bV == null)
+ continue bundles;
+ if (bundleV.getMajor() == bV.getMajor() && bundleV.getMinor() == bV.getMinor()) {
+ if (bundleV.getMicro() > bV.getMicro()) {
+ // uninstall older bundles
+ b.uninstall();
+ OsgiBootUtils.debug("Uninstalled " + b);
+ } else if (bundleV.getMicro() < bV.getMicro()) {
+ // uninstall just installed bundle if newer
+ bundle.uninstall();
+ OsgiBootUtils.debug("Uninstalled " + bundle);
+ break bundles;
+ } else {
+ // uninstall any other with same major/minor
+ if (!bundleV.getQualifier().equals(bV.getQualifier())) {
+ b.uninstall();
+ OsgiBootUtils.debug("Uninstalled " + b);
+ }
+ }
+ }
+ }
+ }
}
} catch (BundleException e) {
+ final String ALREADY_INSTALLED = "is already installed";
String message = e.getMessage();
- if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT + "\"") || message
- .contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX + "\""))
- && message.contains("has already been installed")) {
+ if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT + "\"")
+ || message.contains("Bundle \"" + SYMBOLIC_NAME_EQUINOX + "\""))
+ && message.contains(ALREADY_INSTALLED)) {
// silent, in order to avoid warnings: we know that both
// have already been installed...
} else {
- OsgiBootUtils.warn("Could not install bundle from " + url
- + ": " + message);
+ if (message.contains(ALREADY_INSTALLED)) {
+ if (OsgiBootUtils.isDebug())
+ OsgiBootUtils.warn("Duplicate install from " + url + ": " + message);
+ } else
+ OsgiBootUtils.warn("Could not install bundle from " + url + ": " + message);
}
- if (debug)
+ if (OsgiBootUtils.debug && !message.contains(ALREADY_INSTALLED))
e.printStackTrace();
}
}
@@ -272,42 +263,62 @@ public class OsgiBoot {
* 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"));
-
- SortedMap/* > */startLevels = new TreeMap();
- computeStartLevels(startLevels, System.getProperties(),
- defaultStartLevel);
-
- Iterator/* */levels = startLevels.keySet().iterator();
- while (levels.hasNext()) {
- Integer level = (Integer) levels.next();
- boolean allStarted = startBundles((List) startLevels.get(level));
- if (!allStarted)
- OsgiBootUtils
- .warn("Not all bundles started for level " + level);
- if (level.equals(activeStartLevel))
- break;// active start level reached
+ Integer defaultStartLevel = new Integer(
+ Integer.parseInt(getProperty(PROP_OSGI_BUNDLES_DEFAULTSTARTLEVEL, "4")));
+ Integer activeStartLevel = new Integer(getProperty(PROP_OSGI_STARTLEVEL, "6"));
+
+ SortedMap> startLevels = new TreeMap>();
+ computeStartLevels(startLevels, properties, defaultStartLevel);
+ // inverts the map for the time being, TODO optimise
+ Map bundleStartLevels = new HashMap<>();
+ for (Integer level : startLevels.keySet()) {
+ for (String bsn : startLevels.get(level))
+ bundleStartLevels.put(bsn, level);
}
-
+ for (Bundle bundle : bundleContext.getBundles()) {
+ String bsn = bundle.getSymbolicName();
+ if (bundleStartLevels.containsKey(bsn)) {
+ BundleStartLevel bundleStartLevel = bundle.adapt(BundleStartLevel.class);
+ Integer level = bundleStartLevels.get(bsn);
+ if (bundleStartLevel.getStartLevel() != level || !bundleStartLevel.isPersistentlyStarted()) {
+ bundleStartLevel.setStartLevel(level);
+ try {
+ bundle.start();
+ } catch (BundleException e) {
+ OsgiBootUtils.error("Cannot mark " + bsn + " as started", e);
+ }
+ if (getDebug())
+ OsgiBootUtils.debug(bsn + " starts at level " + level);
+ }
+ }
+ }
+ frameworkStartLevel.setStartLevel(activeStartLevel, (FrameworkEvent event) -> {
+ if (getDebug())
+ OsgiBootUtils.debug("Framework event: " + event);
+ int initialStartLevel = frameworkStartLevel.getInitialBundleStartLevel();
+ int startLevel = frameworkStartLevel.getStartLevel();
+ OsgiBootUtils.debug("Framework start level: " + startLevel + " (initial: " + initialStartLevel + ")");
+ });
}
- public static void computeStartLevels(
- SortedMap/* > */startLevels,
- Properties properties, Integer defaultStartLevel) {
+ private static void computeStartLevels(SortedMap> startLevels, Properties properties,
+ Integer defaultStartLevel) {
// default (and previously, only behaviour)
- appendToStartLevels(startLevels, defaultStartLevel,
- properties.getProperty(PROP_ARGEO_OSGI_START, ""));
+ appendToStartLevels(startLevels, defaultStartLevel, properties.getProperty(PROP_ARGEO_OSGI_START, ""));
// list argeo.osgi.start.* system properties
- Iterator/* */keys = properties.keySet().iterator();
+ Iterator