mapbasedinvocationfactory.java

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

JAVA
736
字号
/* * 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;import javax.portlet.PortletMode;import javax.portlet.WindowState;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;/** */public class MapBasedInvocationFactory  implements InvocationFactory, Cloneable{  static protected final Logger log =     Logger.getLogger(MapBasedInvocationFactory.class.getName());  private String _reservedNamespace = "__";  private String _actionTargetParameterName = "A";  private String _windowStateParameterName = "W";  private String _portletModeParameterName = "M";  private LinkedHashMap<String, MapBasedInvocation> _invocationMap    = new LinkedHashMap<String, MapBasedInvocation>();  private String _actionNamespace;  private Set<WindowState> _windowStatesUsed;  private Set<PortletMode> _portletModesUsed;  // this class is not required to be thread-safe  private StringBuffer _buffer = new StringBuffer(256);  /**   * The reserved namespace is used to mark parameters that have special   * meaning to the portal.  The specification suggests "javax.portal.", which   * is rather long so the default is "__".     *    * This implementation also uses the reserved namespace as a prefix to   * parameter names.   */  public void setReservedNamespace(String namespace)  {    _reservedNamespace = namespace;  }  /**   * The reserved namespace is used to mark parameters that have special   * meaning to the portal.   */  public String getReservedNamespace()  {    return _reservedNamespace;  }  /**   * The name of the parameter to use to store the namespace of   * the target of an action.   */  public void setActionTargetParameterName(String name)  {    _actionTargetParameterName = name;  }  /**   * The name of the parameter to use to store the namespace of   * the target of an action.   */  public String getActionTargetParameterName()  {    return _actionTargetParameterName;  }  /**   * The name of the parameter to use to store the window state.   */  public void setWindowStateParameterName(String name)  {    _windowStateParameterName = name;  }  /**   * The name of the parameter to use to store the window state.   */  public String getWindowStateParameterName()  {    return _windowStateParameterName;  }  /**   * The name of the parameter to use to store the portlet mode.   */  public void setPortletModeParameterName(String name)  {    _portletModeParameterName = name;  }  /**   * The name of the parameter to use to store the portlet mode.   */  public String getPortletModeParameterName()  {    return _portletModeParameterName;  }  public void start(Map<String, String[]> rawParameters)  {    if (rawParameters != null)      decodeRawParameters(rawParameters);  }  public void finish()  {    _windowStatesUsed = null;    _portletModesUsed = null;    _actionNamespace = null;    _invocationMap.clear();  }  public boolean isActionTarget(String namespace)  {    return namespace == _actionNamespace;  }  /**   * The actionNamespace and actionMap are null in the returned clone   * The invocation that matches the passed namespace will not have any   * parameters.    */  protected MapBasedInvocationFactory clone(String namespace)  {    try {      MapBasedInvocationFactory clone         = (MapBasedInvocationFactory) super.clone();      clone._windowStatesUsed = null;      clone._portletModesUsed = null;      clone._actionNamespace = null;      clone._invocationMap = new LinkedHashMap<String, MapBasedInvocation>();      Iterator<Map.Entry<String, MapBasedInvocation>> iter         = _invocationMap.entrySet().iterator();      while (iter.hasNext()) {        Map.Entry<String, MapBasedInvocation> entry = iter.next();        String invocationNamespace = entry.getKey();        MapBasedInvocation invocation = entry.getValue();        boolean keepParameters = !namespace.equals(invocationNamespace);        clone._invocationMap.put( invocationNamespace,                                   invocation.clone(keepParameters) );      }      return clone;    }    catch (CloneNotSupportedException ex) {      throw new RuntimeException(ex);    }  }  protected void decodeRawParameters(Map<String, String[]> raw)  {    _buffer.setLength(0);    StringBuffer buf = _buffer;    buf.append(_reservedNamespace);    int len = buf.length();    buf.append(_reservedNamespace);    buf.append(_actionTargetParameterName);    String actionTargetParameterName = buf.toString();    buf.setLength(len);    buf.append(_windowStateParameterName);    String windowStateParameterName = buf.toString();    buf.setLength(len);    buf.append(_portletModeParameterName);    String portletModeParameterName = buf.toString();    MapBasedInvocation invocation = null;    // first, determine the target of the action, if any.    String[] action =  raw.get(actionTargetParameterName);     _actionNamespace = action == null || action.length == 0 ? null : action[0];    if (_actionNamespace != null)      getInvocation(_actionNamespace);    // iterate the parameters.    // parameters that begin with the _reservedNamespace are render    // parameters.    // parameters that do not begin with the _reservedNamespace are action    // parameters, and belong to the Invocation for _actionNamespace.    Iterator<Map.Entry<String, String[]>> iter = raw.entrySet().iterator();    while (iter.hasNext()) {      Map.Entry<String, String[]> entry = iter.next();      String key = entry.getKey();      String[] values = entry.getValue();      String namespace = null;      if (key.startsWith(_reservedNamespace)) {        int keyLen = key.length();        int st = _reservedNamespace.length();        int nd = key.indexOf('.', st);        if (nd > -1) {          namespace = key.substring(st, nd);          nd++;        }         else {          if ( log.isLoggable(Level.FINE)                && !key.equals(actionTargetParameterName) )            log.fine("unusable raw parameter name `" + key + "'");          continue;        }                if (nd < keyLen)          key = key.substring(nd);        else {          if ( log.isLoggable(Level.FINE) )             log.fine("unusable . raw parameter name `" + key + "'");          continue;        }      }      else {        if (_actionNamespace == null) {          log.finer("unusable raw action parameter name `" + key + "'");          continue;        } else          namespace = _actionNamespace;      }      if (invocation == null || !invocation.getNamespace().equals(namespace))        invocation = getInvocation(namespace);      if (windowStateParameterName.equals(key)) {        invocation.setWindowStateName(values);      }      else if (portletModeParameterName.equals(key)) {        invocation.setPortletModeName(values);      }      else {        invocation.getParameterMap().put(key, values);      }    }  }  public String getURL()  {    _buffer.setLength(0);    StringBuffer buf = _buffer;    StringBuffer url = _buffer;    if (_actionNamespace != null)      appendReserved(url, null, _actionTargetParameterName, _actionNamespace);    Iterator<Map.Entry<String, MapBasedInvocation>> iter       = _invocationMap.entrySet().iterator();    // "view" and "normal" are defaults.  They only to to be included    // in the url if:    // 1) some namespace uses view or normal and has no parameters    // 2) some other namespace uses a different portlet mode/window state    //    // They only need to be included once, the purpose for including them is so    // that on a subsequent request the windowStatesUsed and portletModesUsed    // are correct.    String viewPortletModeNamespace = null;    String normalWindowStateNamespace = null;    boolean needViewPortletMode = false;    boolean needNormalWindowState = false;    boolean sawActionNamespace = false;    while (iter.hasNext()) {      Map.Entry<String, MapBasedInvocation> entry = iter.next();      String namespace = entry.getKey();      MapBasedInvocation invocation = entry.getValue();      // when the namespace is the target of an action, the parameter      // names are not encoded      String paramNamespace = namespace == _actionNamespace ? null : namespace;      PortletMode portletMode = invocation.getPortletMode();      WindowState windowState = invocation.getWindowState();      Map<String, String[]> parameterMap = invocation._parameterMap;      boolean hasParameters = parameterMap != null && !parameterMap.isEmpty();      if (portletMode == PortletMode.VIEW) {        if (viewPortletModeNamespace == null && !hasParameters)          viewPortletModeNamespace = namespace;      }       else {        needViewPortletMode = true;        String key = _portletModeParameterName;        String value = portletMode.toString();        appendReserved( url, namespace, key, value );      }

⌨️ 快捷键说明

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