1 package com.atlassian.plugin.descriptors.servlet;
2
3 import java.io.InputStream;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.Enumeration;
9 import java.util.HashSet;
10 import java.util.Set;
11 import java.util.concurrent.ConcurrentHashMap;
12 import java.util.concurrent.ConcurrentMap;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.InvocationTargetException;
15
16 import javax.servlet.RequestDispatcher;
17 import javax.servlet.Servlet;
18 import javax.servlet.ServletContext;
19 import javax.servlet.ServletException;
20
21
22
23
24
25 public class PluginServletContextWrapper implements ServletContext
26 {
27 private final ServletModuleDescriptor descriptor;
28 private final ServletContext context;
29 private final ConcurrentMap attributes;
30 private final Method methodGetContextPath;
31
32 public PluginServletContextWrapper(ServletModuleDescriptor descriptor, ServletContext context)
33 {
34 Method tmpMethod = null;
35 this.descriptor = descriptor;
36 this.context = context;
37 this.attributes = new ConcurrentHashMap();
38
39 Class cls = context.getClass();
40 try
41 {
42 tmpMethod = cls.getMethod("getContextPath", new Class[0]);
43 } catch (NoSuchMethodException e)
44 {
45
46 }
47 methodGetContextPath = tmpMethod;
48 }
49
50
51
52
53
54
55
56
57
58 public Object getAttribute(String name)
59 {
60 Object attr = attributes.get(name);
61 if (attr == null)
62 attr = context.getAttribute(name);
63
64 return attr;
65 }
66
67
68
69
70
71 public Enumeration getAttributeNames()
72 {
73 Collection names = new HashSet();
74 names.addAll(attributes.keySet());
75 names.addAll(Collections.list(context.getAttributeNames()));
76 return Collections.enumeration(names);
77 }
78
79
80
81
82
83 public void removeAttribute(String name)
84 {
85 attributes.remove(name);
86 }
87
88
89
90
91
92
93
94
95
96
97 public void setAttribute(String name, Object object)
98 {
99 attributes.put(name, object);
100 }
101
102
103
104
105 public String getInitParameter(String name)
106 {
107 return (String) descriptor.getInitParams().get(name);
108 }
109
110
111
112
113
114 public Enumeration getInitParameterNames()
115 {
116 return Collections.enumeration(descriptor.getInitParams().keySet());
117 }
118
119
120
121
122
123 public URL getResource(String path) throws MalformedURLException
124 {
125 URL url = descriptor.getPlugin().getResource(path);
126 if (url == null)
127 {
128 url = context.getResource(path);
129 }
130 return url;
131 }
132
133
134
135
136
137 public InputStream getResourceAsStream(String path)
138 {
139 InputStream in = descriptor.getPlugin().getResourceAsStream(path);
140 if (in == null)
141 {
142 in = context.getResourceAsStream(path);
143 }
144 return in;
145 }
146
147
148
149
150 public ServletContext getContext(String uripath)
151 {
152 return null;
153 }
154
155
156
157 public String getContextPath() {
158
159
160 if (methodGetContextPath != null)
161 {
162 try
163 {
164 return (String) methodGetContextPath.invoke(context, new Object[0]);
165 } catch (IllegalAccessException e)
166 {
167 throw new RuntimeException("Cannot access this method", e);
168 } catch (InvocationTargetException e)
169 {
170 throw new RuntimeException("Unable to execute getContextPath()", e.getCause());
171 }
172 } else
173 {
174 throw new UnsupportedOperationException("This servlet context doesn't support 2.5 methods");
175 }
176
177 }
178
179 public int getMajorVersion()
180 {
181 return context.getMajorVersion();
182 }
183
184 public String getMimeType(String file)
185 {
186 return context.getMimeType(file);
187 }
188
189 public int getMinorVersion()
190 {
191 return context.getMinorVersion();
192 }
193
194 public RequestDispatcher getNamedDispatcher(String name)
195 {
196 return context.getNamedDispatcher(name);
197 }
198
199 public String getRealPath(String path)
200 {
201 return context.getRealPath(path);
202 }
203
204 public RequestDispatcher getRequestDispatcher(String path)
205 {
206 return context.getRequestDispatcher(path);
207 }
208
209 public Set getResourcePaths(String arg0)
210 {
211 return context.getResourcePaths(arg0);
212 }
213
214 public String getServerInfo()
215 {
216 return context.getServerInfo();
217 }
218
219 public Servlet getServlet(String name) throws ServletException
220 {
221 return context.getServlet(name);
222 }
223
224 public String getServletContextName()
225 {
226 return context.getServletContextName();
227 }
228
229 public Enumeration getServletNames()
230 {
231 return context.getServletNames();
232 }
233
234 public Enumeration getServlets()
235 {
236 return context.getServlets();
237 }
238
239 public void log(Exception exception, String msg)
240 {
241 context.log(exception, msg);
242 }
243
244 public void log(String message, Throwable throwable)
245 {
246 context.log(message, throwable);
247 }
248
249 public void log(String msg)
250 {
251 context.log(msg);
252 }
253
254 }