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 javax.servlet.ServletContext;
8   import javax.servlet.http.HttpSession;
9   import java.util.Enumeration;
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      private HttpSession delegate;
20      private static final Logger log = LoggerFactory.getLogger(PluginHttpSessionWrapper.class);
21  
22      public PluginHttpSessionWrapper(final HttpSession session) {
23          this.delegate = session;
24      }
25  
26      public Object getAttribute(final String name) {
27          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
28          //  getAttribute() call. See PLUG-515.
29          ClassLoader classLoader = ClassLoaderStack.pop();
30          try {
31              if (log.isDebugEnabled()) {
32                  log.debug("getAttribute('" + name + "') Popping ClassLoader: " + classLoader + " .New ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
33              }
34              return delegate.getAttribute(name);
35          } finally {
36              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
37              ClassLoaderStack.push(classLoader);
38          }
39      }
40  
41      public void setAttribute(final String name, final Object value) {
42          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
43          // method call. See PLUG-515.
44          ClassLoader classLoader = ClassLoaderStack.pop();
45          try {
46              delegate.setAttribute(name, value);
47          } finally {
48              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
49              ClassLoaderStack.push(classLoader);
50          }
51      }
52  
53      public Object getValue(final String name) {
54          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
55          // method call. See PLUG-515.
56          ClassLoader classLoader = ClassLoaderStack.pop();
57          try {
58              //noinspection deprecation
59              return delegate.getValue(name);
60          } finally {
61              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
62              ClassLoaderStack.push(classLoader);
63          }
64      }
65  
66      public void putValue(final String name, final Object value) {
67          // Trick WLS by putting the WebAppClassLoader back into this thread's ContextClassLoader for the duration of the
68          // method call. See PLUG-515.
69          ClassLoader classLoader = ClassLoaderStack.pop();
70          try {
71              //noinspection deprecation
72              delegate.putValue(name, value);
73          } finally {
74              // Reset to the Plugins ClassLoader and let OSGi continue to do its ClassLoader voodoo.
75              ClassLoaderStack.push(classLoader);
76          }
77      }
78  
79      public long getCreationTime() {
80          return delegate.getCreationTime();
81      }
82  
83      public String getId() {
84          return delegate.getId();
85      }
86  
87      public long getLastAccessedTime() {
88          return delegate.getLastAccessedTime();
89      }
90  
91      public ServletContext getServletContext() {
92          return delegate.getServletContext();
93      }
94  
95      public void setMaxInactiveInterval(final int interval) {
96          delegate.setMaxInactiveInterval(interval);
97      }
98  
99      public int getMaxInactiveInterval() {
100         return delegate.getMaxInactiveInterval();
101     }
102 
103     @SuppressWarnings({"deprecation"})
104     public javax.servlet.http.HttpSessionContext getSessionContext() {
105         return delegate.getSessionContext();
106     }
107 
108     public Enumeration getAttributeNames() {
109         return delegate.getAttributeNames();
110     }
111 
112     @SuppressWarnings({"deprecation"})
113     public String[] getValueNames() {
114         return delegate.getValueNames();
115     }
116 
117     public void removeAttribute(final String name) {
118         delegate.removeAttribute(name);
119     }
120 
121     @SuppressWarnings({"deprecation"})
122     public void removeValue(final String name) {
123         delegate.removeValue(name);
124     }
125 
126     public void invalidate() {
127         delegate.invalidate();
128     }
129 
130     public boolean isNew() {
131         return delegate.isNew();
132     }
133 }