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