diff options
author | Raymond Auge | 2016-08-17 18:50:54 +0000 |
---|---|---|
committer | Raymond Auge | 2016-08-17 18:50:54 +0000 |
commit | f1a422d53e293b510be1bb2af8b879c6b1721d15 (patch) | |
tree | 9fa695b5413810c7b72c5f53f8404864c5c8f056 | |
parent | 19ece600df81cdd99be55c27216249a8aa2c7b63 (diff) | |
download | rt.equinox.bundles-f1a422d53e293b510be1bb2af8b879c6b1721d15.tar.gz rt.equinox.bundles-f1a422d53e293b510be1bb2af8b879c6b1721d15.tar.xz rt.equinox.bundles-f1a422d53e293b510be1bb2af8b879c6b1721d15.zip |
Revert "Bug 497435 - [http servlet] performance optimizations, CPU and memory"Y20160818-1000
This reverts commit 19ece600df81cdd99be55c27216249a8aa2c7b63.
8 files changed, 155 insertions, 276 deletions
diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java index 2a15d8a4a..9af775c60 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/ContextController.java @@ -15,8 +15,6 @@ import java.net.URI; import java.net.URISyntaxException; import java.security.AccessController; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.atomic.AtomicReference; import java.util.regex.Pattern; @@ -144,7 +142,7 @@ public class ContextController { this.trackingContext = trackingContextParam; this.consumingContext = consumingContext; - this.string = SIMPLE_NAME + '[' + serviceId + "][" + contextName + ", " + trackingContextParam.getBundle() + ']'; //$NON-NLS-1$ + this.string = getClass().getSimpleName() + '[' + serviceId + "][" + contextName + ", " + consumingContext.getBundle() + ']'; //$NON-NLS-1$ listenerServiceTracker = new ServiceTracker<EventListener, AtomicReference<ListenerRegistration>>( trackingContext, httpServiceRuntime.getListenerFilter(), @@ -1182,22 +1180,20 @@ public class ContextController { } private void flushActiveSessions() { - Collection<HttpSessionAdaptor> httpSessionAdaptors = - activeSessions.values(); - - Iterator<HttpSessionAdaptor> iterator = httpSessionAdaptors.iterator(); - - while (iterator.hasNext()) { - HttpSessionAdaptor httpSessionAdaptor = iterator.next(); - + Collection<HttpSessionAdaptor> currentActiveSessions; + synchronized (activeSessions) { + currentActiveSessions = new ArrayList<HttpSessionAdaptor>(activeSessions.values()); + activeSessions.clear(); + } + for (HttpSessionAdaptor httpSessionAdaptor : currentActiveSessions) { httpSessionAdaptor.invalidate(); - - iterator.remove(); } } public void removeActiveSession(HttpSession session) { - activeSessions.remove(session.getId()); + synchronized (activeSessions) { + activeSessions.remove(session); + } } public void fireSessionIdChanged(String oldSessionId) { @@ -1212,7 +1208,11 @@ public class ContextController { return; } - for (HttpSessionAdaptor httpSessionAdaptor : activeSessions.values()) { + Collection<HttpSessionAdaptor> currentActiveSessions; + synchronized (activeSessions) { + currentActiveSessions = new ArrayList<HttpSessionAdaptor>(activeSessions.values()); + } + for (HttpSessionAdaptor httpSessionAdaptor : currentActiveSessions) { HttpSessionEvent httpSessionEvent = new HttpSessionEvent(httpSessionAdaptor); for (javax.servlet.http.HttpSessionIdListener listener : listeners) { listener.sessionIdChanged(httpSessionEvent, oldSessionId); @@ -1222,35 +1222,22 @@ public class ContextController { public HttpSessionAdaptor getSessionAdaptor( HttpSession session, ServletContext servletContext) { - - String sessionId = session.getId(); - - HttpSessionAdaptor httpSessionAdaptor = activeSessions.get(sessionId); - - if (httpSessionAdaptor != null) { - return httpSessionAdaptor; - } - - httpSessionAdaptor = HttpSessionAdaptor.createHttpSessionAdaptor( - session, servletContext, this); - - HttpSessionAdaptor previousHttpSessionAdaptor = - activeSessions.putIfAbsent(sessionId, httpSessionAdaptor); - - if (previousHttpSessionAdaptor != null) { - return previousHttpSessionAdaptor; + boolean created = false; + HttpSessionAdaptor sessionAdaptor; + synchronized (activeSessions) { + sessionAdaptor = activeSessions.get(session); + if (sessionAdaptor == null) { + created = true; + sessionAdaptor = HttpSessionAdaptor.createHttpSessionAdaptor(session, servletContext, this); + activeSessions.put(session, sessionAdaptor); + } } - - HttpSessionEvent httpSessionEvent = new HttpSessionEvent( - httpSessionAdaptor); - - for (HttpSessionListener listener : eventListeners.get( - HttpSessionListener.class)) { - - listener.sessionCreated(httpSessionEvent); + if (created) { + for (HttpSessionListener listener : eventListeners.get(HttpSessionListener.class)) { + listener.sessionCreated(new HttpSessionEvent(sessionAdaptor)); + } } - - return httpSessionAdaptor; + return sessionAdaptor; } private void validate(String preValidationContextName, String preValidationContextPath) { @@ -1274,8 +1261,6 @@ public class ContextController { private static final String[] DISPATCHER = new String[] {DispatcherType.REQUEST.toString()}; - private static final String SIMPLE_NAME = ContextController.class.getSimpleName(); - private static final Pattern contextNamePattern = Pattern.compile("^([a-zA-Z_0-9\\-]+\\.)*[a-zA-Z_0-9\\-]+$"); //$NON-NLS-1$ private final Map<String, String> initParams; @@ -1287,7 +1272,7 @@ public class ContextController { private final Set<EndpointRegistration<?>> endpointRegistrations = new ConcurrentSkipListSet<EndpointRegistration<?>>(); private final EventListeners eventListeners = new EventListeners(); private final Set<FilterRegistration> filterRegistrations = new ConcurrentSkipListSet<FilterRegistration>(); - private final ConcurrentMap<String, HttpSessionAdaptor> activeSessions = new ConcurrentHashMap<String, HttpSessionAdaptor>(); + private final Map<HttpSession, HttpSessionAdaptor> activeSessions = new HashMap<HttpSession, HttpSessionAdaptor>(); private final HttpServiceRuntimeImpl httpServiceRuntime; private final Set<ListenerRegistration> listenerRegistrations = new HashSet<ListenerRegistration>(); diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java index 6e30ca04a..903166f77 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/context/DispatchTargets.java @@ -54,6 +54,8 @@ public class DispatchTargets { this.servletPath = (servletPath == null) ? Const.BLANK : servletPath; this.pathInfo = pathInfo; this.queryString = queryString; + + this.string = SIMPLE_NAME + '[' + contextController.getFullContextPath() + requestURI + (queryString != null ? '?' + queryString : "") + ", " + endpointRegistration.toString() + ']'; //$NON-NLS-1$ //$NON-NLS-2$ } public void addRequestParameters(HttpServletRequest request) { @@ -195,15 +197,7 @@ public class DispatchTargets { @Override public String toString() { - String value = string; - - if (value == null) { - value = SIMPLE_NAME + '[' + contextController.getFullContextPath() + requestURI + (queryString != null ? '?' + queryString : "") + ", " + endpointRegistration.toString() + ']'; //$NON-NLS-1$ - - string = value; - } - - return value; + return string; } private static Map<String, String[]> queryStringToParameterMap(String queryString) { @@ -272,6 +266,6 @@ public class DispatchTargets { private final String servletPath; private final String servletName; private final Map<String, Object> specialOverides = new ConcurrentHashMap<String, Object>(); - private String string; + private final String string; }
\ No newline at end of file diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/EndpointRegistration.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/EndpointRegistration.java index 2c8e87d80..067a04fc8 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/EndpointRegistration.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/EndpointRegistration.java @@ -168,19 +168,7 @@ public abstract class EndpointRegistration<D extends DTO> @Override public String toString() { - String toString = _toString; - - if (toString == null) { - toString = SIMPLE_NAME + '[' + getD().toString() + ']'; - - _toString = toString; - } - - return toString; + return getClass().getSimpleName() + '[' + getD().toString() + ']'; } - private static final String SIMPLE_NAME = - EndpointRegistration.class.getSimpleName(); - - private String _toString; } diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/Registration.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/Registration.java index 4e3e612f8..0df5d73a0 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/Registration.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/registration/Registration.java @@ -12,11 +12,6 @@ *******************************************************************************/ package org.eclipse.equinox.http.servlet.internal.registration; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; import org.osgi.dto.DTO; public abstract class Registration<T, D extends DTO> { @@ -24,82 +19,42 @@ public abstract class Registration<T, D extends DTO> { private final D d; private final T t; - protected final AtomicInteger referenceCount = new AtomicInteger(); + protected int referenceCount; public Registration(T t, D d) { this.t = t; this.d = d; } - public void addReference() { - readLock.lock(); - - try { - referenceCount.incrementAndGet(); - } - finally { - readLock.unlock(); - } + public synchronized void addReference() { + ++referenceCount; } - public void removeReference() { - readLock.lock(); - - try { - if (referenceCount.decrementAndGet() == 0 && destroyed) { - readLock.unlock(); - - writeLock.lock(); - - try { - condition.signalAll(); - } - finally { - writeLock.unlock(); - - readLock.lock(); - } - } - } - finally { - readLock.unlock(); + public synchronized void removeReference() { + --referenceCount; + if (referenceCount == 0) { + notifyAll(); } } - public void destroy() { + public synchronized void destroy() { boolean interrupted = false; - - writeLock.lock(); - - destroyed = true; - try { - while (referenceCount.get() != 0) { + while (referenceCount != 0) { try { - condition.await(); - } - catch (InterruptedException ie) { + (new Exception()).printStackTrace(); + wait(); + } catch (InterruptedException e) { + // wait until the servlet is inactive but save the interrupted status interrupted = true; } } - } - finally { - writeLock.unlock(); - - if (interrupted) { - Thread.currentThread().interrupt(); - } + } finally { + if (interrupted) + Thread.currentThread().interrupt(); //restore the interrupted state } } - private volatile boolean destroyed; - - private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); - - private final Lock readLock = readWriteLock.readLock(); - private final Lock writeLock = readWriteLock.writeLock(); - private final Condition condition = writeLock.newCondition(); - public D getD() { return d; } diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java index 6472681f1..30f03be2e 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpServletRequestWrapperImpl.java @@ -32,26 +32,24 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { private Map<String, Part> parts; private final Lock lock = new ReentrantLock(); - private static final Set<String> dispatcherAttributes = new HashSet<String>(); - - static { - dispatcherAttributes.add(RequestDispatcher.ERROR_EXCEPTION); - dispatcherAttributes.add(RequestDispatcher.ERROR_EXCEPTION_TYPE); - dispatcherAttributes.add(RequestDispatcher.ERROR_MESSAGE); - dispatcherAttributes.add(RequestDispatcher.ERROR_REQUEST_URI); - dispatcherAttributes.add(RequestDispatcher.ERROR_SERVLET_NAME); - dispatcherAttributes.add(RequestDispatcher.ERROR_STATUS_CODE); - dispatcherAttributes.add(RequestDispatcher.FORWARD_CONTEXT_PATH); - dispatcherAttributes.add(RequestDispatcher.FORWARD_PATH_INFO); - dispatcherAttributes.add(RequestDispatcher.FORWARD_QUERY_STRING); - dispatcherAttributes.add(RequestDispatcher.FORWARD_REQUEST_URI); - dispatcherAttributes.add(RequestDispatcher.FORWARD_SERVLET_PATH); - dispatcherAttributes.add(RequestDispatcher.INCLUDE_CONTEXT_PATH); - dispatcherAttributes.add(RequestDispatcher.INCLUDE_PATH_INFO); - dispatcherAttributes.add(RequestDispatcher.INCLUDE_QUERY_STRING); - dispatcherAttributes.add(RequestDispatcher.INCLUDE_REQUEST_URI); - dispatcherAttributes.add(RequestDispatcher.INCLUDE_SERVLET_PATH); - } + private static final String[] dispatcherAttributes = new String[] { + RequestDispatcher.ERROR_EXCEPTION, + RequestDispatcher.ERROR_EXCEPTION_TYPE, + RequestDispatcher.ERROR_MESSAGE, + RequestDispatcher.ERROR_REQUEST_URI, + RequestDispatcher.ERROR_SERVLET_NAME, + RequestDispatcher.ERROR_STATUS_CODE, + RequestDispatcher.FORWARD_CONTEXT_PATH, + RequestDispatcher.FORWARD_PATH_INFO, + RequestDispatcher.FORWARD_QUERY_STRING, + RequestDispatcher.FORWARD_REQUEST_URI, + RequestDispatcher.FORWARD_SERVLET_PATH, + RequestDispatcher.INCLUDE_CONTEXT_PATH, + RequestDispatcher.INCLUDE_PATH_INFO, + RequestDispatcher.INCLUDE_QUERY_STRING, + RequestDispatcher.INCLUDE_REQUEST_URI, + RequestDispatcher.INCLUDE_SERVLET_PATH + }; public static HttpServletRequestWrapperImpl findHttpRuntimeRequest( HttpServletRequest request) { @@ -89,13 +87,11 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { } public String getPathInfo() { - DispatchTargets currentDispatchTargets = dispatchTargets.peek(); - - if ((currentDispatchTargets.getServletName() != null) || - (currentDispatchTargets.getDispatcherType() == DispatcherType.INCLUDE)) { + if ((dispatchTargets.peek().getServletName() != null) || + (dispatchTargets.peek().getDispatcherType() == DispatcherType.INCLUDE)) { return this.dispatchTargets.get(0).getPathInfo(); } - return currentDispatchTargets.getPathInfo(); + return this.dispatchTargets.peek().getPathInfo(); } public DispatcherType getDispatcherType() { @@ -124,24 +120,20 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { @Override public String getQueryString() { - DispatchTargets currentDispatchTargets = dispatchTargets.peek(); - - if ((currentDispatchTargets.getServletName() != null) || - (currentDispatchTargets.getDispatcherType() == DispatcherType.INCLUDE)) { + if ((dispatchTargets.peek().getServletName() != null) || + (dispatchTargets.peek().getDispatcherType() == DispatcherType.INCLUDE)) { return request.getQueryString(); } - return currentDispatchTargets.getQueryString(); + return this.dispatchTargets.peek().getQueryString(); } @Override public String getRequestURI() { - DispatchTargets currentDispatchTargets = dispatchTargets.peek(); - - if ((currentDispatchTargets.getServletName() != null) || - (currentDispatchTargets.getDispatcherType() == DispatcherType.INCLUDE)) { + if ((dispatchTargets.peek().getServletName() != null) || + (dispatchTargets.peek().getDispatcherType() == DispatcherType.INCLUDE)) { return request.getRequestURI(); } - return currentDispatchTargets.getRequestURI(); + return this.dispatchTargets.peek().getRequestURI(); } public ServletContext getServletContext() { @@ -149,16 +141,14 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { } public String getServletPath() { - DispatchTargets currentDispatchTargets = dispatchTargets.peek(); - - if ((currentDispatchTargets.getServletName() != null) || - (currentDispatchTargets.getDispatcherType() == DispatcherType.INCLUDE)) { + if ((dispatchTargets.peek().getServletName() != null) || + (dispatchTargets.peek().getDispatcherType() == DispatcherType.INCLUDE)) { return this.dispatchTargets.get(0).getServletPath(); } - if (currentDispatchTargets.getServletPath().equals(Const.SLASH)) { + if (dispatchTargets.peek().getServletPath().equals(Const.SLASH)) { return Const.BLANK; } - return currentDispatchTargets.getServletPath(); + return this.dispatchTargets.peek().getServletPath(); } public String getContextPath() { @@ -167,20 +157,19 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { public Object getAttribute(String attributeName) { DispatchTargets current = dispatchTargets.peek(); - DispatcherType dispatcherType = current.getDispatcherType(); - boolean hasServletName = (current.getServletName() != null); + Map<String, Object> specialOverides = current.getSpecialOverides(); - if (dispatcherType == DispatcherType.ERROR) { - if (dispatcherAttributes.contains(attributeName) && + if (current.getDispatcherType() == DispatcherType.ERROR) { + if ((Arrays.binarySearch(dispatcherAttributes, attributeName) > -1) && !attributeName.startsWith("javax.servlet.error.")) { //$NON-NLS-1$ return null; } } - else if (dispatcherType == DispatcherType.INCLUDE) { + else if (current.getDispatcherType() == DispatcherType.INCLUDE) { if (attributeName.equals(RequestDispatcher.INCLUDE_CONTEXT_PATH)) { - if (hasServletName) { + if (current.getServletName() != null) { return null; } if (specialOverides.containsKey(RequestDispatcher.INCLUDE_CONTEXT_PATH)) { @@ -189,7 +178,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return current.getContextController().getContextPath(); } else if (attributeName.equals(RequestDispatcher.INCLUDE_PATH_INFO)) { - if (hasServletName) { + if (current.getServletName() != null) { return null; } if (specialOverides.containsKey(RequestDispatcher.INCLUDE_PATH_INFO)) { @@ -198,7 +187,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return current.getPathInfo(); } else if (attributeName.equals(RequestDispatcher.INCLUDE_QUERY_STRING)) { - if (hasServletName) { + if (current.getServletName() != null) { return null; } if (specialOverides.containsKey(RequestDispatcher.INCLUDE_QUERY_STRING)) { @@ -207,7 +196,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return current.getQueryString(); } else if (attributeName.equals(RequestDispatcher.INCLUDE_REQUEST_URI)) { - if (hasServletName) { + if (current.getServletName() != null) { return null; } if (specialOverides.containsKey(RequestDispatcher.INCLUDE_REQUEST_URI)) { @@ -216,7 +205,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return current.getRequestURI(); } else if (attributeName.equals(RequestDispatcher.INCLUDE_SERVLET_PATH)) { - if (hasServletName) { + if (current.getServletName() != null) { return null; } if (specialOverides.containsKey(RequestDispatcher.INCLUDE_SERVLET_PATH)) { @@ -225,49 +214,60 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { return current.getServletPath(); } - if (dispatcherAttributes.contains(attributeName)) { + if (Arrays.binarySearch(dispatcherAttributes, attributeName) > -1) { return null; } } - else if (dispatcherType == DispatcherType.FORWARD) { - if (hasServletName && attributeName.startsWith("javax.servlet.forward")) { - return null; - } - + else if (current.getDispatcherType() == DispatcherType.FORWARD) { DispatchTargets original = dispatchTargets.get(0); if (attributeName.equals(RequestDispatcher.FORWARD_CONTEXT_PATH)) { + if (current.getServletName() != null) { + return null; + } if (specialOverides.containsKey(RequestDispatcher.FORWARD_CONTEXT_PATH)) { return specialOverides.get(RequestDispatcher.FORWARD_CONTEXT_PATH); } return original.getContextController().getContextPath(); } else if (attributeName.equals(RequestDispatcher.FORWARD_PATH_INFO)) { + if (current.getServletName() != null) { + return null; + } if (specialOverides.containsKey(RequestDispatcher.FORWARD_PATH_INFO)) { return specialOverides.get(RequestDispatcher.FORWARD_PATH_INFO); } return original.getPathInfo(); } else if (attributeName.equals(RequestDispatcher.FORWARD_QUERY_STRING)) { + if (current.getServletName() != null) { + return null; + } if (specialOverides.containsKey(RequestDispatcher.FORWARD_QUERY_STRING)) { return specialOverides.get(RequestDispatcher.FORWARD_QUERY_STRING); } return original.getQueryString(); } else if (attributeName.equals(RequestDispatcher.FORWARD_REQUEST_URI)) { + if (current.getServletName() != null) { + return null; + } if (specialOverides.containsKey(RequestDispatcher.FORWARD_REQUEST_URI)) { return specialOverides.get(RequestDispatcher.FORWARD_REQUEST_URI); } return original.getRequestURI(); } else if (attributeName.equals(RequestDispatcher.FORWARD_SERVLET_PATH)) { + if (current.getServletName() != null) { + return null; + } if (specialOverides.containsKey(RequestDispatcher.FORWARD_SERVLET_PATH)) { return specialOverides.get(RequestDispatcher.FORWARD_SERVLET_PATH); } return original.getServletPath(); } - if (dispatcherAttributes.contains(attributeName)) { + if (Arrays.binarySearch(dispatcherAttributes, attributeName) > -1) { return null; } } @@ -276,14 +276,12 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { } public RequestDispatcher getRequestDispatcher(String path) { - DispatchTargets currentDispatchTarget = dispatchTargets.peek(); - ContextController contextController = - currentDispatchTarget.getContextController(); + this.dispatchTargets.peek().getContextController(); // support relative paths if (!path.startsWith(Const.SLASH)) { - path = currentDispatchTarget.getServletPath() + Const.SLASH + path; + path = this.dispatchTargets.peek().getServletPath() + Const.SLASH + path; } // if the path starts with the full context path strip it else if (path.startsWith(contextController.getFullContextPath())) { @@ -307,16 +305,20 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { } public HttpSession getSession() { - return getSession(true); + HttpSession session = request.getSession(); + if (session != null) { + return dispatchTargets.peek().getContextController().getSessionAdaptor( + session, dispatchTargets.peek().getServletRegistration().getT().getServletConfig().getServletContext()); + } + + return null; } public HttpSession getSession(boolean create) { HttpSession session = request.getSession(create); if (session != null) { - DispatchTargets currentDispatchTarget = dispatchTargets.peek(); - - return currentDispatchTarget.getContextController().getSessionAdaptor( - session, currentDispatchTarget.getServletRegistration().getT().getServletConfig().getServletContext()); + return dispatchTargets.peek().getContextController().getSessionAdaptor( + session, dispatchTargets.peek().getServletRegistration().getT().getServletConfig().getServletContext()); } return null; @@ -334,7 +336,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { } public void removeAttribute(String name) { - if (dispatcherAttributes.contains(name)) { + if (Arrays.binarySearch(dispatcherAttributes, name) > -1) { DispatchTargets current = dispatchTargets.peek(); current.getSpecialOverides().remove(name); @@ -343,9 +345,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { request.removeAttribute(name); } - DispatchTargets currentDispatchTarget = dispatchTargets.peek(); - - EventListeners eventListeners = currentDispatchTarget.getContextController().getEventListeners(); + EventListeners eventListeners = dispatchTargets.peek().getContextController().getEventListeners(); List<ServletRequestAttributeListener> listeners = eventListeners.get( ServletRequestAttributeListener.class); @@ -356,7 +356,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { ServletRequestAttributeEvent servletRequestAttributeEvent = new ServletRequestAttributeEvent( - currentDispatchTarget.getServletRegistration().getServletContext(), this, name, null); + dispatchTargets.peek().getServletRegistration().getServletContext(), this, name, null); for (ServletRequestAttributeListener servletRequestAttributeListener : listeners) { servletRequestAttributeListener.attributeRemoved( @@ -367,7 +367,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { public void setAttribute(String name, Object value) { boolean added = (request.getAttribute(name) == null); - if (dispatcherAttributes.contains(name)) { + if (Arrays.binarySearch(dispatcherAttributes, name) > -1) { DispatchTargets current = dispatchTargets.peek(); if (value == null) { @@ -381,9 +381,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { request.setAttribute(name, value); } - DispatchTargets currentDispatchTarget = dispatchTargets.peek(); - - EventListeners eventListeners = currentDispatchTarget.getContextController().getEventListeners(); + EventListeners eventListeners = dispatchTargets.peek().getContextController().getEventListeners(); List<ServletRequestAttributeListener> listeners = eventListeners.get( ServletRequestAttributeListener.class); @@ -394,7 +392,7 @@ public class HttpServletRequestWrapperImpl extends HttpServletRequestWrapper { ServletRequestAttributeEvent servletRequestAttributeEvent = new ServletRequestAttributeEvent( - currentDispatchTarget.getServletRegistration().getServletContext(), this, name, value); + dispatchTargets.peek().getServletRegistration().getServletContext(), this, name, value); for (ServletRequestAttributeListener servletRequestAttributeListener : listeners) { if (added) { diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpSessionAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpSessionAdaptor.java index 58ca6e53e..14212dd67 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpSessionAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/HttpSessionAdaptor.java @@ -14,7 +14,6 @@ package org.eclipse.equinox.http.servlet.internal.servlet; import java.io.Serializable; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; import javax.servlet.ServletContext; import javax.servlet.http.*; import org.eclipse.equinox.http.servlet.internal.context.ContextController; @@ -28,7 +27,7 @@ public class HttpSessionAdaptor implements HttpSession, Serializable { private static final long serialVersionUID = 4626167646903550760L; private static final String PARENT_SESSION_LISTENER_KEY = "org.eclipse.equinox.http.parent.session.listener"; //$NON-NLS-1$ - transient final Set<HttpSessionAdaptor> innerSessions = Collections.newSetFromMap(new ConcurrentHashMap<HttpSessionAdaptor, Boolean>()); + transient final Set<HttpSessionAdaptor> innerSessions = new HashSet<HttpSessionAdaptor>(); @Override public void valueBound(HttpSessionBindingEvent event) { // do nothing @@ -38,64 +37,42 @@ public class HttpSessionAdaptor implements HttpSession, Serializable { public void valueUnbound(HttpSessionBindingEvent event) { // Here we assume the unbound event is signifying the session is being invalidated. // Must invalidate the inner sessions - Iterator<HttpSessionAdaptor> iterator = innerSessions.iterator(); - - while (iterator.hasNext()) { - HttpSessionAdaptor innerSession = iterator.next(); - - iterator.remove(); - - ContextController contextController = - innerSession.getController(); - - EventListeners eventListeners = - contextController.getEventListeners(); - - List<HttpSessionListener> httpSessionListeners = - eventListeners.get(HttpSessionListener.class); - - if (!httpSessionListeners.isEmpty()) { - HttpSessionEvent httpSessionEvent = new HttpSessionEvent( - innerSession); - - for (HttpSessionListener listener : httpSessionListeners) { - try { - listener.sessionDestroyed(httpSessionEvent); - } - catch (IllegalStateException ise) { - // outer session is already invalidated - } - } - } - - contextController.removeActiveSession( - innerSession.getSession()); + Set<HttpSessionAdaptor> innerSessionsToInvalidate; + synchronized (innerSessions) { + // copy the sessions to invalidate and clear the set + innerSessionsToInvalidate = new HashSet<HttpSessionAdaptor>(innerSessions); + innerSessions.clear(); + } + for (HttpSessionAdaptor innerSession : innerSessionsToInvalidate) { + innerSession.invalidate(); } } static void addHttpSessionAdaptor(HttpSessionAdaptor innerSession) { - HttpSession httpSession = innerSession.getSession(); - ParentSessionListener parentListener; // need to have a global lock here because we must ensure that this is added only once - synchronized (httpSession) { - parentListener = (ParentSessionListener) httpSession.getAttribute(PARENT_SESSION_LISTENER_KEY); + synchronized (ParentSessionListener.class) { + parentListener = (ParentSessionListener) innerSession.getSession().getAttribute(PARENT_SESSION_LISTENER_KEY); if (parentListener == null) { parentListener = new ParentSessionListener(); - httpSession.setAttribute(PARENT_SESSION_LISTENER_KEY, parentListener); + innerSession.getSession().setAttribute(PARENT_SESSION_LISTENER_KEY, parentListener); } } - - parentListener.innerSessions.add(innerSession); + synchronized (parentListener.innerSessions) { + parentListener.innerSessions.add(innerSession); + } } static void removeHttpSessionAdaptor(HttpSessionAdaptor innerSession) { - HttpSession httpSession = innerSession.getSession(); - - ParentSessionListener parentListener = (ParentSessionListener) httpSession.getAttribute(PARENT_SESSION_LISTENER_KEY); - + ParentSessionListener parentListener; + // need to have a global lock here because we must ensure that this is added only once + synchronized (ParentSessionListener.class) { + parentListener = (ParentSessionListener) innerSession.getSession().getAttribute(PARENT_SESSION_LISTENER_KEY); + } if (parentListener != null) { - parentListener.innerSessions.remove(innerSession); + synchronized (parentListener.innerSessions) { + parentListener.innerSessions.remove(innerSession); + } } } } @@ -195,7 +172,7 @@ public class HttpSessionAdaptor implements HttpSession, Serializable { this.controller = controller; this.attributePrefix = "equinox.http." + controller.getContextName(); //$NON-NLS-1$ - this.string = SIMPLE_NAME + '[' + session.getId() + ", " + attributePrefix + ']'; //$NON-NLS-1$ + this.string = getClass().getSimpleName() + '[' + session.getId() + ", " + attributePrefix + ']'; //$NON-NLS-1$ } public ContextController getController() { @@ -340,8 +317,4 @@ public class HttpSessionAdaptor implements HttpSession, Serializable { public String toString() { return string; } - - private static final String SIMPLE_NAME = - HttpSessionAdaptor.class.getSimpleName(); - } diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/RequestDispatcherAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/RequestDispatcherAdaptor.java index c07d8189e..c3791adfd 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/RequestDispatcherAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/RequestDispatcherAdaptor.java @@ -21,8 +21,6 @@ import org.eclipse.equinox.http.servlet.internal.context.DispatchTargets; //This class unwraps the request so it can be processed by the underlying servlet container. public class RequestDispatcherAdaptor implements RequestDispatcher { - private static final String SIMPLE_NAME = RequestDispatcherAdaptor.class.getSimpleName(); - private final DispatchTargets dispatchTargets; private final String path; private final String string; @@ -33,7 +31,7 @@ public class RequestDispatcherAdaptor implements RequestDispatcher { this.dispatchTargets = dispatchTargets; this.path = path; - this.string = SIMPLE_NAME + '[' + path + ", " + dispatchTargets + ']'; //$NON-NLS-1$ + this.string = getClass().getSimpleName() + '[' + path + ", " + dispatchTargets + ']'; //$NON-NLS-1$ } public void forward(ServletRequest request, ServletResponse response) diff --git a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java index 35a87d47a..9eb656646 100644 --- a/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java +++ b/bundles/org.eclipse.equinox.http.servlet/src/org/eclipse/equinox/http/servlet/internal/servlet/ServletContextAdaptor.java @@ -88,7 +88,7 @@ public class ServletContextAdaptor { this.classLoader = bundleWiring.getClassLoader(); - this.string = SIMPLE_NAME + '[' + contextController + ']'; + this.string = getClass().getSimpleName() + '[' + contextController + ']'; } public ServletContext createServletContext() { @@ -385,13 +385,7 @@ public class ServletContextAdaptor { } Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - boolean useThreadLocal = - "removeAttribute".equals(method.getName()) || - "setAttribute".equals(method.getName()); - - if (useThreadLocal) { - servletContextTL.set((ServletContext)proxy); - } + servletContextTL.set((ServletContext)proxy); try { Method m = contextToHandlerMethods.get(method); @@ -405,9 +399,7 @@ public class ServletContextAdaptor { } } finally { - if (useThreadLocal) { - servletContextTL.remove(); - } + servletContextTL.remove(); } } @@ -430,11 +422,6 @@ public class ServletContextAdaptor { } - private final static String SIMPLE_NAME = - ServletContextAdaptor.class.getSimpleName(); - - private final static ThreadLocal<ServletContext> servletContextTL = new ThreadLocal<ServletContext>(); - private final AccessControlContext acc; private final Bundle bundle; private final ClassLoader classLoader; @@ -443,6 +430,7 @@ public class ServletContextAdaptor { private final ProxyContext proxyContext; private final ServletContext servletContext; final ServletContextHelper servletContextHelper; + private final ThreadLocal<ServletContext> servletContextTL = new ThreadLocal<ServletContext>(); private final String string; } |