首页

关于icefaces源码包EnvUtils环境工具类对系统环境配置处理源码说明

标签:icefaces,EnvUtils,环境工具类     发布时间:2018-06-27   

一、前言

关于icefaces源码包(ice-3.4.2.jar)中org.icefaces.util.EnvUtils环境配置及处理工具类,实现基于org.icefaces.util.EnvConfig环境配置的处理,详情参见源码说明。

二、源码说明

1.EnvUtils工具类

package org.icefaces.util;@b@@b@import java.util.Map;@b@import java.util.logging.Level;@b@import java.util.logging.Logger;@b@import javax.faces.application.Application;@b@import javax.faces.application.Resource;@b@import javax.faces.application.ResourceHandler;@b@import javax.faces.component.UIViewRoot;@b@import javax.faces.context.ExternalContext;@b@import javax.faces.context.FacesContext;@b@@b@public class EnvUtils@b@{@b@  private static Logger log = Logger.getLogger(EnvUtils.class.getName());@b@  public static String ICEFACES_ENV_CONFIG = "org.icefaces.env.config";@b@  public static String ICEFACES_AUTO = "org.icefaces.render.auto";@b@  public static String ICEFACES_AUTOID = "org.icefaces.autoid";@b@  public static String COMPRESS_DOM = "org.icefaces.compressDOM";@b@  public static String COMPRESS_RESOURCES = "org.icefaces.compressResources";@b@  public static String DELTA_SUBMT = "org.icefaces.deltaSubmit";@b@  public static String LAZY_PUSH = "org.icefaces.lazyPush";@b@  public static String STANDARD_FORM_SERIALIZATION = "org.icefaces.standardFormSerialization";@b@  public static String STRICT_SESSION_TIMEOUT = "org.icefaces.strictSessionTimeout";@b@  public static String WINDOW_SCOPE_EXPIRATION = "org.icefaces.windowScopeExpiration";@b@  public static String MANDATORY_RESOURCE_CONFIG = "org.icefaces.mandatoryResourceConfiguration";@b@  public static String UNIQUE_RESOURCE_URLS = "org.icefaces.uniqueResourceURLs";@b@  public static String LAZY_WINDOW_SCOPE = "org.icefaces.lazyWindowScope";@b@  public static String DISABLE_DEFAULT_ERROR_POPUPS = "org.icefaces.disableDefaultErrorPopups";@b@  public static String CONNECTION_LOST_REDIRECT_URI = "org.icefaces.connectionLostRedirectURI";@b@  public static String SESSION_EXPIRED_REDIRECT_URI = "org.icefaces.sessionExpiredRedirectURI";@b@  public static String ICEFACES_RENDER = "org.icefaces.render";@b@  public static String ARIA_ENABLED = "org.icefaces.aria.enabled";@b@  public static String BLOCK_UI_ON_SUBMIT = "org.icefaces.blockUIOnSubmit";@b@  public static final String HEAD_DETECTED = "org.icefaces.headDetected";@b@  public static final String BODY_DETECTED = "org.icefaces.bodyDetected";@b@  private static String RESOURCE_PREFIX = "/javax.faces.resource/";@b@  private static String PATH_TEMPLATE = "org.icefaces.resource.pathTemplate";@b@  private static String DUMMY_RESOURCE = "bridge.js";@b@  private static String[] DEFAULT_TEMPLATE = { RESOURCE_PREFIX, ".jsf" };@b@  private static Class PortletSessionClass;@b@  private static Class PortletRequestClass;@b@  private static boolean icepushPresent;@b@  private static boolean mojarraPresent;@b@@b@  public static boolean isAriaEnabled(FacesContext facesContext)@b@  {@b@    UIViewRoot viewRoot = facesContext.getViewRoot();@b@    Map viewMap = viewRoot.getViewMap();@b@    Object ariaEnabled = viewMap.get(ARIA_ENABLED);@b@    if (null == ariaEnabled)@b@      return EnvConfig.getEnvConfig(facesContext).ariaEnabled;@b@@b@    return Boolean.TRUE.equals(ariaEnabled);@b@  }@b@@b@  public static boolean isAutoId(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).autoId;@b@  }@b@@b@  public static boolean isAutoRender(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).autoRender;@b@  }@b@@b@  public static boolean isBlockUIOnSubmit(FacesContext facesContext)@b@  {@b@    UIViewRoot viewRoot = facesContext.getViewRoot();@b@    Map viewMap = viewRoot.getViewMap();@b@    Object blockUIOnSubmit = viewMap.get(BLOCK_UI_ON_SUBMIT);@b@    if (null == blockUIOnSubmit)@b@      return EnvConfig.getEnvConfig(facesContext).blockUIOnSubmit;@b@@b@    return Boolean.TRUE.equals(blockUIOnSubmit);@b@  }@b@@b@  public static boolean isCompressDOM(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).compressDOM;@b@  }@b@@b@  public static boolean isCompressResources(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).compressResources;@b@  }@b@@b@  public static String getConnectionLostRedirectURI(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).connectionLostRedirectURI;@b@  }@b@@b@  public static boolean isDeltaSubmit(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).deltaSubmit;@b@  }@b@@b@  public static boolean isLazyPush(FacesContext facesContext)@b@  {@b@    UIViewRoot viewRoot = facesContext.getViewRoot();@b@    Map viewMap = viewRoot.getViewMap();@b@    Object lazyPush = viewMap.get(LAZY_PUSH);@b@    if (null == lazyPush)@b@      return EnvConfig.getEnvConfig(facesContext).lazyPush;@b@@b@    return Boolean.TRUE.equals(lazyPush);@b@  }@b@@b@  public static boolean isPartialStateSaving(FacesContext facesContext)@b@  {@b@    return (!("false".equalsIgnoreCase(FacesContext.getCurrentInstance().getExternalContext().getInitParameter("javax.faces.PARTIAL_STATE_SAVING"))));@b@  }@b@@b@  public static String getSessionExpiredRedirectURI(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).sessionExpiredRedirectURI;@b@  }@b@@b@  public static boolean isStandardFormSerialization(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).standardFormSerialization;@b@  }@b@@b@  public static boolean isStrictSessionTimeout(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).strictSessionTimeout;@b@  }@b@@b@  public static long getWindowScopeExpiration(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).windowScopeExpiration;@b@  }@b@@b@  public static String getMandatoryResourceConfig(FacesContext facesContext) {@b@    return EnvConfig.getEnvConfig(facesContext).mandatoryResourceConfig;@b@  }@b@@b@  public static boolean isUniqueResourceURLs(FacesContext facesContext) {@b@    return EnvConfig.getEnvConfig(facesContext).uniqueResourceURLs;@b@  }@b@@b@  public static boolean isICEfacesView(FacesContext facesContext)@b@  {@b@    UIViewRoot viewRoot = facesContext.getViewRoot();@b@    Map viewMap = viewRoot.getViewMap();@b@@b@    Object icefacesRender = viewMap.get(ICEFACES_RENDER);@b@    if (null == icefacesRender) {@b@      icefacesRender = Boolean.valueOf(EnvConfig.getEnvConfig(facesContext).autoRender);@b@      viewMap.put(ICEFACES_RENDER, icefacesRender);@b@    }@b@@b@    return Boolean.TRUE.equals(icefacesRender);@b@  }@b@@b@  public static boolean isICEpushPresent() {@b@    return icepushPresent;@b@  }@b@@b@  public static boolean hasHeadAndBodyComponents(FacesContext facesContext)@b@  {@b@    UIViewRoot viewRoot = facesContext.getViewRoot();@b@    Map viewMap = viewRoot.getViewMap();@b@    if ((!(viewMap.containsKey("org.icefaces.headDetected"))) || (!(viewMap.containsKey("org.icefaces.bodyDetected")))) {@b@      if (log.isLoggable(Level.FINE)) {@b@        log.log(Level.FINE, "ICEfaces disabled for view " + viewRoot.getViewId() + "\n  h:head tag available: " + viewMap.containsKey("org.icefaces.headDetected") + "\n  h:body tag available: " + viewMap.containsKey("org.icefaces.bodyDetected"));@b@      }@b@@b@      return false;@b@    }@b@    return true;@b@  }@b@@b@  public static boolean needViewStateHack()@b@  {@b@    return mojarraPresent;@b@  }@b@@b@  public static String[] getPathTemplate() {@b@    FacesContext facesContext = FacesContext.getCurrentInstance();@b@    Map applicationMap = facesContext.getExternalContext().getApplicationMap();@b@@b@    String[] pathTemplate = (String[])(String[])applicationMap.get(PATH_TEMPLATE);@b@    if (null != pathTemplate)@b@      return pathTemplate;@b@@b@    Resource dummyResource = facesContext.getApplication().getResourceHandler().createResource(DUMMY_RESOURCE);@b@@b@    if (null != dummyResource) {@b@      String dummyPath = dummyResource.getRequestPath();@b@      pathTemplate = extractPathTemplate(dummyPath);@b@    }@b@    if (null == pathTemplate)@b@      return DEFAULT_TEMPLATE;@b@@b@    applicationMap.put(PATH_TEMPLATE, pathTemplate);@b@    return pathTemplate;@b@  }@b@@b@  private static String[] extractPathTemplate(String path) {@b@    int start = path.indexOf(DUMMY_RESOURCE);@b@    String pre = path.substring(0, start);@b@    String post = path.substring(start + DUMMY_RESOURCE.length());@b@    return new String[] { pre, post };@b@  }@b@@b@  public static boolean instanceofPortletSession(Object session) {@b@    return ((PortletSessionClass != null) && (PortletSessionClass.isInstance(session)));@b@  }@b@@b@  public static boolean instanceofPortletRequest(Object request) {@b@    return ((PortletRequestClass != null) && (PortletRequestClass.isInstance(request)));@b@  }@b@@b@  public static boolean isPushRequest(FacesContext facesContext) {@b@    ExternalContext ec = facesContext.getExternalContext();@b@    String reqPath = ec.getRequestServletPath();@b@    String pathInfo = ec.getRequestPathInfo();@b@    String reqParam = (String)ec.getRequestParameterMap().get("ice.submit.type");@b@@b@    return (((reqPath != null) && (reqPath.contains("listen.icepush"))) || ((pathInfo != null) && (pathInfo.contains("listen.icepush"))) || ("ice.push".equals(reqParam)));@b@  }@b@@b@  public static boolean isLazyWindowScope(FacesContext facesContext)@b@  {@b@    return EnvConfig.getEnvConfig(facesContext).lazyWindowScope;@b@  }@b@@b@  public static boolean disableDefaultErrorPopups(FacesContext facesContext) {@b@    return EnvConfig.getEnvConfig(facesContext).disableDefaultErrorPopups;@b@  }@b@@b@  static@b@  {@b@    try@b@    {@b@      PortletSessionClass = Class.forName("javax.portlet.PortletSession");@b@      PortletRequestClass = Class.forName("javax.portlet.PortletRequest");@b@    } catch (Throwable t) {@b@      log.log(Level.FINE, "Portlet classes not available: ", t);@b@    }@b@@b@    try@b@    {@b@      Class.forName("org.icepush.PushContext");@b@      icepushPresent = true;@b@    } catch (ClassNotFoundException e) {@b@      icepushPresent = false;@b@    }@b@@b@    mojarraPresent = false;@b@    try@b@    {@b@      Class.forName("com.sun.faces.context.FacesContextImpl");@b@      mojarraPresent = true;@b@    } catch (Throwable e) {@b@      mojarraPresent = false;@b@    }@b@  }@b@}

2.EnvConfig类

package org.icefaces.util;@b@@b@import java.util.Map;@b@import java.util.logging.Logger;@b@import javax.faces.context.ExternalContext;@b@import javax.faces.context.FacesContext;@b@@b@class EnvConfig@b@{@b@  private static Logger log = Logger.getLogger(EnvConfig.class.getName());@b@  boolean autoRender;@b@  boolean autoId;@b@  boolean ariaEnabled;@b@  boolean blockUIOnSubmit;@b@  boolean compressDOM;@b@  boolean compressResources;@b@  String connectionLostRedirectURI;@b@  boolean deltaSubmit;@b@  boolean lazyPush;@b@  boolean pushActive;@b@  String sessionExpiredRedirectURI;@b@  boolean standardFormSerialization;@b@  boolean strictSessionTimeout;@b@  long windowScopeExpiration;@b@  String mandatoryResourceConfig;@b@  boolean uniqueResourceURLs;@b@  boolean lazyWindowScope;@b@  public boolean disableDefaultErrorPopups;@b@@b@  public EnvConfig(Map initMap)@b@  {@b@    init(initMap);@b@  }@b@@b@  public void init(Map initMap) {@b@    StringBuilder info = new StringBuilder();@b@@b@    this.autoRender = decodeBoolean(initMap, EnvUtils.ICEFACES_AUTO, true, info);@b@    this.autoId = decodeBoolean(initMap, EnvUtils.ICEFACES_AUTOID, true, info);@b@    this.ariaEnabled = decodeBoolean(initMap, EnvUtils.ARIA_ENABLED, true, info);@b@    this.blockUIOnSubmit = decodeBoolean(initMap, EnvUtils.BLOCK_UI_ON_SUBMIT, false, info);@b@    this.compressDOM = decodeBoolean(initMap, EnvUtils.COMPRESS_DOM, false, info);@b@    this.compressResources = decodeBoolean(initMap, EnvUtils.COMPRESS_RESOURCES, true, info);@b@    this.connectionLostRedirectURI = decodeString(initMap, EnvUtils.CONNECTION_LOST_REDIRECT_URI, null, info);@b@    this.deltaSubmit = decodeBoolean(initMap, EnvUtils.DELTA_SUBMT, false, info);@b@    this.lazyPush = decodeBoolean(initMap, EnvUtils.LAZY_PUSH, true, info);@b@    this.sessionExpiredRedirectURI = decodeString(initMap, EnvUtils.SESSION_EXPIRED_REDIRECT_URI, null, info);@b@    this.standardFormSerialization = decodeBoolean(initMap, EnvUtils.STANDARD_FORM_SERIALIZATION, false, info);@b@    this.strictSessionTimeout = decodeBoolean(initMap, EnvUtils.STRICT_SESSION_TIMEOUT, false, info);@b@    this.windowScopeExpiration = decodeLong(initMap, EnvUtils.WINDOW_SCOPE_EXPIRATION, 1000L, info);@b@    this.mandatoryResourceConfig = decodeString(initMap, EnvUtils.MANDATORY_RESOURCE_CONFIG, null, info);@b@    this.uniqueResourceURLs = decodeBoolean(initMap, EnvUtils.UNIQUE_RESOURCE_URLS, true, info);@b@    this.lazyWindowScope = decodeBoolean(initMap, EnvUtils.LAZY_WINDOW_SCOPE, true, info);@b@    this.disableDefaultErrorPopups = decodeBoolean(initMap, EnvUtils.DISABLE_DEFAULT_ERROR_POPUPS, false, info);@b@@b@    log.info("ICEfaces Configuration: \n" + info);@b@  }@b@@b@  public static EnvConfig getEnvConfig(FacesContext facesContext) {@b@    ExternalContext externalContext = facesContext.getExternalContext();@b@    Map appMap = externalContext.getApplicationMap();@b@    EnvConfig envConfig = (EnvConfig)appMap.get(EnvUtils.ICEFACES_ENV_CONFIG);@b@    if (null == envConfig) {@b@      envConfig = new EnvConfig(externalContext.getInitParameterMap());@b@      appMap.put(EnvUtils.ICEFACES_ENV_CONFIG, envConfig);@b@    }@b@    return envConfig;@b@  }@b@@b@  boolean decodeBoolean(Map map, String name, boolean defaultValue, StringBuilder info)@b@  {@b@    String paramValue = (String)map.get(name);@b@    if (null == paramValue) {@b@      info.append(name).append(": ").append(defaultValue).append(" [default]\n");@b@      return defaultValue;@b@    }@b@    if ("true".equalsIgnoreCase(paramValue)) {@b@      info.append(name).append(": true\n");@b@      return true;@b@    }@b@    if ("false".equalsIgnoreCase(paramValue)) {@b@      info.append(name).append(": false\n");@b@      return false;@b@    }@b@    info.append(name).append(": ").append(defaultValue).append(" [default replacing malformed non-boolean value: ").append(paramValue).append("]\n");@b@    return defaultValue;@b@  }@b@@b@  String decodeString(Map map, String name, String defaultValue, StringBuilder info) {@b@    String paramValue = (String)map.get(name);@b@    if (null == paramValue) {@b@      info.append(name).append(": ").append(defaultValue).append(" [default]\n");@b@      return defaultValue;@b@    }@b@    info.append(name).append(": ").append(paramValue).append("\n");@b@    return paramValue;@b@  }@b@@b@  long decodeLong(Map map, String name, long defaultValue, StringBuilder info) {@b@    String paramValue = (String)map.get(name);@b@    if (null == paramValue) {@b@      info.append(name).append(" = ").append(defaultValue).append(" [default]\n");@b@      return defaultValue;@b@    }@b@    try {@b@      return Long.parseLong(paramValue);@b@    } catch (Exception e) {@b@      info.append(name).append(": ").append(defaultValue).append(" [default replacing malformed long value: ").append(paramValue).append("]\n");@b@    }@b@    return defaultValue;@b@  }@b@}