View Javadoc

1   package com.atlassian.plugin.servlet;
2   
3   import com.atlassian.plugin.servlet.util.ClassLoaderStack;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.util.Enumeration;
8   import javax.servlet.ServletContext;
9   import javax.servlet.http.HttpSession;
10  
11  /**
12   * Wraps a HttpSession for consumption by OSGi plugins in order to workaround Weblogic problems caused by setting
13   * different Context ClassLoaders.
14   * See https://studio.atlassian.com/browse/PLUG-515
15   *
16   * @since 2.3.9
17   */
18  public class PluginHttpSessionWrapper implements HttpSession
19  {
20      private HttpSession delegate;
21      private static final Logger log = LoggerFactory.getLogger(PluginHttpSessionWrapper.class);
22  
23      public PluginHttpSessionWrapper(final HttpSession session)
24      {
25          this.delegate = session;
26      }
27  
28      public Object getAttribute(final String name)
29      {
30          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
31          //  getAttribute() call. See PLUG-515.
32          ClassLoader classLoader = ClassLoaderStack.pop();
33          try
34          {
35              if (log.isDebugEnabled())
36              {
37                  log.debug("getAttribute('" + name + "') Popping ClassLoader: " + classLoader + " .New ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
38              }
39              return delegate.getAttribute(name);
40          }
41          finally
42          {
43              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
44              ClassLoaderStack.push(classLoader);
45          }
46      }
47  
48      public void setAttribute(final String name, final Object value)
49      {
50          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
51          // method call. See PLUG-515.
52          ClassLoader classLoader = ClassLoaderStack.pop();
53          try
54          {
55              delegate.setAttribute(name, value);
56          }
57          finally
58          {
59              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
60              ClassLoaderStack.push(classLoader);
61          }
62      }
63  
64      public Object getValue(final String name)
65      {
66          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
67          // method call. See PLUG-515.
68          ClassLoader classLoader = ClassLoaderStack.pop();
69          try
70          {
71              //noinspection deprecation
72              return delegate.getValue(name);
73          }
74          finally
75          {
76              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
77              ClassLoaderStack.push(classLoader);
78          }
79      }
80  
81      public void putValue(final String name, final Object value)
82      {
83          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
84          // method call. See PLUG-515.
85          ClassLoader classLoader = ClassLoaderStack.pop();
86          try
87          {
88              //noinspection deprecation
89              delegate.putValue(name, value);
90          }
91          finally
92          {
93              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
94              ClassLoaderStack.push(classLoader);
95          }
96      }
97      
98      public long getCreationTime()
99      {
100         return delegate.getCreationTime();
101     }
102 
103     public String getId()
104     {
105         return delegate.getId();
106     }
107 
108     public long getLastAccessedTime()
109     {
110         return delegate.getLastAccessedTime();
111     }
112 
113     public ServletContext getServletContext()
114     {
115         return delegate.getServletContext();
116     }
117 
118     public void setMaxInactiveInterval(final int interval)
119     {
120         delegate.setMaxInactiveInterval(interval);
121     }
122 
123     public int getMaxInactiveInterval()
124     {
125         return delegate.getMaxInactiveInterval();
126     }
127 
128     @SuppressWarnings ({ "deprecation" })
129     public javax.servlet.http.HttpSessionContext getSessionContext()
130     {
131         return delegate.getSessionContext();
132     }
133 
134     public Enumeration getAttributeNames()
135     {
136         return delegate.getAttributeNames();
137     }
138 
139     @SuppressWarnings ({ "deprecation" })
140     public String[] getValueNames()
141     {
142         return delegate.getValueNames();
143     }
144 
145     public void removeAttribute(final String name)
146     {
147         delegate.removeAttribute(name);
148     }
149 
150     @SuppressWarnings ({ "deprecation" })
151     public void removeValue(final String name)
152     {
153         delegate.removeValue(name);
154     }
155 
156     public void invalidate()
157     {
158         delegate.invalidate();
159     }
160 
161     public boolean isNew()
162     {
163         return delegate.isNew();
164     }
165 }