1   package com.atlassian.plugin.osgi.container.felix;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.felix.framework.Logger;
5   import org.apache.felix.framework.util.FelixConstants;
6   import org.apache.felix.moduleloader.ResourceNotFoundException;
7   import org.osgi.framework.BundleException;
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 Log 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(Log 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                  log.debug(message);
51                  break;
52              case LOG_ERROR:
53                  if (throwable != null) {
54                      if ((throwable instanceof BundleException) &&
55                              (((BundleException) throwable).getNestedException() != null)) {
56                          throwable = ((BundleException) throwable).getNestedException();
57                      }
58                      log.error(message, throwable);
59                  } else
60                      log.error(message);
61                  break;
62              case LOG_INFO:
63                  logInfoUnlessLame(message);
64                  break;
65              case LOG_WARNING:
66                  // Handles special class loader errors from felix that have quite useful information
67                  if (throwable != null)
68                  {
69                      if (throwable instanceof ClassNotFoundException && isClassNotFoundsWeCareAbout(throwable))
70                      {
71                          log.debug("Class not found in bundle: " + message);
72                      }
73                      else if (throwable instanceof ResourceNotFoundException)
74                      {
75                          log.trace("Resource not found in bundle: " + message);
76                      }
77                  }
78                  else
79                  {
80                      logInfoUnlessLame(message);
81                  }
82                  break;
83              default:
84                  log.debug("UNKNOWN[" + level + "]: " + message);
85          }
86      }
87  
88      protected void logInfoUnlessLame(String message)
89      {
90          if (message != null)
91          {
92              // I'm really, really sick of these stupid messages
93              for (String dumbBit : messagesToIgnore)
94                  if (message.contains(dumbBit))
95                      return;
96          }
97          log.info(message);
98      }
99  
100     public boolean isClassNotFoundsWeCareAbout(Throwable t)
101     {
102         if (t instanceof ClassNotFoundException)
103         {
104             String className = t.getMessage();
105             if (className.contains("***") && t.getCause() instanceof ClassNotFoundException)
106             {
107                 className = t.getCause().getMessage();
108             }
109             if (!className.startsWith("org.springframework") && !className.endsWith("BeanInfo") && !className.endsWith("Editor"))
110             {
111                 return true;
112             }
113         }
114         return false;
115     }
116 }