View Javadoc

1   package com.atlassian.plugin.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.servlet.descriptors.BaseServletModuleDescriptor;
14  import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
15  import com.atlassian.plugin.servlet.util.ClassLoaderStack;
16  
17  /**
18   * We are wrapping the plugins servlet in another servlet so that we can set some things up before
19   * the plugins servlet is called. Currently we do the following:
20   *      <ul>
21   *        <li>the Threads classloader to the plugins classloader)</li>
22   *        <li>wrap the request so that path info is right for the servlets</li>
23   *      </ul>
24   */
25  public class DelegatingPluginServlet extends HttpServlet
26  {
27      private final ServletModuleDescriptor descriptor;
28  
29      private final HttpServlet servlet;
30  
31      public DelegatingPluginServlet(ServletModuleDescriptor descriptor)
32      {
33          this.descriptor = descriptor;
34          this.servlet = descriptor.getModule();
35      }
36  
37      public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
38      {
39          ClassLoaderStack.push(descriptor.getPlugin().getClassLoader());
40          try
41          {
42              servlet.service(new PluginHttpRequestWrapper(req, descriptor), res);
43          }
44          finally
45          {
46              ClassLoaderStack.pop();
47          }
48      }
49  
50      public void init(ServletConfig config) throws ServletException
51      {
52          ClassLoaderStack.push(descriptor.getPlugin().getClassLoader());
53          try
54          {
55              servlet.init(config);
56          }
57          finally
58          {
59              ClassLoaderStack.pop();
60          }
61      }
62  
63      public void destroy()
64      {
65          ClassLoaderStack.push(descriptor.getPlugin().getClassLoader());
66          try
67          {
68              servlet.destroy();
69          }
70          finally
71          {
72              ClassLoaderStack.pop();
73          }
74      }
75  
76      public boolean equals(Object obj)
77      {
78          return servlet.equals(obj);
79      }
80  
81      public String getInitParameter(String name)
82      {
83          return servlet.getInitParameter(name);
84      }
85  
86      public Enumeration getInitParameterNames()
87      {
88          return servlet.getInitParameterNames();
89      }
90  
91      public ServletConfig getServletConfig()
92      {
93          return servlet.getServletConfig();
94      }
95  
96      public ServletContext getServletContext()
97      {
98          return servlet.getServletContext();
99      }
100 
101     public String getServletInfo()
102     {
103         return servlet.getServletInfo();
104     }
105 
106     public String getServletName()
107     {
108         return servlet.getServletName();
109     }
110 
111     public int hashCode()
112     {
113         return servlet.hashCode();
114     }
115 
116     public void init() throws ServletException
117     {
118         servlet.init();
119     }
120 
121     public void log(String message, Throwable t)
122     {
123         servlet.log(message, t);
124     }
125 
126     public void log(String msg)
127     {
128         servlet.log(msg);
129     }
130 
131     public String toString()
132     {
133         return servlet.toString();
134     }
135 
136     public BaseServletModuleDescriptor<?> getDescriptor()
137     {
138         return descriptor;
139     }
140 }