View Javadoc

1   package com.atlassian.plugin.servlet.util;
2   
3   import com.atlassian.plugin.hostcontainer.HostContainer;
4   
5   import javax.servlet.ServletContext;
6   
7   /**
8    * Provides static access to a {@link com.atlassian.plugin.hostcontainer.HostContainer} instance.  Requires initialisation before first use.
9    *
10   * @since 2.2.0
11   */
12  public class ServletContextHostContainerAccessor
13  {
14      private static final String HOST_CONTAINER_KEY = ServletContextHostContainerAccessor.class.getPackage()+".hostcontainer";
15  
16      /**
17       * Gets the host container for instance or thread
18       *
19       * @param servletContext the servlet context to look up the host container in
20       * @return The host container instance
21       * @throws IllegalStateException If it hasn't been initialised yet
22       */
23      public static HostContainer getHostContainer(ServletContext servletContext) throws IllegalStateException
24      {
25          return (HostContainer) servletContext.getAttribute(HOST_CONTAINER_KEY);
26      }
27  
28      /**
29       * Sets the implementation of the host container accessor
30       *
31       * @param servletContext the servlet context to set the container in
32       * @param hostContainer the implementation to set
33       */
34      public static void setHostContainer(ServletContext servletContext, HostContainer hostContainer)
35      {
36          servletContext.setAttribute(HOST_CONTAINER_KEY, hostContainer);
37      }
38  
39  }