1 package com.atlassian.plugin.descriptors.servlet;
2
3 import java.io.IOException;
4 import java.util.Enumeration;
5
6 import javax.servlet.ServletConfig;
7 import javax.servlet.ServletContext;
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import com.atlassian.plugin.Plugin;
14 import com.atlassian.plugin.impl.DynamicPlugin;
15
16
17
18
19
20
21
22
23
24 public class DelegatingPluginServlet extends HttpServlet
25 {
26 private final ServletModuleDescriptor descriptor;
27
28 private final HttpServlet servlet;
29
30 public DelegatingPluginServlet(ServletModuleDescriptor descriptor)
31 {
32 this.descriptor = descriptor;
33 this.servlet = descriptor.getServlet();
34 }
35
36 private ClassLoader replaceThreadClassLoaderWithPluginClassLoader(Plugin plugin)
37 {
38 ClassLoader startingClassLoader = Thread.currentThread().getContextClassLoader();
39 if (plugin.isDynamicallyLoaded())
40 {
41 Thread.currentThread().setContextClassLoader(plugin.getClassLoader());
42 }
43 return startingClassLoader;
44 }
45
46 public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
47 {
48 ClassLoader startingClassLoader = replaceThreadClassLoaderWithPluginClassLoader(descriptor.getPlugin());
49 servlet.service(new PluginHttpRequestWrapper(req, descriptor), res);
50 Thread.currentThread().setContextClassLoader(startingClassLoader);
51 }
52
53 public void init(ServletConfig config) throws ServletException
54 {
55 ClassLoader startingClassLoader = replaceThreadClassLoaderWithPluginClassLoader(descriptor.getPlugin());
56 servlet.init(config);
57 Thread.currentThread().setContextClassLoader(startingClassLoader);
58 }
59
60 public void destroy()
61 {
62 servlet.destroy();
63 }
64
65 public boolean equals(Object obj)
66 {
67 return servlet.equals(obj);
68 }
69
70 public String getInitParameter(String name)
71 {
72 return servlet.getInitParameter(name);
73 }
74
75 public Enumeration getInitParameterNames()
76 {
77 return servlet.getInitParameterNames();
78 }
79
80 public ServletConfig getServletConfig()
81 {
82 return servlet.getServletConfig();
83 }
84
85 public ServletContext getServletContext()
86 {
87 return servlet.getServletContext();
88 }
89
90 public String getServletInfo()
91 {
92 return servlet.getServletInfo();
93 }
94
95 public String getServletName()
96 {
97 return servlet.getServletName();
98 }
99
100 public int hashCode()
101 {
102 return servlet.hashCode();
103 }
104
105 public void init() throws ServletException
106 {
107 servlet.init();
108 }
109
110 public void log(String message, Throwable t)
111 {
112 servlet.log(message, t);
113 }
114
115 public void log(String msg)
116 {
117 servlet.log(msg);
118 }
119
120 public String toString()
121 {
122 return servlet.toString();
123 }
124
125 public ServletModuleDescriptor getDescriptor() {
126 return descriptor;
127 }
128 }