package org.argeo.slc.lib.detached;
import java.io.File;
+import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ResourceLoaderAware;
+import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.osgi.context.BundleContextAware;
protected Resource findOsgiboot(Bundle bundle) {
String location = bundle.getLocation();
if (location.startsWith("initial@reference:file:"))
- location = System.getProperty("osgi.install.area") + "/../"
+ location = System.getProperty("osgi.install.area")
+ location.substring("initial@reference:file:".length());
if (location.charAt(location.length() - 1) == '/')
location.substring(0, location.length() - 1);
}
protected Resource asResource(String location) {
- Resource res = resourceLoader.getResource(location);
+ // Resource res = resourceLoader.getResource(location);
+
+ final Resource res;
+ if (location.startsWith("file:")) {
+ File file = new File(location.substring("file:".length()));
+ if (!file.exists())
+ throw new SlcException("File " + file + " does not exist");
+
+ try {
+ res = new FileSystemResource(file.getCanonicalFile());
+ } catch (IOException e) {
+ throw new SlcException("Cannot create resource based on "
+ + file, e);
+ }
+ } else
+ res = resourceLoader.getResource(location);
+
if (log.isDebugEnabled())
log.debug("Converted " + location + " to " + res);
return res;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.osgi.context.BundleContextAware;
+import org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent;
+import org.springframework.osgi.context.event.OsgiBundleApplicationContextListener;
+import org.springframework.osgi.context.event.OsgiBundleContextClosedEvent;
+import org.springframework.osgi.context.event.OsgiBundleContextFailedEvent;
+import org.springframework.osgi.context.event.OsgiBundleContextRefreshedEvent;
import org.springframework.osgi.util.OsgiFilterUtils;
import org.springframework.util.Assert;
/** Wraps low-level access to a {@link BundleContext} */
public class BundlesManager implements BundleContextAware, FrameworkListener,
- InitializingBean, DisposableBean {
+ InitializingBean, DisposableBean, OsgiBundleApplicationContextListener {
private final static Log log = LogFactory.getLog(BundlesManager.class);
private BundleContext bundleContext;
+ ")";
// Wait for application context to be ready
// TODO: use service tracker
- getServiceRefSynchronous(ApplicationContext.class.getName(), filter);
+ ServiceReference[] srs = getServiceRefSynchronous(
+ ApplicationContext.class.getName(), filter);
+ ServiceReference sr = srs[0];
long aAppContext = System.currentTimeMillis();
long end = aAppContext;
log.debug(" TOTAL\t: " + (aAppContext - bAppContext) + "ms");
}
- if (log.isDebugEnabled())
+ if (log.isDebugEnabled()) {
log.debug("Bundle " + bundle.getSymbolicName()
+ " ready to be used at latest version."
+ " (upgrade performed in " + (end - begin) + "ms).");
- log.debug(" TOTAL\t: " + (end - begin) + "ms");
+ log.debug(" TOTAL\t: " + (end - begin) + "ms");
+
+ ApplicationContext applicationContext = (ApplicationContext) bundleContext
+ .getService(sr);
+ int beanDefCount = applicationContext.getBeanDefinitionCount();
+ log.debug(" " + beanDefCount + " beans in app context of "
+ + bundle.getSymbolicName()
+ + ", average init time per bean=" + (end - begin)
+ / beanDefCount + "ms");
+ }
+
+ bundleContext.ungetService(sr);
+
} catch (Exception e) {
throw new SlcException("Cannot update bundle " + osgiBundle, e);
}
this.pollingPeriod = pollingPeriod;
}
+ public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent event) {
+ if (event instanceof OsgiBundleContextRefreshedEvent) {
+ log.debug("App context refreshed: " + event);
+ } else if (event instanceof OsgiBundleContextFailedEvent) {
+ log.debug("App context failed: " + event);
+ }
+ if (event instanceof OsgiBundleContextClosedEvent) {
+ log.debug("App context closed: " + event);
+ }
+
+ }
+
}