View Javadoc

1   package com.atlassian.plugin.osgi.container.felix;
2   
3   import org.apache.felix.framework.Logger;
4   import org.apache.felix.framework.resolver.ResolveException;
5   import org.apache.felix.framework.util.FelixConstants;
6   import org.osgi.framework.BundleException;
7   import org.slf4j.LoggerFactory;
8   
9   import java.util.List;
10  import java.util.Arrays;
11  
12  /**
13   * Bridges Felix logging messages with the Commons Logging
14   */
15  public class FelixLoggerBridge extends Logger {
16      private final org.slf4j.Logger log;
17  
18      private static final List<String> messagesToIgnore = Arrays.asList(
19              "BeanInfo",
20              "sun.beans.editors.",
21              "add an import for 'org.eclipse.gemini.blueprint.service.",
22              "Class 'org.springframework.util.Assert'",
23              "Class '[Lorg.eclipse.gemini.blueprint.service",
24              "org.springframework.core.InfrastructureProxy",
25              "org.springframework.aop.SpringProxy",
26              "org.springframework.aop.IntroductionInfo",
27              "Class 'org.apache.commons.logging.impl.Log4JLogger'",
28              "org.springframework.util.Assert",
29              "org.eclipse.gemini.blueprint.service.importer.ServiceReferenceProxy",
30              "org.eclipse.gemini.blueprint.service.importer.ImportedOsgiServiceProxy",
31              "org.eclipse.gemini.blueprint.service.importer.support.ImportContextClassLoaderEditor",
32              "[Lorg.eclipse.gemini.blueprint.service.importer.OsgiServiceLifecycleListener;Editor"
33      );
34  
35      public FelixLoggerBridge(org.slf4j.Logger log) {
36          this.log = log;
37          setLogLevel(
38                  log.isDebugEnabled() ? Logger.LOG_DEBUG :
39                  log.isInfoEnabled() ? Logger.LOG_WARNING :
40                  log.isWarnEnabled() ? Logger.LOG_WARNING :
41                  Logger.LOG_ERROR);
42      }
43  
44      protected void doLog(org.osgi.framework.ServiceReference serviceReference, int level, java.lang.String message, java.lang.Throwable throwable) {
45          if (serviceReference != null)
46              message = "Service " + serviceReference + ": " + message;
47  
48          switch (level) {
49              case LOG_DEBUG:
50  
51                  // helps debug resolution errors when there are multiple constraint violations (final exception just
52                  // reports the last one)
53                  if (throwable != null && throwable instanceof ResolveException)
54                  {
55                      log.debug(message, throwable);
56                  }
57                  else
58                  {
59                      log.debug(message);
60                  }
61                  break;
62              case LOG_ERROR:
63                  if (throwable != null) {
64                      if ((throwable instanceof BundleException) &&
65                              (((BundleException) throwable).getNestedException() != null)) {
66                          throwable = ((BundleException) throwable).getNestedException();
67                      }
68                      log.error(message, throwable);
69                  } else
70                      log.error(message);
71                  break;
72              case LOG_INFO:
73                  logInfoUnlessLame(message);
74                  break;
75              case LOG_WARNING:
76                  // Handles special class loader errors from felix that have quite useful information
77                  if (throwable != null)
78                  {
79                      if (throwable instanceof ClassNotFoundException && isClassNotFoundsWeCareAbout(throwable))
80                      {
81                          log.debug("Class not found in bundle: " + message);
82                      }
83                  }
84                  else
85                  {
86                      logInfoUnlessLame(message);
87                  }
88                  break;
89              default:
90                  log.debug("UNKNOWN[" + level + "]: " + message);
91          }
92      }
93  
94      protected void logInfoUnlessLame(String message)
95      {
96          if (message != null)
97          {
98              // I'm really, really sick of these stupid messages
99              for (String dumbBit : messagesToIgnore)
100                 if (message.contains(dumbBit))
101                     return;
102         }
103         log.info(message);
104     }
105 
106     public boolean isClassNotFoundsWeCareAbout(Throwable t)
107     {
108         if (t instanceof ClassNotFoundException)
109         {
110             String className = t.getMessage();
111             if (className.contains("***") && t.getCause() instanceof ClassNotFoundException)
112             {
113                 className = t.getCause().getMessage();
114             }
115             if (!className.startsWith("org.springframework") && !className.endsWith("BeanInfo") && !className.endsWith("Editor"))
116             {
117                 return true;
118             }
119         }
120         return false;
121     }
122 }