connectioncontext.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,389 行 · 第 1/5 页
JAVA
2,389 行
/* * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Caucho Technology (http://www.caucho.com/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "Hessian", "Resin", and "Caucho" must not be used to * endorse or promote products derived from this software without prior * written permission. For written permission, please contact * info@caucho.com. * * 5. Products derived from this software may not be called "Resin" * nor may "Resin" appear in their names without prior written * permission of Caucho Technology. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @author Sam */package com.caucho.portal.generic.context;import com.caucho.portal.generic.*;import javax.portlet.*;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;/** * ConnectionContext tracks the state of a PortletConnection and provides * methods for all operations that affect the connection. * * InterfaceImpl classes implement interfaces and delegate all operations to * the ConnectionContext. * * <ul> * <li>{@link com.caucho.portal.generic.context.ActionImpl} * <li>{@link com.caucho.portal.generic.context.RenderImpl} * <li>{@link com.caucho.portal.generic.context.PortalRequestImpl} * <li>{@link com.caucho.portal.generic.context.PortalResponseImpl} * </ul> * * <ul> * <li>{@link com.caucho.portal.generic.context.PortletRequestImpl} * <li>{@link com.caucho.portal.generic.context.PortletResponseImpl} * <li>{@link com.caucho.portal.generic.context.ActionRequestImpl} * <li>{@link com.caucho.portal.generic.context.ActionResponseImpl} * <li>{@link com.caucho.portal.generic.context.RenderRequestImpl} * <li>{@link com.caucho.portal.generic.context.RenderResponseImpl} * </ul> * * Some of the state of the connection depends on the "current window". The * ConnectionContext maintains a stack of {@link WindowContext}. */ public class ConnectionContext { private static final Logger log = PortletConnection.log; private static final String EXPIRATION_CACHE = "javax.portlet.expirationCache"; private static final String RENDER_REQUEST = "javax.portlet.renderRequest"; private static final String RENDER_RESPONSE = "javax.portlet.renderResponse"; private static final String PORTLET_CONFIG = "javax.portlet.portletConfig"; private static final Locale LOCALE_ANY = new Locale("", "", ""); ActionImpl _action = new ActionImpl(this); RenderImpl _render = new RenderImpl(this); PortalRequestImpl _portalRequest = new PortalRequestImpl(this); PortalResponseImpl _portalResponse = new PortalResponseImpl(this); PortletRequestImpl _portletRequest = new PortletRequestImpl(this); PortletResponseImpl _portletResponse = new PortletResponseImpl(this); ActionRequestImpl _actionRequest = new ActionRequestImpl(this); ActionResponseImpl _actionResponse = new ActionResponseImpl(this); RenderRequestImpl _renderRequest = new RenderRequestImpl(this); RenderResponseImpl _renderResponse = new RenderResponseImpl(this); final static int STAGE_START = 1; final static int STAGE_ACTION = 2; final static int STAGE_DONEACTION = 3; final static int STAGE_RENDER = 4; final static int STAGE_DONE = 5; private PortletConnection _connection; private InvocationFactory _invocationFactory; private TopLevelResponseHandler _topLevelResponseHandler; int _connectionExpirationCache = -1; boolean _connectionIsPrivate = false; private boolean _forbidRedirect; private PreferencesStore _preferencesStore; private UserAttributeStore _userAttributeStore; private int _stage = STAGE_START; private Map<String, WindowContext> _windowContextMap = new HashMap<String, WindowContext>(); private ArrayList<WindowContext> _windowContextStack = new ArrayList<WindowContext>(); private WindowContext _windowContext; // current public ConnectionContext(PortletConnection connection) { _connection = connection; _topLevelResponseHandler = new TopLevelResponseHandler(connection, this); } public void start( InvocationFactory invocationFactory ) { log(Level.FINER, "starting connection"); if (_stage != STAGE_START || _invocationFactory != null) throw new IllegalStateException("missing finish()? " + _stage); _invocationFactory = invocationFactory; } public void finish() { log(Level.FINEST, "finishing connection"); try { for (int i = _windowContextStack.size() - 1; i >= 0; i--) { try { WindowContext windowContext = _windowContextStack.get(i); if (windowContext != null) finishWindowContext(windowContext); } catch (IOException ex) { log(Level.WARNING, ex.toString(), ex); } } } finally { _windowContextStack.clear(); } _windowContextMap.clear(); _windowContext = null; _stage = STAGE_START; _forbidRedirect = false; _preferencesStore = null; _userAttributeStore = null; _connection = null; _invocationFactory = null; } /** * finish a WindowContext that is no longer needed for this connection */ private void finishWindowContext(WindowContext windowContext) throws IOException { Map<String, String> userAttributeMap = windowContext.getUserAttributeMap(); LinkingPortletPreferences pref = windowContext.getPreferences(); windowContext.finish(); if (pref != null) { Map<String, String[]> store = pref.getStore(); pref.finish(); getPreferencesStore().finish(store); } if (userAttributeMap != null) { getUserAttributeStore().finish(userAttributeMap); } } protected void log(Level level, String message) { if (!log.isLoggable(level)) return; String namespace = _windowContext == null ? null : _windowContext.getNamespace(); log(namespace, level, message, null); } protected void log(Level level, String message, Exception ex) { if (!log.isLoggable(level)) return; String namespace = _windowContext == null ? null : _windowContext.getNamespace(); log(namespace, level, message, ex); } protected void log(String namespace, Level level, String message) { if (!log.isLoggable(level)) return; log(namespace, level, message, null); } protected void log(String namespace, Level level, String message, Exception ex) { if (log.isLoggable(level)) { StringBuffer sb = new StringBuffer(256); sb.append('['); sb.append(_connection.getId()); if (namespace != null) { sb.append(' '); sb.append(namespace); } sb.append(']'); if (_stage == STAGE_ACTION) sb.append('a'); else if (_stage == STAGE_RENDER) sb.append('r'); else sb.append(' '); sb.append(' '); sb.append(message); if (ex == null) log.log(level, sb.toString()); else log.log(level, sb.toString(), ex); } } protected PortletConnection getConnection() { return _connection; } protected InvocationFactory getInvocationFactory() { return _invocationFactory; } protected void setConnectionFailed(Exception ex) { _connection.setConnectionFailed(ex); } protected void setConnectionFailed() { _connection.setConnectionFailed(); } protected boolean isConnectionFailed() { return _connection.isConnectionFailed(); } public int getConnectionExpirationCache() { return _connectionExpirationCache; } protected void updateConnectionExpirationCache(int expirationCache) { if (_connectionExpirationCache != 0 && expirationCache >= 0 && _connectionExpirationCache < expirationCache) { _connectionExpirationCache = expirationCache; } } public boolean isConnectionPrivate() { return _connectionIsPrivate; } protected void setConnectionPrivate() { _connectionIsPrivate = true; } protected void setForbidRedirect() { _forbidRedirect = true; } protected boolean isForbidRedirect() { return _forbidRedirect; } public PortalRequest getPortalRequest() { return _portalRequest; } public PortalResponse getPortalResponse() { return _portalResponse; } public Portal getPortal() { return _connection.getPortal(); } public PortalContext getPortalContext() { return _connection.getPortal().getPortalContext(); } protected PreferencesStore getPreferencesStore() { if (_preferencesStore == null) _preferencesStore = getPortal().getPreferencesStore(); return _preferencesStore; } protected UserAttributeStore getUserAttributeStore() { if (_userAttributeStore == null) _userAttributeStore = getPortal().getUserAttributeStore(); return _userAttributeStore; } private void pushStack(WindowContext windowContext) { _windowContextStack.add(_windowContext); _windowContext = windowContext; } private void popStack() { try { if (_windowContextStack.size() == 0) throw new IllegalStateException( "top of window stack reached, extra finish()?"); WindowContext windowContext = _windowContext; _windowContext = null; if (_stage == STAGE_RENDER) finishWindowContext(windowContext); _windowContext = _windowContextStack.remove( _windowContextStack.size() - 1 ); } catch (Exception ex) { setConnectionFailed(ex); } } /** * See if the Window can handle the exception. Return true * if the exception has been handled in some way. * * Side-effect: might set windows.isExcluded to true * might set window.setException to null if it is handled */ protected void handleException() throws PortletException, IOException { final Exception exception = _windowContext.getException(); if (exception == null) return; if (log.isLoggable(Level.FINER)) log(Level.FINER, "handling exception: " + exception.getClass().getName()); final WindowContext windowContext = _windowContext; ExceptionEvent event = new ExceptionEvent() { public Exception getException() { return exception; } public void setHandled(boolean hideWindow) { windowContext.setException(null); if (hideWindow) windowContext.setExcluded(); } public boolean isHandled() { return !windowContext.isException(); } public boolean isHideWindow() { return windowContext.isExcluded(); } }; Window window = getWindow(); window.handleException( getRenderRequest(), getRenderResponse(), event ); } /** * Check for constraint failures and if any are encountered * set the _windowContext appropriately. */ protected void checkConstraints() { ArrayList<Constraint> constraints = getWindow().getConstraints(); if (constraints == null) return; if (log.isLoggable(Level.FINER)) log(Level.FINER, "checking constraints"); int startIndex = _windowContext.getConstraintIndex();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?