felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1734035 - /felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java
Date Tue, 08 Mar 2016 08:43:00 GMT
Author: gnodet
Date: Tue Mar  8 08:42:59 2016
New Revision: 1734035

URL: http://svn.apache.org/viewvc?rev=1734035&view=rev
Log:
[FELIX-4871] The felix framework logger can't be used with reflection anymore

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java?rev=1734035&r1=1734034&r2=1734035&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/Logger.java Tue Mar  8
08:42:59 2016
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.framework;
 
+import java.lang.reflect.Method;
+
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleException;
 import org.osgi.framework.ServiceReference;
@@ -37,11 +39,36 @@ import org.osgi.framework.ServiceReferen
 **/
 public class Logger extends org.apache.felix.resolver.Logger
 {
+    private Object[] m_logger;
+
     public Logger()
     {
         super(LOG_ERROR);
     }
 
+    public void setLogger(Object logger)
+    {
+        if (logger == null)
+        {
+            m_logger = null;
+        }
+        else
+        {
+            try
+            {
+                Method mth = logger.getClass().getMethod("log",
+                        Integer.TYPE, String.class, Throwable.class);
+                mth.setAccessible(true);
+                m_logger = new Object[] { logger, mth };
+            }
+            catch (NoSuchMethodException ex)
+            {
+                System.err.println("Logger: " + ex);
+                m_logger = null;
+            }
+        }
+    }
+
     public final void log(ServiceReference sr, int level, String msg)
     {
         _log(null, sr, level, msg, null);
@@ -62,24 +89,55 @@ public class Logger extends org.apache.f
         _log(bundle, null, level, msg, throwable);
     }
 
+    protected void _log(
+            Bundle bundle, ServiceReference sr, int level,
+            String msg, Throwable throwable)
+    {
+        if (getLogLevel() >= level)
+        {
+            // Default logging action.
+            doLog(bundle, sr, level, msg, throwable);
+        }
+    }
+
     protected void doLog(
         Bundle bundle, ServiceReference sr, int level,
         String msg, Throwable throwable)
     {
-        String s = "";
+        StringBuilder s = new StringBuilder();
         if (sr != null)
         {
-            s = s + "SvcRef "  + sr + " ";
+            s.append("SvcRef ").append(sr).append(" ").append(msg);
         }
         else if (bundle != null)
         {
-            s = s + "Bundle " + bundle.toString() + " ";
+            s.append("Bundle ").append(bundle.toString()).append(" ").append(msg);
+        }
+        else
+        {
+            s.append(msg);
         }
-        s = s + msg;
         if (throwable != null)
         {
-            s = s + " (" + throwable + ")";
+            s.append(" (").append(throwable).append(")");
         }
+        doLog(level, s.toString(), throwable);
+    }
+
+    protected void doLog(int level, String msg, Throwable throwable)
+    {
+        if (m_logger != null)
+        {
+            doLogReflectively(level, msg, throwable);
+        }
+        else
+        {
+            doLogOut(level, msg, throwable);
+        }
+    }
+
+    protected void doLogOut(int level, String s, Throwable throwable)
+    {
         switch (level)
         {
             case LOG_DEBUG:
@@ -108,14 +166,20 @@ public class Logger extends org.apache.f
         }
     }
 
-    private void _log(
-        Bundle bundle, ServiceReference sr, int level,
-        String msg, Throwable throwable)
+    protected void doLogReflectively(int level, String msg, Throwable throwable)
     {
-        if (getLogLevel() >= level)
+        try
         {
-            // Default logging action.
-            doLog(bundle, sr, level, msg, throwable);
+            ((Method) m_logger[1]).invoke(
+                    m_logger[0],
+                    level,
+                    msg,
+                    throwable
+            );
+        }
+        catch (Exception ex)
+        {
+            System.err.println("Logger: " + ex);
         }
     }
 }



Mime
View raw message