Improve logging and monitoring
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jul 2009 17:37:27 +0000 (17:37 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 26 Jul 2009 17:37:27 +0000 (17:37 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2770 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.lib.detached/src/main/java/org/argeo/slc/lib/detached/DetachedLauncher.java
runtime/org.argeo.slc.support.equinox/pom.xml
runtime/org.argeo.slc.support.equinox/src/main/resources/META-INF/spring/osgi.xml
runtime/org.argeo.slc.support.osgi/src/main/java/org/argeo/slc/osgi/BundlesManager.java

index d0ceb56b77cc5175dbb70fa762a49dcd4ed3330d..825a9717d91efec1fbb1cb1b0a2996775a1abd2d 100644 (file)
@@ -1,6 +1,7 @@
 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;
@@ -10,6 +11,7 @@ import org.osgi.framework.Bundle;
 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;
@@ -88,7 +90,7 @@ public class DetachedLauncher extends JvmProcess implements 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);
@@ -96,7 +98,23 @@ public class DetachedLauncher extends JvmProcess implements BundleContextAware,
        }
 
        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;
index 9fed73946acb57ba9b4c42260f9d7a8f289eaeca..a4d112a03d02853b590bf5f0f9c7ced3d602d63d 100644 (file)
@@ -41,7 +41,8 @@
                                                        org.argeo.slc.*
                                                </Export-Package>
                                                <Import-Package>*,
-                                                       org.springframework.beans.factory.config
+                                                       org.springframework.beans.factory.config,
+                                                       org.springframework.osgi.context.event
                                                </Import-Package>
                                        </instructions>
                                </configuration>
index 0a98838284e144653363b5c5d3adab23caa1d99f..9dab0f29ea8662b5d93ba56a16fc03e3a1a0ac42 100644 (file)
@@ -9,4 +9,9 @@
        <service interface="org.eclipse.osgi.framework.console.CommandProvider"\r
                ref="commandProvider" />\r
 \r
+<!-- \r
+       <service\r
+               interface="org.springframework.osgi.context.event.OsgiBundleApplicationContextListener"\r
+               ref="bundlesManager" />\r
+ -->\r
 </beans:beans>
\ No newline at end of file
index a096a44d413bf6eca8c216440144bfd09bfab427..22d1e4b455b347953b2b38d0806c13cbd7e46f3f 100644 (file)
@@ -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);
                }
@@ -339,4 +358,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);
+               }
+
+       }
+
 }