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.descriptors.AbstractModuleDescriptor;
10  
11  /**
12   * Provides a way for plugins to declare {@link ServletContextListener}s so they can be notified when the 
13   * {@link javax.servlet.ServletContext} is created for the plugin.  Implementors need to extend this class and implement the
14   * {#link autowireObject} method.
15   *
16   * @since 2.1.0
17   */
18  public abstract class ServletContextListenerModuleDescriptor extends AbstractModuleDescriptor<ServletContextListener>
19  {
20      protected static final Log log = LogFactory.getLog(ServletContextListenerModuleDescriptor.class);
21  
22      @Override
23      public ServletContextListener getModule()
24      {
25          ServletContextListener obj = null;
26          try
27          {
28              // Give the plugin a go first
29              if (plugin instanceof AutowireCapablePlugin)
30                  obj = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
31              else
32              {
33                  obj = getModuleClass().newInstance();
34                  autowireObject(obj);
35              }
36          }
37          catch (InstantiationException e)
38          {
39              log.error("Error instantiating: " + getModuleClass(), e);
40          }
41          catch (IllegalAccessException e)
42          {
43              log.error("Error accessing: " + getModuleClass(), e);
44          }
45          return obj;
46      }
47  
48      /**
49       * Autowire an object. Implement this in your IoC framework or simply do nothing.
50       */
51      protected abstract void autowireObject(Object obj);
52  }