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.PackageAdmin;
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_ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
public final static String PROP_ARGEO_OSGI_START = "argeo.osgi.start";
}
System.setProperty(PROP_ARGEO_OSGI_DATA_DIR, dataDir.getAbsolutePath());
- info(PROP_ARGEO_OSGI_DATA_DIR + "=" + dataDir.getAbsolutePath());
}
public static String removeFilePrefix(String url) {
}
public void bootstrap() {
- info("SLC OSGi bootstrap starting...");
+ long begin = System.currentTimeMillis();
+ System.out.println();
+ info("OSGi bootstrap starting...");
+ 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());
+ checkUnresolved();
startBundles();
- info("SLC OSGi bootstrap completed");
+ long duration = System.currentTimeMillis() - begin;
+ info("OSGi bootstra completed in "
+ + Math.round(((double) duration) / 1000) + "s (" + duration
+ + "ms), " + bundleContext.getBundles().length + " bundles");
+ System.out.println();
}
public void installUrls(List urls) {
+ " from " + url);
}
} catch (BundleException e) {
- warn("Could not install bundle from " + url + ": "
- + e.getMessage());
+ String message = e.getMessage();
+ if ((message.contains("Bundle \"" + SYMBOLIC_NAME_OSGI_BOOT
+ + "\"") || message.contains("Bundle \""
+ + SYMBOLIC_NAME_EQUINOX + "\""))
+ && message.contains("has already been installed")) {
+ // silent, in order to avoid warnings: we know that both
+ // have already been installed...
+ } else {
+ warn("Could not install bundle from " + url + ": "
+ + message);
+ }
if (debug)
e.printStackTrace();
}
+ " not started because it was not found.");
}
+ protected void checkUnresolved() {
+ // Refresh
+ ServiceReference packageAdminRef = bundleContext
+ .getServiceReference(PackageAdmin.class.getName());
+ PackageAdmin packageAdmin = (PackageAdmin) bundleContext
+ .getService(packageAdminRef);
+ packageAdmin.resolveBundles(null);
+
+ Bundle[] bundles = bundleContext.getBundles();
+ List /* Bundle */unresolvedBundles = new ArrayList();
+ for (int i = 0; i < bundles.length; i++) {
+ int bundleState = bundles[i].getState();
+ if (!(bundleState == Bundle.ACTIVE
+ || bundleState == Bundle.RESOLVED || bundleState == Bundle.STARTING))
+ unresolvedBundles.add(bundles[i]);
+ }
+
+ if (unresolvedBundles.size() != 0) {
+ warn("Unresolved bundles " + unresolvedBundles);
+ }
+ }
+
protected void waitForBundleResolvedOrActive(long startBegin, Bundle bundle)
throws Exception {
int originalState = bundle.getState();
}
protected static void info(Object obj) {
- System.out.println("#OSGiBOOT# " + obj);
+ System.out.println("# OSGiBOOT # " + obj);
}
protected void debug(Object obj) {
if (debug)
- System.out.println("#OSGiBOOT DEBUG# " + obj);
+ System.out.println("# OSGiBOOT DBG # " + obj);
}
protected void warn(Object obj) {
- System.out.println("# WARN " + obj);
+ System.out.println("# OSGiBOOT WARN # " + obj);
// Because of a weird bug under Windows when starting it in a forked VM
// if (System.getProperty("os.name").contains("Windows"))
// System.out.println("# WARN " + obj);