X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.osgi%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fosgi%2FBundlesManager.java;h=e24b7223b7736299125d5aae7f6ddd29b5a4d0f6;hb=44d72c1a645ca69e28ad03d46367cd4a9dde99bd;hp=a096a44d413bf6eca8c216440144bfd09bfab427;hpb=593b5ee7a27f426f2c14126a9144ac3630ab4ee6;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java index a096a44d4..e24b7223b 100644 --- a/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java +++ b/runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java @@ -17,12 +17,17 @@ import org.springframework.beans.factory.DisposableBean; 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; @@ -72,7 +77,9 @@ public class BundlesManager implements BundleContextAware, FrameworkListener, + ")"; // 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; @@ -83,11 +90,23 @@ public class BundlesManager implements BundleContextAware, FrameworkListener, 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); } @@ -280,16 +299,23 @@ public class BundlesManager implements BundleContextAware, FrameworkListener, Assert.isTrue( osgiBundle.getName().equals(bundle.getSymbolicName()), "symbolic name consistent"); - Assert.isTrue(osgiBundle.getVersion().equals( - bundle.getHeaders().get(Constants.BUNDLE_VERSION)), - "version consistent"); + if (osgiBundle.getVersion() != null) + Assert.isTrue(osgiBundle.getVersion().equals( + bundle.getHeaders().get(Constants.BUNDLE_VERSION)), + "version consistent"); } else { - for (Bundle b : bundleContext.getBundles()) { + bundles: for (Bundle b : bundleContext.getBundles()) { if (b.getSymbolicName().equals(osgiBundle.getName())) { + if (osgiBundle.getVersion() == null) { + bundle = b; + break bundles; + } + if (b.getHeaders().get(Constants.BUNDLE_VERSION).equals( osgiBundle.getVersion())) { bundle = b; osgiBundle.setInternalBundleId(b.getBundleId()); + break bundles; } } } @@ -339,4 +365,16 @@ public class BundlesManager implements BundleContextAware, FrameworkListener, 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); + } + + } + }