pagecontextimpl.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,055 行 · 第 1/4 页

JAVA
2,055
字号
      else	_out.flush();    }    RequestDispatcher rd = null;    HttpServletRequest req = (HttpServletRequest) getCauchoRequest();    HttpServletResponse res = (HttpServletResponse) getResponse();    if (res.isCommitted())      throw new IOException(L.l("can't forward after writing HTTP headers"));        _out.clear();    if (relativeUrl != null && ! relativeUrl.startsWith("/")) {      String servletPath = RequestAdapter.getPageServletPath(req);      int p = servletPath.lastIndexOf('/');      if (p >= 0) {	_cb.clear();	_cb.append(servletPath, 0, p + 1);	_cb.append(relativeUrl);        rd = getServletContext().getRequestDispatcher(_cb.toString());      }    }    if (rd == null)      rd = req.getRequestDispatcher(relativeUrl);    if (rd == null)      throw new ServletException(L.l("unknown forwarding page: `{0}'",                                     relativeUrl));    rd.forward(req, res);    //_out.close();    _responseStream.close();  }  /**   * Handles an exception caught in the JSP page.   *   * @param e the caught exception   */  public void handlePageException(Exception e)    throws ServletException, IOException  {    handlePageException((Throwable) e);  }  /**   * Handles an exception caught in the JSP page.   *   * @param e the caught exception   */  public void handlePageException(Throwable e)    throws ServletException, IOException  {    if (e instanceof SkipPageException)      return;    HttpServletRequest request = getCauchoRequest();        request.setAttribute("javax.servlet.jsp.jspException", e);    CauchoResponse response = getCauchoResponse();        response.setForbidForward(false);    response.setResponseStream(_responseStream);    response.killCache();    response.setNoCache(true);    _hasException = true;    if (e instanceof ClientDisconnectException)      throw (ClientDisconnectException) e;    if (! (_servlet instanceof Page)) {    }    else if (getApplication() == null	     || getApplication().getJsp() == null	     || ! getApplication().getJsp().isRecompileOnError()) {    }    else if (e instanceof OutOfMemoryError) {    }    else if (e instanceof Error) {      try {	Path workDir = getApplication().getAppDir().lookup("WEB-INF/work");	String className = _servlet.getClass().getName();	Path path = workDir.lookup(className.replace('.', '/') + ".class");		log.warning("Removing " + path + " due to " + e);		    	path.remove();      } catch (Exception e1) {      }      Page page = (Page) _servlet;      page._caucho_unload();      if (! page.isDead()) {	page.setDead();	page.destroy();      }    }    _topOut.clearBuffer();        if (_errorPage != null) {      getApplication().log(e.toString(), e);      getCauchoRequest().setAttribute(EXCEPTION, e);      getCauchoRequest().setAttribute("javax.servlet.error.exception", e);      getCauchoRequest().setAttribute("javax.servlet.error.exception_type", e);      getCauchoRequest().setAttribute("javax.servlet.error.request_uri",                            getCauchoRequest().getRequestURI());      try {	RequestDispatcher rd = getCauchoRequest().getRequestDispatcher(_errorPage);        if (rd instanceof RequestDispatcherImpl) {	  getCauchoResponse().setHasError(true);	            ((RequestDispatcherImpl) rd).error(getCauchoRequest(), getCauchoResponse());        }        else {	  if (rd != null) {	    getCauchoResponse().killCache();	    getCauchoResponse().setNoCache(true);	    rd.forward(getCauchoRequest(), getCauchoResponse());	  }	  else {	    log.log(Level.FINE, e.toString(), e);	    throw new ServletException(L.l("`{0}' is an unknown error page.  The JSP errorPage directive must refer to a valid URL relative to the current web-app.",					   _errorPage));	  }        }              } catch (FileNotFoundException e2) {        log.log(Level.WARNING, e.toString(), e2);        throw new ServletException(L.l("`{0}' is an unknown error page.  The JSP errorPage directive must refer to a valid URL relative to the current web-app.",                                       _errorPage));      } catch (IOException e2) {        log.log(Level.FINE, e.toString(), e2);      }      return;    }    /*    if (_servlet instanceof Page && ! (e instanceof LineMapException)) {      LineMap lineMap = ((Page) _servlet)._caucho_getLineMap();            if (lineMap != null)        e = new JspLineException(e, lineMap);            }    */        if (e instanceof ServletException) {      throw (ServletException) e;    }    else if (e instanceof IOException) {      throw (IOException) e;    }    else if (e instanceof RuntimeException) {      throw (RuntimeException) e;    }    else if (e instanceof Error) {      throw (Error) e;    }    else {      throw new ServletException(e);    }  }  /**   * Returns the error data   */  public ErrorData getErrorData()  {    String uri = (String) getCauchoRequest().getAttribute(AbstractHttpRequest.ERROR_URI);    if (uri == null)      return null;    Integer status = (Integer) getCauchoRequest().getAttribute(AbstractHttpRequest.STATUS_CODE);    return new ErrorData(getThrowable(),			 status == null ? 0 : status.intValue(),			 (String) getCauchoRequest().getAttribute(AbstractHttpRequest.ERROR_URI),			 (String) getCauchoRequest().getAttribute(AbstractHttpRequest.SERVLET_NAME));  }  /**   * Returns the variable resolver   */  public javax.servlet.jsp.el.VariableResolver getVariableResolver()  {    return this;  }  /**   * Returns the expression evaluator   */  public ExpressionEvaluator getExpressionEvaluator()  {    if (_expressionEvaluator == null)      _expressionEvaluator = new ExpressionEvaluatorImpl(getELContext());        return _expressionEvaluator;  }  /**   * Returns the expression evaluator   */  public ELContext getELContext()  {    if (_elContext == null) {      _elContext = new PageELContext();      WebApp webApp = getApplication();            JspApplicationContextImpl jspContext	= (JspApplicationContextImpl) webApp.getJspApplicationContext();      ELResolver []resolverArray = jspContext.getELResolverArray();            _elResolver = new PageContextELResolver(this, resolverArray);      ELContextListener []listenerArray = jspContext.getELListenerArray();      if (listenerArray.length > 0) {	ELContextEvent event = new ELContextEvent(_elContext);	for (int i = 0; i < listenerArray.length; i++) {	  listenerArray[i].contextCreated(event);	}      }            _functionMapper = new PageFunctionMapper();      _variableMapper = new PageVariableMapper();    }        return _elContext;  }  /**   * Given a relative url, return the absolute url.   *   * @param value the relative url   *   * @return the absolute url.   */  private String getRelativeUrl(String value)  {    if (value.length() > 0 && value.charAt(0) == '/')      return value;    ServletContext context = getServletContext();    String contextPath = RequestAdapter.getPageContextPath(getCauchoRequest());    String uri = RequestAdapter.getPageURI(getCauchoRequest());    String relPath = uri.substring(contextPath.length());    int p = relPath.lastIndexOf('/');    String urlPwd = p <= 0 ? "/" : relPath.substring(0, p + 1);        return urlPwd + value;  }  /**   * Releases the context.   */  public void release()  {    try {      _servlet = null;            if (_attributes.size() > 0)	_attributes.clear();      /* XXX:      if (! autoFlush && response instanceof Response)        ((Response) response).setDisableAutoFlush(false);      */      getCauchoResponse().setResponseStream(_responseStream);      getCauchoResponse().setFlushBuffer(null);      _request = null;      _webApp = null;      _session = null;      while (_out instanceof AbstractJspWriter) {	if (_out instanceof AbstractJspWriter)	  _out = ((AbstractJspWriter) _out).popWriter();      }      JspWriter out = _out;      _out = null;      _topOut = null;      _nodeEnv = null;      _jspOutputStream.release();      AbstractResponseStream responseStream = _responseStream;      _responseStream = null;      if (_responseAdapter != null) {        // jsp/15l3        _responseAdapter.finish();	//_responseAdapter.close();	ToCharResponseAdapter resAdapt = _responseAdapter;	ToCharResponseAdapter.free(resAdapt);      }      /*	// server/137q      if (! _hasException && responseStream != null)      	responseStream.close();      */	      _response = null;    } catch (IOException e) {      _out = null;    }  }    /**   * Returns the localized message appropriate for the current context.   */  public String getLocalizedMessage(String key,                                    Object []args,                                    String basename)  {    Object lc = basename;    if (lc == null)      lc = getAttribute("caucho.bundle");        if (lc == null)      lc = Config.find(this, Config.FMT_LOCALIZATION_CONTEXT);        return getLocalizedMessage(lc, key, args, basename);  }  /**   * Returns the localized message appropriate for the current context.   */  public String getLocalizedMessage(Object lc,                                    String key,                                    Object []args,                                    String basename)  {    String bundleString = null;    // jsp/1c51, jsp/1c54    String prefix = (String) getAttribute("caucho.bundle.prefix");    // jsp/1c3x    if (key == null)       key = "";    if (prefix != null)      key = prefix + key;    if (lc == null) {      lc = basename;      if (lc == null || "".equals(lc))        lc = getAttribute("caucho.bundle");      if (lc == null)        lc = Config.find(this, Config.FMT_LOCALIZATION_CONTEXT);    }    Locale locale = null;    if (lc instanceof LocalizationContext) {      ResourceBundle bundle = ((LocalizationContext) lc).getResourceBundle();      locale = ((LocalizationContext) lc).getLocale();      try {        if (bundle != null)          bundleString = bundle.getString(key);      } catch (Exception e) {      }    }    else if (lc instanceof String) {      LocalizationContext loc = getBundle((String) lc);      locale = loc.getLocale();            ResourceBundle bundle = loc.getResourceBundle();            try {        if (bundle != null)          bundleString = bundle.getString(key);      } catch (Exception e) {      }    }    if (bundleString == null)      return "???" + key + "???";    else if (args == null || args.length == 0)      return bundleString;    if (locale == null)      locale = getLocale();    if (locale != null)      return new MessageFormat(bundleString, locale).format(args);    else      return new MessageFormat(bundleString).format(args);  }  /**   * Returns the localized message appropriate for the current context.   */  public LocalizationContext getBundle(String name)  {    Object localeObj = Config.find(this, Config.FMT_LOCALE);    LocalizationContext bundle = null;    BundleManager manager = getBundleManager();    if (localeObj instanceof Locale) {      Locale locale = (Locale) localeObj;      bundle = manager.getBundle(name, locale);    }    else if (localeObj instanceof String) {      Locale locale = getLocale((String) localeObj, null);      bundle = manager.getBundle(name, locale);    }    else {      String acceptLanguage = getCauchoRequest().getHeader("Accept-Language");      if (acceptLanguage != null) {	String cacheKey = name + acceptLanguage; 	bundle = manager.getBundle(name, cacheKey, getCauchoRequest().getLocales());      }    }    if (bundle != null)      return bundle;    Object fallback = Config.find(this, Config.FMT_FALLBACK_LOCALE);    if (fallback instanceof Locale) {      Locale locale = (Locale) fallback;      bundle = manager.getBundle(name, locale);    }    else if (fallback instanceof String) {      String localeName = (String) fallback;      Locale locale = getLocale(localeName, null);      bundle = manager.getBundle(name, locale);    }    if (bundle != null)      return bundle;        bundle = manager.getBundle(name);    if (bundle != null)      return bundle;    else {      return BundleManager.NULL_BUNDLE;    }  }  /**   * Returns the currently active locale.   */  public Locale getLocale()  {    if (_locale != null)      return _locale;    _locale = getLocaleImpl();    if (_locale != null)      getResponse().setLocale(_locale);    return _locale;  }  /**   * Returns the currently active locale.   */  private Locale getLocaleImpl()  {    Object localeObj = Config.find(this, Config.FMT_LOCALIZATION_CONTEXT);        LocalizationContext lc;    Locale locale = null;        if (localeObj instanceof LocalizationContext) {      lc = (LocalizationContext) localeObj;      locale = lc.getLocale();      if (locale != null)        return locale;    }    localeObj = Config.find(this, Config.FMT_LOCALE);      if (localeObj instanceof Locale)        return (Locale) localeObj;    else if (localeObj instanceof String)      return getLocale((String) localeObj, null);    lc = (LocalizationContext) getAttribute("caucho.bundle");    if (lc != null)      locale = lc.getLocale();    if (locale != null)      return locale;    

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?