Mercurial Hosting > luan
diff src/org/eclipse/jetty/server/handler/ContextHandler.java @ 994:4e9d373bf6e9
remove ContextHandler.Context
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Oct 2016 16:34:18 -0600 |
parents | d9d95acded81 |
children | 0eba8f555c19 |
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/handler/ContextHandler.java Tue Oct 18 15:54:54 2016 -0600 +++ b/src/org/eclipse/jetty/server/handler/ContextHandler.java Tue Oct 18 16:34:18 2016 -0600 @@ -82,38 +82,13 @@ /** * ContextHandler. * - * This handler wraps a call to handle by setting the context and servlet path, plus setting the context classloader. + * This handler wraps a call to handle by setting the context and servlet path. * - * <p> - * If the context init parameter "org.eclipse.jetty.server.context.ManagedAttributes" is set to a comma separated list of names, then they are treated as - * context attribute names, which if set as attributes are passed to the servers Container so that they may be managed with JMX. - * <p> - * The maximum size of a form that can be processed by this context is controlled by the system properties org.eclipse.jetty.server.Request.maxFormKeys - * and org.eclipse.jetty.server.Request.maxFormContentSize. These can also be configured with {@link #setMaxFormContentSize(int)} and {@link #setMaxFormKeys(int)} - * - * @org.apache.xbean.XBean description="Creates a basic HTTP context" */ public final class ContextHandler extends HandlerWrapper implements Server.Graceful { private static final Logger LOG = LoggerFactory.getLogger(ContextHandler.class); - private static final ThreadLocal<Context> __context = new ThreadLocal<Context>(); - - /* ------------------------------------------------------------ */ - /** - * Get the current ServletContext implementation. - * - * @return ServletContext implementation - */ - public static ContextHandler getCurrentContext() - { - Context context = __context.get(); - return context==null ? null : context.getContextHandler(); - } - - private Context _scontext; - - private final AttributesMap _contextAttributes; private String _contextPath = "/"; private Resource _baseResource; private Logger _logger; @@ -126,8 +101,6 @@ public ContextHandler() { super(); - _scontext = new Context(); - _contextAttributes = new AttributesMap(); } public ContextHandler(String contextPath) @@ -150,13 +123,7 @@ public void dump(Appendable out, String indent) throws IOException { dumpThis(out); - dump(out,indent,TypeUtil.asList(getHandlers()),getBeans(), - _contextAttributes.getAttributeEntrySet()); - } - - public Context getServletContext() - { - return _scontext; + dump(out,indent,TypeUtil.asList(getHandlers()),getBeans()); } public String getContextPath() @@ -182,18 +149,6 @@ } /* ------------------------------------------------------------ */ - public Logger getLogger() - { - return _logger; - } - - /* ------------------------------------------------------------ */ - public void setLogger(Logger logger) - { - _logger = logger; - } - - /* ------------------------------------------------------------ */ /* * @see org.eclipse.thread.AbstractLifeCycle#doStart() */ @@ -206,25 +161,12 @@ throw new IllegalStateException("Null contextPath"); _logger = LoggerFactory.getLogger(getContextPath()); - ClassLoader old_classloader = null; - Thread current_thread = null; - Context old_context = null; - try - { - old_context = __context.get(); - __context.set(_scontext); + super.doStart(); - super.doStart(); - - synchronized(this) - { - _availability = _shutdown?__SHUTDOWN:__AVAILABLE; - } - } - finally + synchronized(this) { - __context.set(old_context); + _availability = _shutdown?__SHUTDOWN:__AVAILABLE; } } @@ -237,22 +179,8 @@ { _availability = __STOPPED; - ClassLoader old_classloader = null; - Thread current_thread = null; - - Context old_context = __context.get(); - __context.set(_scontext); - try - { - super.doStop(); - } - finally - { - LOG.info("stopped {}",this); - __context.set(old_context); - } - - _contextAttributes.clearAttributes(); + super.doStop(); + LOG.info("stopped {}",this); } private boolean checkContext(final String target, final Request baseRequest, final HttpServletResponse response) throws IOException, ServletException @@ -298,18 +226,15 @@ if (LOG.isDebugEnabled()) LOG.debug("scope {}|{}|{} @ {}",baseRequest.getContextPath(),baseRequest.getServletPath(),baseRequest.getPathInfo(),this); - Context old_context = null; String old_context_path = null; String old_servlet_path = null; String old_path_info = null; - ClassLoader old_classloader = null; - Thread current_thread = null; String pathInfo = target; - old_context = baseRequest.getContext(); + ContextHandler oldContextHandler = baseRequest._contextHandler; // Are we already in this context? - if (old_context != _scontext) + if (oldContextHandler != this) { if (!checkContext(target,baseRequest,response)) return; @@ -339,8 +264,7 @@ old_path_info = baseRequest.getPathInfo(); // Update the paths - baseRequest.setContext(_scontext); - __context.set(_scontext); + baseRequest._contextHandler = this; if (target.startsWith("/")) { if (_contextPath.length() == 1) @@ -367,11 +291,10 @@ } finally { - if (old_context != _scontext) + if (oldContextHandler != this) { // reset the context and servlet path. - baseRequest.setContext(old_context); - __context.set(old_context); + baseRequest._contextHandler = oldContextHandler; baseRequest.setContextPath(old_context_path); baseRequest.setServletPath(old_servlet_path); baseRequest.setPathInfo(old_path_info); @@ -386,19 +309,11 @@ _contextPath = contextPath; } - /* ------------------------------------------------------------ */ - /** - * @return Returns the resourceBase. - */ public Resource getBaseResource() { return _baseResource; } - /* ------------------------------------------------------------ */ - /** - * @return Returns the base resource as a string. - */ public String getResourceBase() { if (_baseResource == null) @@ -406,21 +321,11 @@ return _baseResource.toString(); } - /* ------------------------------------------------------------ */ - /** - * @param base - * The resourceBase to set. - */ public void setBaseResource(Resource base) { _baseResource = base; } - /* ------------------------------------------------------------ */ - /** - * @param resourceBase - * The base resource as a string. - */ public void setResourceBase(String resourceBase) { try @@ -459,9 +364,6 @@ return b.toString(); } - /* ------------------------------------------------------------ */ - /* - */ public Resource getResource(String path) throws MalformedURLException { if (path == null || !path.startsWith(URIUtil.SLASH)) @@ -515,592 +417,4 @@ } return Collections.emptySet(); } - - /* ------------------------------------------------------------ */ - /** - * Context. - * <p> - * A partial implementation of {@link javax.servlet.ServletContext}. A complete implementation is provided by the derived {@link ContextHandler}. - * </p> - * - * - */ - public final class Context implements ServletContext - { - protected int _majorVersion = 3; - protected int _minorVersion = 0; - - /* ------------------------------------------------------------ */ - protected Context() - { - } - - /* ------------------------------------------------------------ */ - public ContextHandler getContextHandler() - { - // TODO reduce visibility of this method - return ContextHandler.this; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getContext(java.lang.String) - */ - @Override - public ServletContext getContext(String uripath) - { - List<ContextHandler> contexts = new ArrayList<ContextHandler>(); - Handler[] handlers = getServer().getChildHandlersByClass(ContextHandler.class); - String matched_path = null; - - for (Handler handler : handlers) - { - if (handler == null) - continue; - ContextHandler ch = (ContextHandler)handler; - String context_path = ch.getContextPath(); - - if (uripath.equals(context_path) || (uripath.startsWith(context_path) && uripath.charAt(context_path.length()) == '/') - || "/".equals(context_path)) - { - if (matched_path == null || context_path.length() > matched_path.length()) - { - contexts.clear(); - matched_path = context_path; - } - - if (matched_path.equals(context_path)) - contexts.add(ch); - } - } - - if (contexts.size() > 0) - return contexts.get(0)._scontext; - - // try again ignoring virtual hosts - matched_path = null; - for (Handler handler : handlers) - { - if (handler == null) - continue; - ContextHandler ch = (ContextHandler)handler; - String context_path = ch.getContextPath(); - - if (uripath.equals(context_path) || (uripath.startsWith(context_path) && uripath.charAt(context_path.length()) == '/') - || "/".equals(context_path)) - { - if (matched_path == null || context_path.length() > matched_path.length()) - { - contexts.clear(); - matched_path = context_path; - } - - if (matched_path.equals(context_path)) - contexts.add(ch); - } - } - - if (contexts.size() > 0) - return contexts.get(0)._scontext; - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getMajorVersion() - */ - @Override - public int getMajorVersion() - { - return 3; - } - - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getMimeType(java.lang.String) - */ - @Override - public String getMimeType(String file) - { - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getMinorVersion() - */ - @Override - public int getMinorVersion() - { - return 0; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String) - */ - @Override - public RequestDispatcher getNamedDispatcher(String name) - { - throw new UnsupportedOperationException(); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String) - */ - @Override - public RequestDispatcher getRequestDispatcher(String uriInContext) - { - throw new UnsupportedOperationException(); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getRealPath(java.lang.String) - */ - @Override - public String getRealPath(String path) - { - if (path == null) - return null; - if (path.length() == 0) - path = URIUtil.SLASH; - else if (path.charAt(0) != '/') - path = URIUtil.SLASH + path; - - try - { - Resource resource = ContextHandler.this.getResource(path); - if (resource != null) - { - File file = resource.getFile(); - if (file != null) - return file.getCanonicalPath(); - } - } - catch (Exception e) - { - LOG.trace("",e); - } - - return null; - } - - /* ------------------------------------------------------------ */ - @Override - public URL getResource(String path) throws MalformedURLException - { - Resource resource = ContextHandler.this.getResource(path); - if (resource != null && resource.exists()) - return resource.getURL(); - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String) - */ - @Override - public InputStream getResourceAsStream(String path) - { - try - { - URL url = getResource(path); - if (url == null) - return null; - Resource r = Resource.newResource(url); - return r.getInputStream(); - } - catch (Exception e) - { - LOG.trace("",e); - return null; - } - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String) - */ - @Override - public Set getResourcePaths(String path) - { - return ContextHandler.this.getResourcePaths(path); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServerInfo() - */ - @Override - public String getServerInfo() - { - return "jetty/" + Server.version; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServlet(java.lang.String) - */ - @Override - @Deprecated - public Servlet getServlet(String name) throws ServletException - { - throw new UnsupportedOperationException(); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServletNames() - */ - @SuppressWarnings("unchecked") - @Override - @Deprecated - public Enumeration getServletNames() - { - throw new UnsupportedOperationException(); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServlets() - */ - @SuppressWarnings("unchecked") - @Override - @Deprecated - public Enumeration getServlets() - { - throw new UnsupportedOperationException(); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String) - */ - @Override - public void log(Exception exception, String msg) - { - _logger.warn(msg,exception); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#log(java.lang.String) - */ - @Override - public void log(String msg) - { - _logger.info(msg); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable) - */ - @Override - public void log(String message, Throwable throwable) - { - _logger.warn(message,throwable); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getInitParameter(java.lang.String) - */ - @Override - public String getInitParameter(String name) - { - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getInitParameterNames() - */ - @SuppressWarnings("unchecked") - @Override - public Enumeration getInitParameterNames() - { - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getAttribute(java.lang.String) - */ - @Override - public synchronized Object getAttribute(String name) - { - if (_contextAttributes != null) - return _contextAttributes.getAttribute(name); - return null; - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getAttributeNames() - */ - @SuppressWarnings("unchecked") - @Override - public synchronized Enumeration getAttributeNames() - { - HashSet<String> set = new HashSet<String>(); - if (_contextAttributes != null) - { - Enumeration<String> e = _contextAttributes.getAttributeNames(); - while (e.hasMoreElements()) - set.add(e.nextElement()); - } - - return Collections.enumeration(set); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object) - */ - @Override - public synchronized void setAttribute(String name, Object value) - { - Object old_value = _contextAttributes.getAttribute(name); - - if (value == null) - _contextAttributes.removeAttribute(name); - else - _contextAttributes.setAttribute(name,value); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#removeAttribute(java.lang.String) - */ - @Override - public synchronized void removeAttribute(String name) - { - if (_contextAttributes == null) - { - // Set it on the handler - return; - } - - _contextAttributes.removeAttribute(name); - } - - /* ------------------------------------------------------------ */ - /* - * @see javax.servlet.ServletContext#getServletContextName() - */ - @Override - public String getServletContextName() - { - return ContextHandler.this.getContextPath(); - } - - /* ------------------------------------------------------------ */ - @Override - public String getContextPath() - { - if ((_contextPath != null) && _contextPath.equals(URIUtil.SLASH)) - return ""; - - return _contextPath; - } - - /* ------------------------------------------------------------ */ - @Override - public String toString() - { - return "ServletContext@" + ContextHandler.this.toString(); - } - - /* ------------------------------------------------------------ */ - @Override - public boolean setInitParameter(String name, String value) - { - return false; - } - - /* ------------------------------------------------------------ */ - final private static String __unimplmented="Unimplemented - use org.eclipse.jetty.servlet.ServletContextHandler"; - - @Override - public Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) - { - throw new UnsupportedOperationException(); - } - - @Override - public Dynamic addFilter(String filterName, Filter filter) - { - throw new UnsupportedOperationException(); - } - - @Override - public Dynamic addFilter(String filterName, String className) - { - throw new UnsupportedOperationException(); - } - - @Override - public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) - { - throw new UnsupportedOperationException(); - } - - @Override - public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) - { - throw new UnsupportedOperationException(); - } - - @Override - public javax.servlet.ServletRegistration.Dynamic addServlet(String servletName, String className) - { - throw new UnsupportedOperationException(); - } - - @Override - public <T extends Filter> T createFilter(Class<T> c) throws ServletException - { - throw new UnsupportedOperationException(); - } - - @Override - public <T extends Servlet> T createServlet(Class<T> c) throws ServletException - { - throw new UnsupportedOperationException(); - } - - @Override - public Set<SessionTrackingMode> getDefaultSessionTrackingModes() - { - throw new UnsupportedOperationException(); - } - - @Override - public Set<SessionTrackingMode> getEffectiveSessionTrackingModes() - { - throw new UnsupportedOperationException(); - } - - @Override - public FilterRegistration getFilterRegistration(String filterName) - { - throw new UnsupportedOperationException(); - } - - @Override - public Map<String, ? extends FilterRegistration> getFilterRegistrations() - { - throw new UnsupportedOperationException(); - } - - @Override - public ServletRegistration getServletRegistration(String servletName) - { - throw new UnsupportedOperationException(); - } - - @Override - public Map<String, ? extends ServletRegistration> getServletRegistrations() - { - throw new UnsupportedOperationException(); - } - - @Override - public SessionCookieConfig getSessionCookieConfig() - { - throw new UnsupportedOperationException(); - } - - @Override - public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) - { - throw new UnsupportedOperationException(); - } - - @Override - public void addListener(String className) - { - throw new UnsupportedOperationException(); - } - - @Override - public <T extends EventListener> void addListener(T t) - { - throw new UnsupportedOperationException(); - } - - @Override - public void addListener(Class<? extends EventListener> listenerClass) - { - throw new UnsupportedOperationException(); - } - - @Override - public <T extends EventListener> T createListener(Class<T> clazz) throws ServletException - { - try - { - return clazz.newInstance(); - } - catch (InstantiationException e) - { - throw new ServletException(e); - } - catch (IllegalAccessException e) - { - throw new ServletException(e); - } - } - - @Override - public ClassLoader getClassLoader() - { - AccessController.checkPermission(new RuntimePermission("getClassLoader")); - return null; - } - - @Override - public int getEffectiveMajorVersion() - { - return _majorVersion; - } - - @Override - public int getEffectiveMinorVersion() - { - return _minorVersion; - } - - public void setEffectiveMajorVersion (int v) - { - _majorVersion = v; - } - - public void setEffectiveMinorVersion (int v) - { - _minorVersion = v; - } - - @Override - public JspConfigDescriptor getJspConfigDescriptor() - { - throw new UnsupportedOperationException(); - } - - public void setJspConfigDescriptor(JspConfigDescriptor d) - { - throw new UnsupportedOperationException(); - } - - @Override - public void declareRoles(String... roleNames) - { - throw new UnsupportedOperationException(); - } - } - }