/*
* Copyright 2002-2006,2009 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.opensymphony.xwork2.ognl; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.XWorkConstants;
import com.opensymphony.xwork2.XWorkException;
import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor;
import com.opensymphony.xwork2.util.ClearableValueStack;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.MemberAccessValueStack;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import com.opensymphony.xwork2.util.logging.LoggerUtils;
import com.opensymphony.xwork2.util.reflection.ReflectionContextState;
import ognl.*; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern; /**
* Ognl implementation of a value stack that allows for dynamic Ognl expressions to be evaluated against it. When evaluating an expression,
* the stack will be searched down the stack, from the latest objects pushed in to the earliest, looking for a bean with a getter or setter
* for the given property or a method of the given name (depending on the expression being evaluated).
*
* @author Patrick Lightbody
* @author tm_jee
* @version $Date: 2013-05-28 22:14:15 +0200 (Di, 28 Mai 2013) $ $Id: OgnlValueStack.java 1487092 2013-05-28 20:14:15Z lukaszlenart $
*/
public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack { public static final String THROW_EXCEPTION_ON_FAILURE = OgnlValueStack.class.getName() + ".throwExceptionOnFailure"; private static final long serialVersionUID = 370737852934925530L; private static final String MAP_IDENTIFIER_KEY = "com.opensymphony.xwork2.util.OgnlValueStack.MAP_IDENTIFIER_KEY";
private static final Logger LOG = LoggerFactory.getLogger(OgnlValueStack.class); CompoundRoot root;
transient Map<String, Object> context;
Class defaultType;
Map<Object, Object> overrides;
transient OgnlUtil ognlUtil;
transient SecurityMemberAccess securityMemberAccess; private boolean devMode;
private boolean logMissingProperties; protected OgnlValueStack(XWorkConverter xworkConverter, CompoundRootAccessor accessor, TextProvider prov, boolean allowStaticAccess) {
setRoot(xworkConverter, accessor, new CompoundRoot(), allowStaticAccess);
push(prov);
} protected OgnlValueStack(ValueStack vs, XWorkConverter xworkConverter, CompoundRootAccessor accessor, boolean allowStaticAccess) {
setRoot(xworkConverter, accessor, new CompoundRoot(vs.getRoot()), allowStaticAccess);
} @Inject
public void setOgnlUtil(OgnlUtil ognlUtil) {
this.ognlUtil = ognlUtil;
} protected void setRoot(XWorkConverter xworkConverter, CompoundRootAccessor accessor, CompoundRoot compoundRoot,
boolean allowStaticMethodAccess) {
this.root = compoundRoot;
this.securityMemberAccess = new SecurityMemberAccess(allowStaticMethodAccess);
this.context = Ognl.createDefaultContext(this.root, accessor, new OgnlTypeConverterWrapper(xworkConverter), securityMemberAccess);
context.put(VALUE_STACK, this);
Ognl.setClassResolver(context, accessor);
((OgnlContext) context).setTraceEvaluations(false);
((OgnlContext) context).setKeepLastEvaluation(false);
} @Inject(XWorkConstants.DEV_MODE)
public void setDevMode(String mode) {
devMode = "true".equalsIgnoreCase(mode);
} @Inject(value = "logMissingProperties", required = false)
public void setLogMissingProperties(String logMissingProperties) {
this.logMissingProperties = "true".equalsIgnoreCase(logMissingProperties);
} /**
* @see com.opensymphony.xwork2.util.ValueStack#getContext()
*/
public Map<String, Object> getContext() {
return context;
} /**
* @see com.opensymphony.xwork2.util.ValueStack#setDefaultType(java.lang.Class)
*/
public void setDefaultType(Class defaultType) {
this.defaultType = defaultType;
} /**
* @see com.opensymphony.xwork2.util.ValueStack#setExprOverrides(java.util.Map)
*/
public void setExprOverrides(Map<Object, Object> overrides) {
if (this.overrides == null) {
this.overrides = overrides;
} else {
this.overrides.putAll(overrides);
}
} /**
* @see com.opensymphony.xwork2.util.ValueStack#getExprOverrides()
*/
public Map<Object, Object> getExprOverrides() {
return this.overrides;
} /**
* @see com.opensymphony.xwork2.util.ValueStack#getRoot()
*/
public CompoundRoot getRoot() {
return root;
} /**
* @see com.opensymphony.xwork2.util.ValueStack#setParameter(String, Object)
*/
public void setParameter(String expr, Object value) {
setValue(expr, value, devMode, false);
} /** /**
* @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object)
*/
public void setValue(String expr, Object value) {
setValue(expr, value, devMode);
} /**
* @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object, boolean)
*/
public void setValue(String expr, Object value, boolean throwExceptionOnFailure) {
setValue(expr, value, throwExceptionOnFailure, true);
} private void setValue(String expr, Object value, boolean throwExceptionOnFailure, boolean evalExpression) {
Map<String, Object> context = getContext();
try {
trySetValue(expr, value, throwExceptionOnFailure, context, evalExpression);
} catch (OgnlException e) {
handleOgnlException(expr, value, throwExceptionOnFailure, e);
} catch (RuntimeException re) { //XW-281
handleRuntimeException(expr, value, throwExceptionOnFailure, re);
} finally {
cleanUpContext(context);
}
} private void trySetValue(String expr, Object value, boolean throwExceptionOnFailure, Map<String, Object> context, boolean evalExpression) throws OgnlException {
context.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, expr);
context.put(REPORT_ERRORS_ON_NO_PROP, (throwExceptionOnFailure) ? Boolean.TRUE : Boolean.FALSE);
ognlUtil.setValue(expr, context, root, value, evalExpression);
} private void cleanUpContext(Map<String, Object> context) {
ReflectionContextState.clear(context);
context.remove(XWorkConverter.CONVERSION_PROPERTY_FULLNAME);
context.remove(REPORT_ERRORS_ON_NO_PROP);
} private void handleRuntimeException(String expr, Object value, boolean throwExceptionOnFailure, RuntimeException re) {
if (throwExceptionOnFailure) {
String message = ErrorMessageBuilder.create()
.errorSettingExpressionWithValue(expr, value)
.build();
throw new XWorkException(message, re);
} else {
if (LOG.isWarnEnabled()) {
LOG.warn("Error setting value", re);
}
}
} private void handleOgnlException(String expr, Object value, boolean throwExceptionOnFailure, OgnlException e) {
String msg = "Error setting expression '" + expr + "' with value '" + value + "'";
if (LOG.isWarnEnabled()) {
LOG.warn(msg, e);
}
if (throwExceptionOnFailure) {
throw new XWorkException(msg, e);
}
} /**
* @see com.opensymphony.xwork2.util.ValueStack#findString(java.lang.String)
*/
public String findString(String expr) {
return (String) findValue(expr, String.class);
} public String findString(String expr, boolean throwExceptionOnFailure) {
return (String) findValue(expr, String.class, throwExceptionOnFailure);
} /**
* @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String)
*/
public Object findValue(String expr, boolean throwExceptionOnFailure) {
try {
setupExceptionOnFailure(throwExceptionOnFailure);
return tryFindValueWhenExpressionIsNotNull(expr);
} catch (OgnlException e) {
return handleOgnlException(expr, throwExceptionOnFailure, e);
} catch (Exception e) {
return handleOtherException(expr, throwExceptionOnFailure, e);
} finally {
ReflectionContextState.clear(context);
}
} private void setupExceptionOnFailure(boolean throwExceptionOnFailure) {
if (throwExceptionOnFailure) {
context.put(THROW_EXCEPTION_ON_FAILURE, true);
}
} private Object tryFindValueWhenExpressionIsNotNull(String expr) throws OgnlException {
if (expr == null) {
return null;
}
return tryFindValue(expr);
} private Object handleOtherException(String expr, boolean throwExceptionOnFailure, Exception e) {
logLookupFailure(expr, e); if (throwExceptionOnFailure)
throw new XWorkException(e); return findInContext(expr);
} private Object tryFindValue(String expr) throws OgnlException {
Object value;
expr = lookupForOverrides(expr);
if (defaultType != null) {
value = findValue(expr, defaultType);
} else {
value = getValueUsingOgnl(expr);
if (value == null) {
value = findInContext(expr);
}
}
return value;
} private String lookupForOverrides(String expr) {
if ((overrides != null) && overrides.containsKey(expr)) {
expr = (String) overrides.get(expr);
}
return expr;
} private Object getValueUsingOgnl(String expr) throws OgnlException {
try {
return ognlUtil.getValue(expr, context, root);
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
} public Object findValue(String expr) {
return findValue(expr, false);
} /**
* @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String, java.lang.Class)
*/
public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) {
try {
setupExceptionOnFailure(throwExceptionOnFailure);
return tryFindValueWhenExpressionIsNotNull(expr, asType);
} catch (OgnlException e) {
return handleOgnlException(expr, throwExceptionOnFailure, e);
} catch (Exception e) {
return handleOtherException(expr, throwExceptionOnFailure, e);
} finally {
ReflectionContextState.clear(context);
}
} private Object tryFindValueWhenExpressionIsNotNull(String expr, Class asType) throws OgnlException {
if (expr == null) {
return null;
}
return tryFindValue(expr, asType);
} private Object handleOgnlException(String expr, boolean throwExceptionOnFailure, OgnlException e) {
Object ret = findInContext(expr);
if (ret == null) {
if (shouldLogNoSuchPropertyWarning(e)) {
LOG.warn("Could not find property [" + ((NoSuchPropertyException) e).getName() + "]");
}
if (throwExceptionOnFailure) {
throw new XWorkException(e);
}
}
return ret;
} private boolean shouldLogNoSuchPropertyWarning(OgnlException e) {
return e instanceof NoSuchPropertyException && devMode && logMissingProperties;
} private Object tryFindValue(String expr, Class asType) throws OgnlException {
Object value = null;
try {
expr = lookupForOverrides(expr);
value = getValue(expr, asType);
if (value == null) {
value = findInContext(expr);
}
} finally {
context.remove(THROW_EXCEPTION_ON_FAILURE);
}
return value;
} private Object getValue(String expr, Class asType) throws OgnlException {
return ognlUtil.getValue(expr, context, root, asType);
} private Object findInContext(String name) {
return getContext().get(name);
} public Object findValue(String expr, Class asType) {
return findValue(expr, asType, false);
} /**
* Log a failed lookup, being more verbose when devMode=true.
*
* @param expr The failed expression
* @param e The thrown exception.
*/
private void logLookupFailure(String expr, Exception e) {
String msg = LoggerUtils.format("Caught an exception while evaluating expression '#0' against value stack", expr);
if (devMode && LOG.isWarnEnabled()) {
LOG.warn(msg, e);
LOG.warn("NOTE: Previous warning message was issued due to devMode set to true.");
} else if (LOG.isDebugEnabled()) {
LOG.debug(msg, e);
}
} /**
* @see com.opensymphony.xwork2.util.ValueStack#peek()
*/
public Object peek() {
return root.peek();
} /**
* @see com.opensymphony.xwork2.util.ValueStack#pop()
*/
public Object pop() {
return root.pop();
} /**
* @see com.opensymphony.xwork2.util.ValueStack#push(java.lang.Object)
*/
public void push(Object o) {
root.push(o);
} /**
* @see com.opensymphony.xwork2.util.ValueStack#set(java.lang.String, java.lang.Object)
*/
public void set(String key, Object o) {
//set basically is backed by a Map pushed on the stack with a key being put on the map and the Object being the value
Map setMap = retrieveSetMap();
setMap.put(key, o);
} private Map retrieveSetMap() {
Map setMap;
Object topObj = peek();
if (shouldUseOldMap(topObj)) {
setMap = (Map) topObj;
} else {
setMap = new HashMap();
setMap.put(MAP_IDENTIFIER_KEY, "");
push(setMap);
}
return setMap;
} /**
* check if this is a Map put on the stack for setting if so just use the old map (reduces waste)
*/
private boolean shouldUseOldMap(Object topObj) {
return topObj instanceof Map && ((Map) topObj).get(MAP_IDENTIFIER_KEY) != null;
} /**
* @see com.opensymphony.xwork2.util.ValueStack#size()
*/
public int size() {
return root.size();
} private Object readResolve() {
// TODO: this should be done better
ActionContext ac = ActionContext.getContext();
Container cont = ac.getContainer();
XWorkConverter xworkConverter = cont.getInstance(XWorkConverter.class);
CompoundRootAccessor accessor = (CompoundRootAccessor) cont.getInstance(PropertyAccessor.class, CompoundRoot.class.getName());
TextProvider prov = cont.getInstance(TextProvider.class, "system");
boolean allow = "true".equals(cont.getInstance(String.class, "allowStaticMethodAccess"));
OgnlValueStack aStack = new OgnlValueStack(xworkConverter, accessor, prov, allow);
aStack.setOgnlUtil(cont.getInstance(OgnlUtil.class));
aStack.setRoot(xworkConverter, accessor, this.root, allow); return aStack;
} public void clearContextValues() {
//this is an OGNL ValueStack so the context will be an OgnlContext
//it would be better to make context of type OgnlContext
((OgnlContext) context).getValues().clear();
} public void setAcceptProperties(Set<Pattern> acceptedProperties) {
securityMemberAccess.setAcceptProperties(acceptedProperties);
} public void setPropertiesJudge(PropertiesJudge judge) {
securityMemberAccess.setPropertiesJudge(judge);
} public void setExcludeProperties(Set<Pattern> excludeProperties) {
securityMemberAccess.setExcludeProperties(excludeProperties);
} }

OgnlValueStack 源码的更多相关文章

  1. struts2拦截器源码分析

    前面博客我们介绍了开发struts2应用程序的基本流程(开发一个struts2的实例),通过前面我们知道了struts2实现请求转发和配置文件加载都是拦截器进行的操作,这也就是为什么我们要在web.x ...

  2. 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新

    本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...

  3. C# ini文件操作【源码下载】

    介绍C#如何对ini文件进行读写操作,C#可以通过调用[kernel32.dll]文件中的 WritePrivateProfileString()和GetPrivateProfileString()函 ...

  4. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  5. 从源码看Azkaban作业流下发过程

    上一篇零散地罗列了看源码时记录的一些类的信息,这篇完整介绍一个作业流在Azkaban中的执行过程,希望可以帮助刚刚接手Azkaban相关工作的开发.测试. 一.Azkaban简介 Azkaban作为开 ...

  6. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  7. 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新

    上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...

  8. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

  9. SDWebImage源码解读之SDWebImageDownloaderOperation

    第七篇 前言 本篇文章主要讲解下载操作的相关知识,SDWebImageDownloaderOperation的主要任务是把一张图片从服务器下载到内存中.下载数据并不难,如何对下载这一系列的任务进行设计 ...

随机推荐

  1. WIN7_X64连接远程Oracle数据库

    当使用微软提供的驱动:Provider=MSDAORA时,要使用32位的Oracle客户端,不然会报错! 下载:Oracle Database Instant Client 11g x86

  2. POJ 2385 Apple Catching(01背包)

    01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...

  3. Android(java)学习笔记81:在TextView组件中利用Html插入文字或图片

    1. TextView中利用Html插入文字或者图片: 首先我们看看代码: (1)activity_main.xml: <LinearLayout xmlns:android="htt ...

  4. java算法面试题:递归算法题1

    递归算法题1 一个整数,大于0,不用循环和本地变量,按照n,2n,4n,8n的顺序递增,当值大于5000时,把值按照指定顺序输出来.例:n=1237则输出为:1237,2474,4948,9896,9 ...

  5. React 服务端渲染最佳解决方案

    最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...

  6. bat 服务启动脚本

    当电脑上有多个数据库(特别是Oracle,占用内存大,所以我都是设置为手动启动的,或者想在电脑上运行一下其他UI类软件或玩些游戏的时候也需要暂时关掉,奈何我这渣机(V_V))需要启动或停止的时候,就用 ...

  7. Java - 类加载的时候,是有缺省同步锁的

    类加载的时候,是有缺省同步锁的

  8. js中的日期

    创建日期对象: var date1 = new Date(2018, 11,10)  第二个参数传入的是月份,月份是0-11,实际上要加1 获得现在的时间:var date2 = Date.now() ...

  9. 九、MySQL 创建数据表

    MySQL 创建数据表 创建MySQL数据表需要以下信息: 表名 表字段名 定义每个表字段 语法 以下为创建MySQL数据表的SQL通用语法: CREATE TABLE table_name (col ...

  10. 【IDEA】热部署插件Jrebel破解安装

    JRebel 介绍 IDEA上原生是不支持热部署的,一般更新了 Java 文件后要手动重启 Tomcat 服务器,才能生效,浪费不少生命啊.目前对于idea热部署最好的解决方案就是安装JRebel插件 ...