1 package com.atlassian.plugin.servlet.descriptors;
2
3 import java.util.Comparator;
4
5 import javax.servlet.Filter;
6
7 import org.dom4j.Element;
8
9 import com.atlassian.plugin.*;
10 import com.atlassian.plugin.hostcontainer.HostContainer;
11 import com.atlassian.plugin.servlet.ServletModuleManager;
12 import com.atlassian.plugin.servlet.filter.FilterLocation;
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class ServletFilterModuleDescriptor extends BaseServletModuleDescriptor<Filter> implements StateAware
36 {
37 static final String DEFAULT_LOCATION = FilterLocation.BEFORE_DISPATCH.name();
38 static final String DEFAULT_WEIGHT = "100";
39
40 private FilterLocation location;
41
42 private int weight;
43 private final ServletModuleManager servletModuleManager;
44 private final HostContainer hostContainer;
45
46
47
48
49 @Deprecated
50 public ServletFilterModuleDescriptor()
51 {
52 this(null, null);
53 }
54
55
56
57
58
59
60
61 public ServletFilterModuleDescriptor(HostContainer hostContainer, ServletModuleManager servletModuleManager)
62 {
63 this.hostContainer = hostContainer;
64 this.servletModuleManager = servletModuleManager;
65 }
66
67 public static final Comparator<ServletFilterModuleDescriptor> byWeight = new Comparator<ServletFilterModuleDescriptor>()
68 {
69 public int compare(ServletFilterModuleDescriptor lhs, ServletFilterModuleDescriptor rhs)
70 {
71 return Integer.valueOf(lhs.getWeight()).compareTo(rhs.getWeight());
72 }
73 };
74
75 public void init(Plugin plugin, Element element) throws PluginParseException
76 {
77 super.init(plugin, element);
78 try
79 {
80 location = FilterLocation.parse(element.attributeValue("location", DEFAULT_LOCATION));
81 weight = Integer.valueOf(element.attributeValue("weight", DEFAULT_WEIGHT));
82 }
83 catch (IllegalArgumentException ex)
84 {
85 throw new PluginParseException(ex);
86 }
87 }
88
89 public void enabled()
90 {
91 super.enabled();
92 getServletModuleManager().addFilterModule(this);
93 }
94
95 public void disabled()
96 {
97 getServletModuleManager().removeFilterModule(this);
98 super.disabled();
99 }
100
101 @Override
102 public Filter getModule()
103 {
104 Filter filter = null;
105 try
106 {
107
108 if (plugin instanceof AutowireCapablePlugin)
109 filter = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
110 else
111 {
112 if (hostContainer != null)
113 {
114 filter = hostContainer.create(getModuleClass());
115 }
116 else
117 {
118 filter = getModuleClass().newInstance();
119 autowireObject(filter);
120 }
121 }
122 }
123 catch (InstantiationException e)
124 {
125 log.error("Error instantiating: " + getModuleClass(), e);
126 }
127 catch (IllegalAccessException e)
128 {
129 log.error("Error accessing: " + getModuleClass(), e);
130 }
131 return filter;
132 }
133
134 public FilterLocation getLocation()
135 {
136 return location;
137 }
138
139 public int getWeight()
140 {
141 return weight;
142 }
143
144
145
146
147 @Deprecated
148 protected void autowireObject(Object obj)
149 {
150 throw new UnsupportedOperationException("This method must be overridden if a HostContainer is not used");
151 }
152
153
154
155
156 @Deprecated
157 protected ServletModuleManager getServletModuleManager()
158 {
159 if (servletModuleManager == null)
160 {
161 throw new IllegalStateException("This method must be implemented if a HostContainer is not used");
162 }
163
164 return servletModuleManager;
165 }
166
167 }