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.AutowireCapablePlugin;
10 import com.atlassian.plugin.Plugin;
11 import com.atlassian.plugin.PluginParseException;
12 import com.atlassian.plugin.StateAware;
13 import com.atlassian.plugin.servlet.ServletModuleManager;
14 import com.atlassian.plugin.servlet.filter.FilterLocation;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public abstract class ServletFilterModuleDescriptor extends BaseServletModuleDescriptor<Filter> implements StateAware
31 {
32 static final String DEFAULT_LOCATION = FilterLocation.bottom.name();
33 static final String DEFAULT_WEIGHT = "100";
34
35 private FilterLocation location;
36 private int weight;
37
38 public static final Comparator<ServletFilterModuleDescriptor> byWeight = new Comparator<ServletFilterModuleDescriptor>()
39 {
40 public int compare(ServletFilterModuleDescriptor lhs, ServletFilterModuleDescriptor rhs)
41 {
42 return Integer.valueOf(lhs.getWeight()).compareTo(rhs.getWeight());
43 }
44 };
45
46 public void init(Plugin plugin, Element element) throws PluginParseException
47 {
48 super.init(plugin, element);
49 location = FilterLocation.valueOf(element.attributeValue("location", DEFAULT_LOCATION));
50 weight = Integer.valueOf(element.attributeValue("weight", DEFAULT_WEIGHT));
51 }
52
53 public void enabled()
54 {
55 super.enabled();
56 getServletModuleManager().addFilterModule(this);
57 }
58
59 public void disabled()
60 {
61 getServletModuleManager().removeFilterModule(this);
62 super.disabled();
63 }
64
65 @Override
66 public Filter getModule()
67 {
68 Filter filter = null;
69 try
70 {
71
72 if (plugin instanceof AutowireCapablePlugin)
73 filter = ((AutowireCapablePlugin)plugin).autowire(getModuleClass());
74 else
75 {
76 filter = getModuleClass().newInstance();
77 autowireObject(filter);
78 }
79 }
80 catch (InstantiationException e)
81 {
82 log.error("Error instantiating: " + getModuleClass(), e);
83 }
84 catch (IllegalAccessException e)
85 {
86 log.error("Error accessing: " + getModuleClass(), e);
87 }
88 return filter;
89 }
90
91 public FilterLocation getLocation()
92 {
93 return location;
94 }
95
96 public int getWeight()
97 {
98 return weight;
99 }
100
101
102
103
104 protected abstract void autowireObject(Object obj);
105
106
107
108
109 protected abstract ServletModuleManager getServletModuleManager();
110 }