📄 contexthandler.java
字号:
{ return Collections.enumeration(_initParams.keySet()); } /* ------------------------------------------------------------ */ /** * @return Returns the initParams. */ public Map getInitParams() { return _initParams; } /* ------------------------------------------------------------ */ /* * @see javax.servlet.ServletContext#getServletContextName() */ public String getDisplayName() { return _displayName; } /* ------------------------------------------------------------ */ public EventListener[] getEventListeners() { return _eventListeners; } /* ------------------------------------------------------------ */ public void setEventListeners(EventListener[] eventListeners) { _contextListeners=null; _contextAttributeListeners=null; _requestListeners=null; _requestAttributeListeners=null; _eventListeners=eventListeners; for (int i=0; eventListeners!=null && i<eventListeners.length;i ++) { EventListener listener = _eventListeners[i]; if (listener instanceof ServletContextListener) _contextListeners= LazyList.add(_contextListeners, listener); if (listener instanceof ServletContextAttributeListener) _contextAttributeListeners= LazyList.add(_contextAttributeListeners, listener); if (listener instanceof ServletRequestListener) _requestListeners= LazyList.add(_requestListeners, listener); if (listener instanceof ServletRequestAttributeListener) _requestAttributeListeners= LazyList.add(_requestAttributeListeners, listener); } } /* ------------------------------------------------------------ */ public void addEventListener(EventListener listener) { setEventListeners((EventListener[])LazyList.addToArray(getEventListeners(), listener, EventListener.class)); } /* ------------------------------------------------------------ */ /** * @return true if this context is accepting new requests */ public boolean isShutdown() { return !_shutdown; } /* ------------------------------------------------------------ */ /** Set shutdown status. * This field allows for graceful shutdown of a context. A started context may be put into non accepting state so * that existing requests can complete, but no new requests are accepted. * @param accepting true if this context is accepting new requests */ public void setShutdown(boolean shutdown) { _shutdown = shutdown; } /* ------------------------------------------------------------ */ /* * @see org.mortbay.thread.AbstractLifeCycle#doStart() */ protected void doStart() throws Exception { if (_contextPath==null) throw new IllegalStateException("Null contextPath"); _logger=Log.getLogger(getDisplayName()==null?getContextPath():getDisplayName()); ClassLoader old_classloader=null; Thread current_thread=null; SContext old_context=null; _contextAttributes=new AttributesMap(); try { // Set the classloader if (_classLoader!=null) { current_thread=Thread.currentThread(); old_classloader=current_thread.getContextClassLoader(); current_thread.setContextClassLoader(_classLoader); } if (_mimeTypes==null) _mimeTypes=new MimeTypes(); old_context=(SContext)__context.get(); __context.set(_scontext); if (_errorHandler==null) setErrorHandler(new ErrorHandler()); startContext(); } finally { __context.set(old_context); // reset the classloader if (_classLoader!=null) { current_thread.setContextClassLoader(old_classloader); } } } /* ------------------------------------------------------------ */ protected void startContext() throws Exception { super.doStart(); if (_errorHandler!=null) _errorHandler.start(); // Context listeners if (_contextListeners != null ) { ServletContextEvent event= new ServletContextEvent(_scontext); for (int i= 0; i < LazyList.size(_contextListeners); i++) { ((ServletContextListener)LazyList.get(_contextListeners, i)).contextInitialized(event); } } String managedAttributes = (String)_initParams.get(MANAGED_ATTRIBUTES); if (managedAttributes!=null) { _managedAttributes=new HashSet(); String[] attributes = managedAttributes.toString().split(","); for (int i=0;i<attributes.length;i++) _managedAttributes.add(attributes[i]); Enumeration e = _scontext.getAttributeNames(); while(e.hasMoreElements()) { String name = (String)e.nextElement(); Object value = _scontext.getAttribute(name); setManagedAttribute(name,value); } } } /* ------------------------------------------------------------ */ /* * @see org.mortbay.thread.AbstractLifeCycle#doStop() */ protected void doStop() throws Exception { ClassLoader old_classloader=null; Thread current_thread=null; SContext old_context=(SContext)__context.get(); __context.set(_scontext); try { // Set the classloader if (_classLoader!=null) { current_thread=Thread.currentThread(); old_classloader=current_thread.getContextClassLoader(); current_thread.setContextClassLoader(_classLoader); } super.doStop(); // Context listeners if (_contextListeners != null ) { ServletContextEvent event= new ServletContextEvent(_scontext); for (int i=LazyList.size(_contextListeners); i-->0;) { ((ServletContextListener)LazyList.get(_contextListeners, i)).contextDestroyed(event); } } if (_errorHandler!=null) _errorHandler.stop(); Enumeration e = _scontext.getAttributeNames(); while(e.hasMoreElements()) { String name = (String)e.nextElement(); setManagedAttribute(name,null); } } finally { __context.set(old_context); // reset the classloader if (_classLoader!=null) current_thread.setContextClassLoader(old_classloader); } if (_contextAttributes!=null) _contextAttributes.clearAttributes(); _contextAttributes=null; } /* ------------------------------------------------------------ */ /* * @see org.mortbay.jetty.Handler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) */ public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { boolean new_context=false; SContext 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; Request base_request=(request instanceof Request)?(Request)request:HttpConnection.getCurrentConnection().getRequest(); if( !isStarted() || _shutdown || (dispatch==REQUEST && base_request.isHandled())) return; old_context=base_request.getContext(); // Are we already in this context? if (old_context!=_scontext) { new_context=true; // Check the vhosts if (_vhosts!=null && _vhosts.length>0) { String vhost = normalizeHostname( request.getServerName()); boolean match=false; // TODO non-linear lookup for (int i=0;!match && i<_vhosts.length;i++) { String contextVhost = _vhosts[i]; if(contextVhost==null) continue; if(contextVhost.startsWith("*.")) { // wildcard only at the beginning, and only for one additional subdomain level match=contextVhost.regionMatches(true,2,vhost,vhost.indexOf(".")+1,contextVhost.length()-2); } else match=contextVhost.equalsIgnoreCase(vhost); } if (!match) return; } // Check the connector if (_connectors!=null && _connectors.size()>0) { String connector=HttpConnection.getCurrentConnection().getConnector().getName(); if (connector==null || !_connectors.contains(connector)) return; } // Nope - so check the target. if (dispatch==REQUEST) { if (_compactPath) target=URIUtil.compactPath(target); if (target.equals(_contextPath)) { if (!_allowNullPathInfo && !target.endsWith(URIUtil.SLASH)) { base_request.setHandled(true); if (request.getQueryString()!=null) response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)+"?"+request.getQueryString()); else response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH)); return; } if (_contextPath.length()>1) { target=URIUtil.SLASH; request.setAttribute("org.mortbay.jetty.nullPathInfo",target); } } else if (target.startsWith(_contextPath) && (_contextPath.length()==1 || target.charAt(_contextPath.length())=='/')) { if (_contextPath.length()>1) target=target.substring(_contextPath.length()); } else { // Not for this context! return; } } } try { old_context_path=base_request.getContextPath(); old_servlet_path=base_request.getServletPath(); old_path_info=base_request.getPathInfo(); // Update the paths base_request.setContext(_scontext); if (dispatch!=INCLUDE && target.startsWith("/")) { if (_contextPath.length()==1) base_request.setContextPath(""); else base_request.setContextPath(_contextPath); base_request.setServletPath(null); base_request.setPathInfo(target); } ServletRequestEvent event=null; if (new_context) { // Set the classloader if (_classLoader!=null) { current_thread=Thread.currentThread(); old_classloader=current_thread.getContextClassLoader(); current_thread.setContextClassLoader(_classLoader); } // Handle the REALLY SILLY request events! base_request.setRequestListeners(_requestListeners); if (_requestAttributeListeners!=null) { final int s=LazyList.size(_requestAttributeListeners); for(int i=0;i<s;i++) base_request.addEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i))); } } // Handle the request try { if (dispatch==REQUEST && isProtectedTarget(target)) throw new HttpException(HttpServletResponse.SC_NOT_FOUND); Handler handler = getHandler(); if (handler!=null) handler.handle(target, request, response, dispatch); } catch(HttpException e) { Log.debug(e); response.sendError(e.getStatus(), e.getReason()); } finally { // Handle more REALLY SILLY request events! if (new_context) { base_request.takeRequestListeners(); if (_requestAttributeListeners!=null) { for(int i=LazyList.size(_requestAttributeListeners);i-->0;) base_request.removeEventListener(((EventListener)LazyList.get(_requestAttributeListeners,i))); } } } } finally { if (old_context!=_scontext) { // reset the classloader if (_classLoader!=null) { current_thread.setContextClassLoader(old_classloader); } // reset the context and servlet path.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -