View Javadoc

1   package com.atlassian.plugin.descriptors.servlet;
2   
3   import com.atlassian.plugin.descriptors.AbstractModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.StateAware;
7   import com.atlassian.plugin.AutowireCapablePlugin;
8   import org.dom4j.Element;
9   import org.apache.commons.logging.Log;
10  import org.apache.commons.logging.LogFactory;
11  
12  import javax.servlet.http.HttpServlet;
13  import java.util.*;
14  
15  public abstract class ServletModuleDescriptor extends AbstractModuleDescriptor implements StateAware
16  {
17      private static final Log log = LogFactory.getLog(ServletModuleDescriptor.class);
18      List paths;
19      private Map initParams;
20  
21      public void init(Plugin plugin, Element element) throws PluginParseException
22      {
23          super.init(plugin, element);
24  
25          List urlPatterns = element.elements("url-pattern");
26          paths = new ArrayList(urlPatterns.size());
27  
28          for (Iterator iterator = urlPatterns.iterator(); iterator.hasNext();)
29          {
30              Element urlPattern = (Element) iterator.next();
31              paths.add(urlPattern.getTextTrim());
32          }
33  
34          initParams = new HashMap();
35          List paramsList = element.elements("init-param");
36          for (Iterator i = paramsList.iterator(); i.hasNext();) {
37              Element initParamEl = (Element) i.next();
38              Element paramNameEl = initParamEl.element("param-name");
39              Element paramValueEl = initParamEl.element("param-value");
40              if (paramNameEl != null && paramValueEl != null) {
41                  initParams.put(paramNameEl.getTextTrim(), paramValueEl.getTextTrim());
42              } else {
43                  log.warn("Invalid init-param XML for servlet module: " + getCompleteKey());
44              }
45          }
46      }
47  
48      public void enabled()
49      {
50          getServletModuleManager().addModule(this);
51      }
52  
53      public void disabled()
54      {
55          getServletModuleManager().removeModule(this);
56      }
57  
58      public Object getModule()
59      {
60          HttpServlet obj = null;
61          try
62          {
63              // Give the plugin a go first
64              if (plugin instanceof AutowireCapablePlugin)
65                  obj = (HttpServlet) ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
66              else
67              {
68                  obj = (HttpServlet) getModuleClass().newInstance();
69                  autowireObject(obj);
70              }
71          }
72          catch (InstantiationException e)
73          {
74              log.error("Error instantiating: " + getModuleClass(), e);
75          }
76          catch (IllegalAccessException e)
77          {
78              log.error("Error accessing: " + getModuleClass(), e);
79          }
80          return obj;
81      }
82  
83      public HttpServlet getServlet()
84      {
85          return (HttpServlet) getModule();
86      }
87  
88      public List getPaths()
89      {
90          return paths;
91      }
92  
93      public Map getInitParams()
94      {
95          return initParams;
96      }
97  
98      /**
99       * Autowire an object. Implement this in your IoC framework or simply do nothing.
100      */
101     protected abstract void autowireObject(Object obj);
102 
103     /**
104      * Retrieve the ServletModuleManager class from your container framework.
105      */
106     protected abstract ServletModuleManager getServletModuleManager();
107 }