felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pde...@apache.org
Subject svn commit: r1448164 - /felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java
Date Wed, 20 Feb 2013 13:40:48 GMT
Author: pderop
Date: Wed Feb 20 13:40:47 2013
New Revision: 1448164

URL: http://svn.apache.org/r1448164
Log:
Register a framework listener in order to track possible exceptions caught by the framework,
when
it invokes service listeners (The framework fires a FrameworkEvent when it catches exceptions
from
service changed listener callbacks).

Modified:
    felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java

Modified: felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java
URL: http://svn.apache.org/viewvc/felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java?rev=1448164&r1=1448163&r2=1448164&view=diff
==============================================================================
--- felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java (original)
+++ felix/trunk/dependencymanager/test/src/test/java/org/apache/felix/dm/test/Base.java Wed
Feb 20 13:40:47 2013
@@ -26,72 +26,74 @@ import java.net.URL;
 import org.junit.After;
 import org.junit.Before;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkEvent;
+import org.osgi.framework.FrameworkListener;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.log.LogService;
 
 /**
  * Base class for all test cases.
  */
-public class Base implements LogService {
+public class Base implements LogService, FrameworkListener {
     public static final String OSGI_SPEC_VERSION = "4.2.0";
     private final static int LOG_LEVEL = LogService.LOG_WARNING;
-    
+    private volatile boolean m_errorsLogged;
+
     /**
      * Register us as a LogService
+     * 
      * @param context
      */
     @Before
-    public void startup(BundleContext context)
-    {
+    public void startup(BundleContext context) {
         context.registerService(LogService.class.getName(), this, null);
+        context.addFrameworkListener(this);
     }
 
     /**
-     * Always cleanup our bundle location file (because pax seems to forget to cleanup it)
+     * Always cleanup our bundle location file (because pax seems to forget to
+     * cleanup it)
+     * 
      * @param context
      */
-    
+
     @After
-    public void tearDown(BundleContext context)
-    {
-        // The following code forces the temporary bundle files (from /tmp/tb/*) to be deleted
when jvm exits
+    public void tearDown(BundleContext context) {
+        // The following code forces the temporary bundle files (from /tmp/tb/*)
+        // to be deleted when jvm exits
         // (this patch seems to be only required with pax examp 2.0.0)
 
-        try
-        {
+        try {
             File f = new File(new URL(context.getBundle().getLocation()).getPath());
             f.deleteOnExit();
-        }
-        catch (Throwable t)
-        {
+        } catch (Throwable t) {
             t.printStackTrace();
         }
+        context.removeFrameworkListener(this);
     }
 
     /**
      * Suspend the current thread for a while.
-     * @param n the number of milliseconds to wait for.
+     * 
+     * @param n
+     *            the number of milliseconds to wait for.
      */
-    protected void sleep(int ms)
-    {
-        try
-        {
+    protected void sleep(int ms) {
+        try {
             Thread.sleep(ms);
-        }
-        catch (InterruptedException e)
-        {
+        } catch (InterruptedException e) {
         }
     }
 
-    public void log(int level, String message)
-    {
+    public void log(int level, String message) {
+        checkError(level, null);
         if (LOG_LEVEL >= level) {
             System.out.println(getLevel(level) + " " + message);
         }
     }
 
-    public void log(int level, String message, Throwable exception)
-    {
+    public void log(int level, String message, Throwable exception) {
+        checkError(level, exception);
         if (LOG_LEVEL >= level) {
             StringBuilder sb = new StringBuilder();
             sb.append(getLevel(level) + " ");
@@ -101,8 +103,8 @@ public class Base implements LogService 
         }
     }
 
-    public void log(ServiceReference sr, int level, String message)
-    {
+    public void log(ServiceReference sr, int level, String message) {
+        checkError(level, null);
         if (LOG_LEVEL >= level) {
             StringBuilder sb = new StringBuilder();
             sb.append(getLevel(level) + " ");
@@ -111,8 +113,8 @@ public class Base implements LogService 
         }
     }
 
-    public void log(ServiceReference sr, int level, String message, Throwable exception)
-    {
+    public void log(ServiceReference sr, int level, String message, Throwable exception)
{
+        checkError(level, exception);
         if (LOG_LEVEL >= level) {
             StringBuilder sb = new StringBuilder();
             sb.append(getLevel(level) + " ");
@@ -122,26 +124,72 @@ public class Base implements LogService 
         }
     }
 
-    private void parse(StringBuilder sb, Throwable t)
-    {
-        if (t != null)
-        {
+    protected boolean errorsLogged() {
+        return m_errorsLogged;
+    }
+
+    private void parse(StringBuilder sb, Throwable t) {
+        if (t != null) {
             sb.append(" - ");
             StringWriter buffer = new StringWriter();
             PrintWriter pw = new PrintWriter(buffer);
             t.printStackTrace(pw);
             sb.append(buffer.toString());
+            m_errorsLogged = true;
         }
     }
-    
+
     private String getLevel(int level) {
-        switch(level) 
-        {
-            case LogService.LOG_DEBUG: return "DEBUG";
-            case LogService.LOG_ERROR: return "ERROR";
-            case LogService.LOG_INFO: return "INFO";
-            case LogService.LOG_WARNING: return "WARN";
-            default: return "";
+        switch (level) {
+        case LogService.LOG_DEBUG:
+            return "DEBUG";
+        case LogService.LOG_ERROR:
+            return "ERROR";
+        case LogService.LOG_INFO:
+            return "INFO";
+        case LogService.LOG_WARNING:
+            return "WARN";
+        default:
+            return "";
+        }
+    }
+
+    private void checkError(int level, Throwable exception) {
+        if (level >= LOG_ERROR) {
+            m_errorsLogged = true;
+        }
+        if (exception != null) {
+            m_errorsLogged = true;
+        }
+    }
+
+    public void frameworkEvent(FrameworkEvent event) {
+        int eventType = event.getType();
+        String msg = getFrameworkEventMessage(eventType);
+        int level = (eventType == FrameworkEvent.ERROR) ? LOG_ERROR : LOG_WARNING;
+        if (msg != null) {
+            log(level, msg, event.getThrowable());
+        } else {
+            log(level, "Unknown fwk event: " + event);
+        }
+    }
+
+    private String getFrameworkEventMessage(int event) {
+        switch (event) {
+        case FrameworkEvent.ERROR:
+            return "FrameworkEvent: ERROR";
+        case FrameworkEvent.INFO:
+            return "FrameworkEvent INFO";
+        case FrameworkEvent.PACKAGES_REFRESHED:
+            return "FrameworkEvent: PACKAGE REFRESHED";
+        case FrameworkEvent.STARTED:
+            return "FrameworkEvent: STARTED";
+        case FrameworkEvent.STARTLEVEL_CHANGED:
+            return "FrameworkEvent: STARTLEVEL CHANGED";
+        case FrameworkEvent.WARNING:
+            return "FrameworkEvent: WARNING";
+        default:
+            return null;
         }
     }
 }



Mime
View raw message