View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.servlet.descriptors.ServletModuleDescriptor;
4   import com.atlassian.plugin.servlet.util.ClassLoaderStack;
5   
6   import java.io.IOException;
7   import java.util.Enumeration;
8   
9   import javax.servlet.ServletConfig;
10  import javax.servlet.ServletContext;
11  import javax.servlet.ServletException;
12  import javax.servlet.http.HttpServlet;
13  import javax.servlet.http.HttpServletRequest;
14  import javax.servlet.http.HttpServletResponse;
15  
16  /**
17   * We are wrapping the plugins servlet in another servlet so that we can set some things up before
18   * the plugins servlet is called. Currently we do the following:
19   *      <ul>
20   *        <li>the Threads classloader to the plugins classloader)</li>
21   *        <li>wrap the request so that path info is right for the servlets</li>
22   *      </ul>
23   */
24  public class DelegatingPluginServlet extends HttpServlet
25  {
26      private final ServletModuleDescriptor descriptor;
27  
28      private final HttpServlet servlet;
29  
30      public DelegatingPluginServlet(final ServletModuleDescriptor descriptor)
31      {
32          this.descriptor = descriptor;
33          servlet = descriptor.getModule();
34      }
35  
36      @Override
37      public void service(final HttpServletRequest req, final 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      @Override
51      public void init(final ServletConfig config) throws ServletException
52      {
53          ClassLoaderStack.push(descriptor.getPlugin().getClassLoader());
54          try
55          {
56              servlet.init(config);
57          }
58          finally
59          {
60              ClassLoaderStack.pop();
61          }
62      }
63  
64      @Override
65      public void destroy()
66      {
67          ClassLoaderStack.push(descriptor.getPlugin().getClassLoader());
68          try
69          {
70              servlet.destroy();
71          }
72          finally
73          {
74              ClassLoaderStack.pop();
75          }
76      }
77  
78      @Override
79      public boolean equals(final Object obj)
80      {
81          return servlet.equals(obj);
82      }
83  
84      @Override
85      public String getInitParameter(final String name)
86      {
87          return servlet.getInitParameter(name);
88      }
89  
90      @Override
91      public Enumeration<String> getInitParameterNames()
92      {
93          @SuppressWarnings("unchecked")
94          final Enumeration<String> initParameterNames = servlet.getInitParameterNames();
95          return initParameterNames;
96      }
97  
98      @Override
99      public ServletConfig getServletConfig()
100     {
101         return servlet.getServletConfig();
102     }
103 
104     @Override
105     public ServletContext getServletContext()
106     {
107         return servlet.getServletContext();
108     }
109 
110     @Override
111     public String getServletInfo()
112     {
113         return servlet.getServletInfo();
114     }
115 
116     @Override
117     public String getServletName()
118     {
119         return servlet.getServletName();
120     }
121 
122     @Override
123     public int hashCode()
124     {
125         return servlet.hashCode();
126     }
127 
128     @Override
129     public void init() throws ServletException
130     {
131         servlet.init();
132     }
133 
134     @Override
135     public void log(final String message, final Throwable t)
136     {
137         servlet.log(message, t);
138     }
139 
140     @Override
141     public void log(final String msg)
142     {
143         servlet.log(msg);
144     }
145 
146     @Override
147     public String toString()
148     {
149         return servlet.toString();
150     }
151 
152     public ServletModuleDescriptor getModuleDescriptor()
153     {
154         return descriptor;
155     }
156 
157     HttpServlet getDelegatingServlet()
158     {
159         return servlet;
160     }
161 }