View Javadoc

1   package com.atlassian.plugins.rest.module;
2   
3   import org.slf4j.Logger;
4   import org.slf4j.LoggerFactory;
5   import org.slf4j.bridge.SLF4JBridgeHandler;
6   
7   /**
8    * Utility class for safely installing and uninstalling the JUL-{@literal >}SLF4J bridge.
9    *
10   * @see SLF4JBridgeHandler
11   */
12  public final class Slf4jBridge {
13      /**
14       * Logger for this class.
15       */
16      private static final Logger log = LoggerFactory.getLogger(Slf4jBridge.class);
17  
18      /**
19       * Returns a Helper instance that can be used to install/uninstall the JUL-{@literal >}SLF4J bridge. This method tries to
20       * reflectively load the class <code>org.slf4j.bridge.SLF4JBridgeHandler</code> before using it, so it is safe to
21       * use in an OSGi environment where the class may or may not be present.
22       * <p>
23       * If the SLF4J bridge is found, then the helper can be used to install and uninstall the bridge in the thread
24       * context class loader (TCCL). If the bridge is not found then the helper's operations are no-ops.
25       *
26       * @return a Helper instance that can be used to install/uninstall the JUL-{@literal >}SLF4J bridge.
27       */
28      public static Helper createHelper() {
29          try {
30              Class.forName("org.slf4j.bridge.SLF4JBridgeHandler");
31  
32              return new BridgePresentHelper();
33          } catch (ClassNotFoundException e) {
34              return new BridgeMissingHelper();
35          }
36      }
37  
38      public interface Helper {
39          /**
40           * Installs the bridge.
41           */
42          void install();
43  
44          /**
45           * Un-installs the bridge.
46           */
47          void uninstall();
48  
49      }
50  
51      private static class BridgeMissingHelper implements Helper {
52          public void install() {
53              log.debug("Skipping installation of SLF4JBridgeHandler for {}. Have you provided jcl-over-slf4j.jar?", Thread.currentThread().getContextClassLoader());
54          }
55  
56          public void uninstall() {
57          }
58      }
59  
60      private static class BridgePresentHelper implements Helper {
61          public void install() {
62              log.debug("Installing SLF4JBridgeHandler for {}.", Thread.currentThread().getContextClassLoader());
63              SLF4JBridgeHandler.install();
64          }
65  
66          public void uninstall() {
67              SLF4JBridgeHandler.uninstall();
68              log.debug("Uninstalled SLF4JBridgeHandler for {}.", Thread.currentThread().getContextClassLoader());
69          }
70      }
71  }