pagecontextimpl.java

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

JAVA
2,055
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.jsp;import com.caucho.el.EL;import com.caucho.el.ExprEnv;import com.caucho.jsp.cfg.JspPropertyGroup;import com.caucho.jsp.el.*;import com.caucho.jstl.JstlPageContext;import com.caucho.log.Log;import com.caucho.server.connection.AbstractHttpRequest;import com.caucho.server.connection.AbstractResponseStream;import com.caucho.server.connection.CauchoRequest;import com.caucho.server.connection.CauchoResponse;import com.caucho.server.connection.RequestAdapter;import com.caucho.server.connection.ToCharResponseAdapter;import com.caucho.server.webapp.RequestDispatcherImpl;import com.caucho.server.webapp.WebApp;import com.caucho.util.CharBuffer;import com.caucho.util.HashMapImpl;import com.caucho.util.L10N;import com.caucho.util.NullEnumeration;import com.caucho.vfs.ClientDisconnectException;import com.caucho.vfs.FlushBuffer;import com.caucho.vfs.Path;import com.caucho.vfs.TempCharBuffer;import com.caucho.xpath.VarEnv;import org.w3c.dom.Node;import javax.el.ELContext;import javax.el.ELContextEvent;import javax.el.ELContextListener;import javax.el.ELResolver;import javax.el.ValueExpression;import javax.servlet.*;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.servlet.jsp.ErrorData;import javax.servlet.jsp.JspContext;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.SkipPageException;import javax.servlet.jsp.el.ExpressionEvaluator;import javax.servlet.jsp.el.VariableResolver;import javax.servlet.jsp.jstl.core.Config;import javax.servlet.jsp.jstl.fmt.LocalizationContext;import javax.servlet.jsp.tagext.BodyContent;import javax.servlet.jsp.tagext.JspFragment;import java.io.FileNotFoundException;import java.io.IOException;import java.io.PrintWriter;import java.io.Reader;import java.io.Writer;import java.lang.reflect.Method;import java.text.MessageFormat;import java.util.Collections;import java.util.Enumeration;import java.util.HashMap;import java.util.Locale;import java.util.Map;import java.util.ResourceBundle;import java.util.logging.Level;import java.util.logging.Logger;public class PageContextImpl extends PageContext  implements ExprEnv, JstlPageContext, VariableResolver {  private static final Logger log = Log.open(PageContextImpl.class);  static final L10N L = new L10N(PageContextImpl.class);    private JspWriterAdapter _jspAdapter = new JspWriterAdapter();  private JspServletOutputStream _jspOutputStream    = new JspServletOutputStream(this);  private Map<String,Object> _attributes;  private Servlet _servlet;  private HttpServletRequest _request;  private CauchoResponse _response;  private ToCharResponseAdapter _responseAdapter;    private WebApp _webApp;  private HttpSession _session;  private JspWriter _topOut;  private JspWriter _out;  private String _errorPage;  protected boolean _isFilled;    private AbstractResponseStream _responseStream;  private BodyResponseStream _bodyResponseStream;    private JspPrintWriter _jspPrintWriter;  private int _bufferSize = 8192;  private boolean autoFlush;  private BodyContentImpl _bodyOut;  private Locale _locale;  private BundleManager _bundleManager;  private VarEnv _varEnv;  private Node _nodeEnv;  private final CharBuffer _cb = new CharBuffer();  private VariableResolver _varResolver;  private ELContext _elContext;  private ELResolver _elResolver;  private javax.el.FunctionMapper _functionMapper;  private PageVariableMapper _variableMapper;  private boolean _hasException;  private HashMap<String,Method> _functionMap;  private ExpressionEvaluatorImpl _expressionEvaluator;  PageContextImpl()  {    _attributes = new HashMapImpl<String,Object>();        _bodyResponseStream = new BodyResponseStream();    _bodyResponseStream.start();        _jspPrintWriter = new JspPrintWriter();  }  public PageContextImpl(WebApp webApp, Servlet servlet)  {    this();    if (webApp == null)      throw new NullPointerException();        _webApp = webApp;    _servlet = servlet;    if (servlet instanceof Page) {      Page page = (Page) servlet;      _functionMap = page._caucho_getFunctionMap();    }    else      _functionMap = null;  }  public PageContextImpl(WebApp webApp, HashMap<String,Method> functionMap)  {    this();    if (webApp == null)      throw new NullPointerException();        _webApp = webApp;        _functionMap = functionMap;  }  public void initialize(Servlet servlet,			 ServletRequest request,			 ServletResponse response,			 String errorPage,			 boolean needsSession,			 int bufferSize,			 boolean autoFlush)  {    HttpSession session = null;        if (needsSession)      session = ((HttpServletRequest) request).getSession(true);    ServletConfig config = servlet.getServletConfig();    WebApp app = (WebApp) config.getServletContext();    initialize(servlet, app, request, response,	       errorPage, session, bufferSize, autoFlush,	       false);  }  public void initialize(Servlet servlet,			 WebApp app,			 ServletRequest request,			 ServletResponse response,			 String errorPage,			 HttpSession session,			 int bufferSize,			 boolean autoFlush,			 boolean isPrintNullAsBlank)  {    _servlet = servlet;    _request = (HttpServletRequest) request;    if (response instanceof CauchoResponse	&& bufferSize <= TempCharBuffer.SIZE) {      _response = (CauchoResponse) response;      _responseAdapter = null;    }    else {      // JSP.12.2.3 - JSP must use PrintWriter      _responseAdapter = ToCharResponseAdapter.create((HttpServletResponse) response);      _response = _responseAdapter;      try {	// jsp/017m	response.setBufferSize(bufferSize);      } catch (Exception e) {	log.log(Level.FINE, e.toString(), e);      }    }        _responseStream = _response.getResponseStream();    _topOut = _jspAdapter;    _responseStream.setAutoFlush(autoFlush);    _jspAdapter.init(null, _responseStream);    _jspAdapter.setPrintNullAsBlank(isPrintNullAsBlank);    if (bufferSize != TempCharBuffer.SIZE) {      try {	_responseStream.setBufferSize(bufferSize);      } catch (Exception e) {	log.log(Level.FINE, e.toString(), e);      }    }    // needed for includes from static pages    _bufferSize = bufferSize;    this.autoFlush = autoFlush;    _session = session;    _out = _topOut;        _errorPage = errorPage;    _webApp = app;    _locale = null;    // major performance issue if null    //_elContext = null;    _hasException = false;    //if (_attributes.size() > 0)    //  _attributes.clear();    _isFilled = false;    _bundleManager = null;    _varResolver = null;    _nodeEnv = null;    if (servlet instanceof Page) {      Page page = (Page) servlet;      _functionMap = page._caucho_getFunctionMap();    }    else      _functionMap = null;  }  protected void init()  {    _elContext = null;  }  protected void setOut(JspWriter out)  {    _out = out;  }  protected void clearAttributes()  {    _attributes.clear();  }  /**   * Returns the page attribute with the given name.   *   * @param name the attribute name.   *   * @return the attribute's value.   */  public Object getAttribute(String name)  {    if (name == null)      throw new NullPointerException(L.l("getAttribute must have a non-null name"));        Object value = _attributes.get(name);    if (value != null)      return value;    else if (! _isFilled) {      fillAttribute();      value = _attributes.get(name);    }    if (value != null) {    }    else if (name.equals(OUT)) {      // jsp/162d      return _out;    }    return value;  }  /**   * Sets the page attribute with the given name.   *   * @param name the attribute name.   * @param value the new value   */  public void setAttribute(String name, Object attribute)  {    if (name == null)      throw new NullPointerException(L.l("setAttribute must have a non-null name"));        if (attribute != null)      _attributes.put(name, attribute);    else      _attributes.remove(name);  }  /**   * Sets the page attribute with the given name.   *   * @param name the attribute name.   * @param value the new value   */  public Object putAttribute(String name, Object attribute)  {    if (name == null)      throw new NullPointerException(L.l("putAttribute must have a non-null name"));        if (attribute != null)      return _attributes.put(name, attribute);    else      return _attributes.remove(name);  }  /**   * Removes a named attribute from the page context.   *   * @param name the name of the attribute to remove   */  public void removeAttribute(String name)  {    if (name == null)      throw new NullPointerException(L.l("removeAttribute must have a non-null name"));        _attributes.remove(name);    // jsp/162b    if (_request != null)      _request.removeAttribute(name);    if (_session != null) {      try {	_session.removeAttribute(name);      } catch (IllegalStateException e) {	// jsp/162f	log.log(Level.FINE, e.toString(), e);      }    }        if (_webApp != null)      _webApp.removeAttribute(name);  }  public Enumeration<String> getAttributeNames()  {    if (! _isFilled)      fillAttribute();    return Collections.enumeration(_attributes.keySet());  }  /**   * Fills the predefined page content _attributes with their values.   */  protected void fillAttribute()  {    _isFilled = true;    _attributes.put(PAGE, _servlet);    _attributes.put(PAGECONTEXT, this);    _attributes.put(REQUEST, getCauchoRequest());    _attributes.put(RESPONSE, getCauchoResponse());    if (_servlet != null)      _attributes.put(CONFIG, _servlet.getServletConfig());    if (getSession() != null)      _attributes.put(SESSION, getSession());    _attributes.put(APPLICATION, getApplication());  }  public Object getAttribute(String name, int scope)  {    switch (scope) {    case PAGE_SCOPE:      return getAttribute(name);    case REQUEST_SCOPE:      return getCauchoRequest().getAttribute(name);    case SESSION_SCOPE:      {	HttpSession session = getSession();	return session != null ? session.getValue(name) : null;      }    case APPLICATION_SCOPE:      return getApplication().getAttribute(name);    default:      throw new IllegalArgumentException();    }  }  public void setAttribute(String name, Object value, int scope)  {    switch (scope) {    case PAGE_SCOPE:      setAttribute(name, value);      break;    case REQUEST_SCOPE:      getCauchoRequest().setAttribute(name, value);      break;    case SESSION_SCOPE:      if (getSession() != null)        getSession().putValue(name, value);      break;    case APPLICATION_SCOPE:      getApplication().setAttribute(name, value);      break;    default:      throw new IllegalArgumentException();    }  }  public void removeAttribute(String name, int scope)  {    if (name == null)      throw new NullPointerException(L.l("removeAttribute must have a non-null name"));        switch (scope) {    case PAGE_SCOPE:      if (name != null)	_attributes.remove(name);      break;    case REQUEST_SCOPE:      getCauchoRequest().removeAttribute(name);      break;    case SESSION_SCOPE:      if (getSession() != null)        getSession().removeValue(name);      break;    case APPLICATION_SCOPE:      getApplication().removeAttribute(name);      break;    default:      throw new IllegalArgumentException();    }  }  public Enumeration getAttributeNames(int scope)  {    switch (scope) {    case PAGE_SCOPE:      return getAttributeNames();    case REQUEST_SCOPE:      return getCauchoRequest().getAttributeNames();    case SESSION_SCOPE:      if (getSession() != null)        return new StringArrayEnum(getSession().getValueNames());      else        return NullEnumeration.create();    case APPLICATION_SCOPE:      return getApplication().getAttributeNames();    default:      throw new IllegalArgumentException();    }

⌨️ 快捷键说明

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