📄 servletholder.java
字号:
if (_unavailable!=0) { if (_unavailable<0 || _unavailable>0 && System.currentTimeMillis()<_unavailable) throw _unavailableEx; _unavailable=0; _unavailableEx=null; } if (_servlet==null) initServlet(); return _servlet; } /* ------------------------------------------------------------ */ /** * Check to ensure class of servlet is acceptable. * @throws UnavailableException */ public void checkServletType () throws UnavailableException { if (!javax.servlet.Servlet.class.isAssignableFrom(_class)) { throw new UnavailableException("Servlet "+_class+" is not a javax.servlet.Servlet"); } } /* ------------------------------------------------------------ */ /** * @return true if the holder is started and is not unavailable */ public boolean isAvailable() { if (isStarted()&& _unavailable==0) return true; try { getServlet(); } catch(Exception e) { Log.ignore(e); } return isStarted()&& _unavailable==0; } /* ------------------------------------------------------------ */ private void makeUnavailable(UnavailableException e) { if (_unavailableEx==e && _unavailable!=0) return; _servletHandler.getServletContext().log("unavailable",e); _unavailableEx=e; _unavailable=-1; if (e.isPermanent()) _unavailable=-1; else { if (_unavailableEx.getUnavailableSeconds()>0) _unavailable=System.currentTimeMillis()+1000*_unavailableEx.getUnavailableSeconds(); else _unavailable=System.currentTimeMillis()+5000; // TODO configure } } /* ------------------------------------------------------------ */ private void makeUnavailable(Throwable e) { if (e instanceof UnavailableException) makeUnavailable((UnavailableException)e); else { _servletHandler.getServletContext().log("unavailable",e); _unavailableEx=new UnavailableException(e.toString(),-1); _unavailable=-1; } } /* ------------------------------------------------------------ */ private void initServlet() throws ServletException { Principal user=null; try { if (_servlet==null) _servlet=(Servlet)newInstance(); if (_config==null) _config=new Config(); //handle any cusomizations of the servlet, such as @postConstruct if (!(_servlet instanceof SingleThreadedWrapper)) _servlet = getServletHandler().customizeServlet(_servlet); // Handle run as if (_runAs!=null && _realm!=null) user=_realm.pushRole(null,_runAs); _servlet.init(_config); } catch (UnavailableException e) { makeUnavailable(e); _servlet=null; _config=null; throw e; } catch (ServletException e) { makeUnavailable(e.getCause()==null?e:e.getCause()); _servlet=null; _config=null; throw e; } catch (Exception e) { makeUnavailable(e); _servlet=null; _config=null; throw new ServletException(e); } finally { // pop run-as role if (_runAs!=null && _realm!=null && user!=null) _realm.popRole(user); } } /* ------------------------------------------------------------ */ /** Service a request with this servlet. */ public void handle(ServletRequest request, ServletResponse response) throws ServletException, UnavailableException, IOException { if (_class==null) throw new UnavailableException("Servlet Not Initialized"); Servlet servlet=_servlet; synchronized(this) { if (_unavailable!=0 || !_initOnStartup) servlet=getServlet(); if (servlet==null) throw new UnavailableException("Could not instantiate "+_class); } // Service the request boolean servlet_error=true; Principal user=null; Request base_request=null; try { // Handle aliased path if (_forcedPath!=null) // TODO complain about poor naming to the Jasper folks request.setAttribute("org.apache.catalina.jsp_file",_forcedPath); // Handle run as if (_runAs!=null && _realm!=null) { base_request=HttpConnection.getCurrentConnection().getRequest(); user=_realm.pushRole(base_request.getUserPrincipal(),_runAs); base_request.setUserPrincipal(user); } servlet.service(request,response); servlet_error=false; } catch(UnavailableException e) { makeUnavailable(e); throw _unavailableEx; } finally { // pop run-as role if (_runAs!=null && _realm!=null && user!=null && base_request!=null) { user=_realm.popRole(user); base_request.setUserPrincipal(user); } // Handle error params. if (servlet_error) request.setAttribute("javax.servlet.error.servlet_name",getName()); } } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ class Config implements ServletConfig { /* -------------------------------------------------------- */ public String getServletName() { return getName(); } /* -------------------------------------------------------- */ public ServletContext getServletContext() { return _servletHandler.getServletContext(); } /* -------------------------------------------------------- */ public String getInitParameter(String param) { return ServletHolder.this.getInitParameter(param); } /* -------------------------------------------------------- */ public Enumeration getInitParameterNames() { return ServletHolder.this.getInitParameterNames(); } } /* -------------------------------------------------------- */ /* -------------------------------------------------------- */ /* -------------------------------------------------------- */ private class SingleThreadedWrapper implements Servlet { Stack _stack=new Stack(); public void destroy() { synchronized(this) { while(_stack.size()>0) try { ((Servlet)_stack.pop()).destroy(); } catch (Exception e) { Log.warn(e); } } } public ServletConfig getServletConfig() { return _config; } public String getServletInfo() { return null; } public void init(ServletConfig config) throws ServletException { synchronized(this) { if(_stack.size()==0) { try { Servlet s = (Servlet) newInstance(); s = getServletHandler().customizeServlet(s); s.init(config); _stack.push(s); } catch (ServletException e) { throw e; } catch (Exception e) { throw new ServletException(e); } } } } public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { Servlet s; synchronized(this) { if(_stack.size()>0) s=(Servlet)_stack.pop(); else { try { s = (Servlet) newInstance(); s = getServletHandler().customizeServlet(s); s.init(_config); } catch (ServletException e) { throw e; } catch (IOException e) { throw e; } catch (Exception e) { throw new ServletException(e); } } } try { s.service(req,res); } finally { synchronized(this) { _stack.push(s); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -