/atlassian-plugins-servlet/src/main/java/com/atlassian/plugin/servlet/PluginHttpSessionWrapper.java

https://bitbucket.org/purewind/atlassian-plugins · Java · 133 lines · 91 code · 21 blank · 21 comment · 1 complexity · d1430d5e469415256f7e9e2cee0deb65 MD5 · raw file

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