View Javadoc

1   package com.atlassian.plugin.servlet.descriptors;
2   
3   import javax.servlet.ServletContextListener;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   import com.atlassian.plugin.AutowireCapablePlugin;
9   import com.atlassian.plugin.hostcontainer.HostContainer;
10  import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
11  
12  /**
13   * Provides a way for plugins to declare {@link ServletContextListener}s so they can be notified when the 
14   * {@link javax.servlet.ServletContext} is created for the plugin.  Implementors need to extend this class and implement the
15   * {#link autowireObject} method.
16   *
17   * @since 2.1.0
18   */
19  public class ServletContextListenerModuleDescriptor extends AbstractModuleDescriptor<ServletContextListener>
20  {
21      protected static final Log log = LogFactory.getLog(ServletContextListenerModuleDescriptor.class);
22  
23      protected final HostContainer hostContainer;
24  
25      /**
26       * @deprecated Since 2.2.0, don't extend and use {@link #ServletContextListenerModuleDescriptor(com.atlassian.plugin.hostcontainer.HostContainer)} instead
27       */
28      @Deprecated
29      public ServletContextListenerModuleDescriptor()
30      {
31          this(null);
32      }
33  
34      /**
35       * Creates a descriptor that uses a module factory to create instances
36       *
37       * @param hostContainer The module factory
38       * @since 2.2.0
39       */
40      public ServletContextListenerModuleDescriptor(HostContainer hostContainer)
41      {
42          this.hostContainer = hostContainer;
43      }
44  
45      @Override
46      public ServletContextListener getModule()
47      {
48          ServletContextListener obj = null;
49          try
50          {
51              // Give the plugin a go first
52              if (plugin instanceof AutowireCapablePlugin)
53                  obj = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
54              else
55              {
56                  if (hostContainer != null)
57                  {
58                      obj = hostContainer.create(getModuleClass());
59                  }
60                  else
61                  {
62                      obj = getModuleClass().newInstance();
63                      autowireObject(obj);
64                  }
65              }
66          }
67          catch (InstantiationException e)
68          {
69              log.error("Error instantiating: " + getModuleClass(), e);
70          }
71          catch (IllegalAccessException e)
72          {
73              log.error("Error accessing: " + getModuleClass(), e);
74          }
75          return obj;
76      }
77  
78      /**
79       * @deprecated Since 2.2.0, don't extend and use a {@link com.atlassian.plugin.hostcontainer.HostContainer} instead
80       */
81      @Deprecated
82      protected void autowireObject(Object obj)
83      {
84          throw new UnsupportedOperationException("This method must be overridden if a HostContainer is not used");
85      }
86  
87  }