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.springframework.osgi.service.",
22              "Class 'org.springframework.util.Assert'",
23              "Class '[Lorg.springframework.osgi.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.springframework.osgi.service.importer.ServiceReferenceProxy",
30              "org.springframework.osgi.service.importer.ImportedOsgiServiceProxy",
31              "org.springframework.osgi.service.importer.support.ImportContextClassLoaderEditor",
32              "[Lorg.springframework.osgi.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                      else if (throwable instanceof BundleException)
84                      {
85                          // Felix logs these when resolution fails, and it's useful to know why
86                          log.warn(message + ": " + throwable.getMessage());
87                      }
88                  }
89                  else
90                  {
91                      logInfoUnlessLame(message);
92                  }
93                  break;
94              default:
95                  log.debug("UNKNOWN[" + level + "]: " + message);
96          }
97      }
98  
99      protected void logInfoUnlessLame(String message)
100     {
101         if (message != null)
102         {
103             // I'm really, really sick of these stupid messages
104             for (String dumbBit : messagesToIgnore)
105                 if (message.contains(dumbBit))
106                     return;
107         }
108         log.info(message);
109     }
110 
111     public boolean isClassNotFoundsWeCareAbout(Throwable t)
112     {
113         if (t instanceof ClassNotFoundException)
114         {
115             String className = t.getMessage();
116             if (className.contains("***") && t.getCause() instanceof ClassNotFoundException)
117             {
118                 className = t.getCause().getMessage();
119             }
120             if (!className.startsWith("org.springframework") && !className.endsWith("BeanInfo") && !className.endsWith("Editor"))
121             {
122                 return true;
123             }
124         }
125         return false;
126     }
127 }