View Javadoc

1   package com.atlassian.plugin.servlet.descriptors;
2   
3   import com.atlassian.plugin.AutowireCapablePlugin;
4   import com.atlassian.plugin.StateAware;
5   import com.atlassian.plugin.hostcontainer.HostContainer;
6   import com.atlassian.plugin.servlet.ServletModuleManager;
7   
8   import javax.servlet.http.HttpServlet;
9   
10  /**
11   * A module descriptor that allows plugin developers to define servlets. Developers can define what urls the
12   * servlet should be serve by defining one or more <url-pattern> elements.
13   */
14  public class ServletModuleDescriptor<T extends HttpServlet> extends BaseServletModuleDescriptor<T> implements StateAware
15  {
16      private final ServletModuleManager servletModuleManager;
17      private final HostContainer hostContainer;
18  
19      /**
20       * @deprecated Since 2.2.0, don't extend and use {@link #ServletModuleDescriptor(com.atlassian.plugin.hostcontainer.HostContainer ,ServletModuleManager)} instead
21       */
22      @Deprecated
23      public ServletModuleDescriptor()
24      {
25          this(null, null);
26      }
27  
28      /**
29       * Creates a descriptor that uses a module factory to create instances
30       *
31       * @param hostContainer The module factory
32       * @since 2.2.0
33       */
34      public ServletModuleDescriptor(HostContainer hostContainer, ServletModuleManager servletModuleManager)
35      {
36          this.hostContainer = hostContainer;
37          this.servletModuleManager = servletModuleManager;
38      }
39  
40      public void enabled()
41      {
42          super.enabled();
43          getServletModuleManager().addServletModule(this);
44      }
45  
46      public void disabled()
47      {
48          getServletModuleManager().removeServletModule(this);
49          super.disabled();
50      }
51  
52      public T getModule()
53      {
54          T servlet = null;
55          try
56          {
57              // Give the plugin a go first
58              if (plugin instanceof AutowireCapablePlugin)
59              {
60                  servlet = ((AutowireCapablePlugin) plugin).autowire(getModuleClass());
61              }
62              else
63              {
64                  if (hostContainer != null)
65                  {
66                      servlet = hostContainer.create(getModuleClass());
67                  }
68                  else
69                  {
70                      servlet = getModuleClass().newInstance();
71                      autowireObject(servlet);
72                  }
73              }
74          }
75          catch (InstantiationException e)
76          {
77              log.error("Error instantiating: " + getModuleClass(), e);
78          }
79          catch (IllegalAccessException e)
80          {
81              log.error("Error accessing: " + getModuleClass(), e);
82          }
83          return servlet;
84      }
85  
86      /**
87       * @deprecated Since 2.0.0, use {@link #getModule}
88       */
89      public HttpServlet getServlet()
90      {
91          return getModule();
92      }
93  
94      /**
95       * @deprecated Since 2.2.0, don't extend and use a {@link com.atlassian.plugin.hostcontainer.HostContainer} instead
96       */
97      @Deprecated
98      protected void autowireObject(Object obj)
99      {
100         throw new UnsupportedOperationException("This method must be overridden if a HostContainer is not used");
101     }
102 
103     /**
104      * @deprecated Since 2.2.0, don't extend and use a {@link com.atlassian.plugin.hostcontainer.HostContainer} instead
105      */
106     @Deprecated
107     protected ServletModuleManager getServletModuleManager()
108     {
109         if (servletModuleManager == null)
110         {
111             throw new IllegalStateException("This method must be implemented if a HostContainer is not used");
112         }
113 
114         return servletModuleManager;
115     }
116 }