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<HttpServlet> implements StateAware
16 {
17 private static final Log log = LogFactory.getLog(ServletModuleDescriptor.class);
18 List<String> paths;
19 private Map<String,String> 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<String>(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<String,String>();
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 HttpServlet getModule()
59 {
60 HttpServlet obj = null;
61 try
62 {
63
64 if (plugin instanceof AutowireCapablePlugin)
65 obj = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
66 else
67 {
68 obj = 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
84
85
86 public HttpServlet getServlet()
87 {
88 return getModule();
89 }
90
91 public List<String> getPaths()
92 {
93 return paths;
94 }
95
96 public Map<String,String> getInitParams()
97 {
98 return initParams;
99 }
100
101
102
103
104 protected abstract void autowireObject(Object obj);
105
106
107
108
109 protected abstract ServletModuleManager getServletModuleManager();
110 }