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