View Javadoc

1   package com.atlassian.johnson.context;
2   
3   import com.atlassian.johnson.Johnson;
4   
5   import javax.servlet.ServletContextEvent;
6   import javax.servlet.ServletContextListener;
7   
8   /**
9    * Initialises and terminates {@link Johnson} with the servlet container.
10   * <p/>
11   * In web environments, this is the preferred mechanism for managing Johnson's lifecycle, allowing it to be initialised
12   * and terminated in a thread-safe context. This listener should be registered before any others except, if applicable,
13   * those that setup logging.
14   * <p/>
15   * To use this listener, add the following to web.xml:
16   * <pre>
17   * <code>&lt;listener&gt;
18   *     &lt;listener-class&gt;com.atlassian.johnson.context.JohnsonContextListener&lt;/listener-class&gt;
19   * &lt;/listener&gt;
20   * </code>
21   * </pre>
22   *
23   * @since 2.0
24   */
25  public class JohnsonContextListener implements ServletContextListener
26  {
27      /**
28       * Terminates {@link Johnson}.
29       * 
30       * @param event the context event
31       * @see Johnson#terminate(javax.servlet.ServletContext)
32       */
33      public void contextDestroyed(ServletContextEvent event)
34      {
35          Johnson.terminate(event.getServletContext());
36      }
37  
38      /**
39       * Initialises {@link Johnson}.
40       *
41       * @param event the context event
42       * @see Johnson#initialize(javax.servlet.ServletContext)
43       */
44      public void contextInitialized(ServletContextEvent event)
45      {
46          Johnson.initialize(event.getServletContext());
47      }
48  }