`
longgangbai
  • 浏览: 7341547 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Struts2.0 中值栈的实现以及解析OGNL以及值堆栈的原理

Go 
阅读更多

    OGNL的值栈实现的堆栈动态OGNL的表达式。何时设置一个表达式,堆栈将被搜索降低堆栈从推到最新的对象, 最早的一个getter或给定的表达式或给定名称的方法寻找表达式的值)。

public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessValueStack {

    private static final long serialVersionUID = 370737852934925530L;

    private static Logger LOG = LoggerFactory.getLogger(OgnlValueStack.class);
    private boolean devMode;
    private boolean logMissingProperties;

    public static void link(Map<String, Object> context, Class clazz, String name) {
        context.put("__link", new Object[]{clazz, name});
    }


    CompoundRoot root;
    transient Map<String, Object> context;
    Class defaultType;
    Map<Object, Object> overrides;
    transient OgnlUtil ognlUtil;

    transient SecurityMemberAccess securityMemberAccess;

    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);
    }

 

   //注入OgnlUtil对象

    @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("devMode")
    public void setDevMode(String mode) {
        devMode = "true".equalsIgnoreCase(mode);
    }

 

    @Inject(value = "logMissingProperties", required = false )
    public void setLogMissingProperties(String logMissingProperties) {
        this.logMissingProperties = "true".equalsIgnoreCase(logMissingProperties);
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#getContext()
     */
    public Map<String, Object> getContext() {
        return context;
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#setDefaultType(java.lang.Class)
     */
    public void setDefaultType(Class defaultType) {
        this.defaultType = defaultType;
    }

    /* (non-Javadoc)
     * @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);
        }
    }

    /* (non-Javadoc)
    * @see com.opensymphony.xwork2.util.ValueStack#getExprOverrides()
    */
    public Map<Object, Object> getExprOverrides() {
        return this.overrides;
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#getRoot()
     */
    public CompoundRoot getRoot() {
        return root;
    }

 

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object)
     */
    public void setValue(String expr, Object value) {
        setValue(expr, value, devMode);
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#setValue(java.lang.String, java.lang.Object, boolean)
     */
    public void setValue(String expr, Object value, boolean throwExceptionOnFailure) {
        Map<String, Object> context = getContext();

        try {
            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);
        } catch (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);
            }
        } catch (RuntimeException re) { //XW-281
            if (throwExceptionOnFailure) {
                StringBuilder msg = new StringBuilder();
                msg.append("Error setting expression '");
                msg.append(expr);
                msg.append("' with value ");

                 //值栈中数组的处理方式

                if (value instanceof Object[]) {
                    Object[] valueArray = (Object[]) value;
                    msg.append("[");
                    for (int index = 0; index < valueArray.length; index++) {
                        msg.append("'");
                        msg.append(valueArray[index]);
                        msg.append("'");

                        if (index < (valueArray.length + 1))
                            msg.append(", ");
                    }
                    msg.append("]");
                } else {
                    msg.append("'");
                    msg.append(value);
                    msg.append("'");
                }

                throw new XWorkException(msg.toString(), re);
            } else {
                if (LOG.isWarnEnabled()) {
                    LOG.warn("Error setting value", re);
                }
            }
        } finally {
            ReflectionContextState.clear(context);
            context.remove(XWorkConverter.CONVERSION_PROPERTY_FULLNAME);
            context.remove(REPORT_ERRORS_ON_NO_PROP);
        }
    }

    /* (non-Javadoc)
     * @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);
    }

 

    /* 从值栈中获取对象的值

 

   * (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String)
     */
    public Object findValue(String expr, boolean throwExceptionOnFailure) {
        try {
            if (expr == null) {
                return null;
            }

            if ((overrides != null) && overrides.containsKey(expr)) {
                expr = (String) overrides.get(expr);
            }

            if (defaultType != null) {
                return findValue(expr, defaultType);
            }

            Object value = ognlUtil.getValue(expr, context, root);
            if (value != null) {
                return value;
            } else {
                checkForInvalidProperties(expr, throwExceptionOnFailure, throwExceptionOnFailure);
                return findInContext(expr);
            }
        } catch (OgnlException e) {
            checkForInvalidProperties(expr, throwExceptionOnFailure, throwExceptionOnFailure);

            return findInContext(expr);
        } catch (Exception e) {
            logLookupFailure(expr, e);

            if (throwExceptionOnFailure)
                throw new XWorkException(e);

            return findInContext(expr);
        } finally {
            ReflectionContextState.clear(context);
        }
    }

     public Object findValue(String expr) {
         return findValue(expr, false);
     }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String, java.lang.Class)
     */
    public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) {
        try {
            if (expr == null) {
                return null;
            }

            if ((overrides != null) && overrides.containsKey(expr)) {
                expr = (String) overrides.get(expr);
            }

            Object value = ognlUtil.getValue(expr, context, root, asType);
            if (value != null) {
                return value;
            } else {
                checkForInvalidProperties(expr, throwExceptionOnFailure, throwExceptionOnFailure);
                return findInContext(expr);
            }
        } catch (OgnlException e) {
            checkForInvalidProperties(expr, throwExceptionOnFailure, throwExceptionOnFailure);
            return findInContext(expr);
        } catch (Exception e) {
            logLookupFailure(expr, e);

            if (throwExceptionOnFailure)
                throw new XWorkException(e);
           
            return findInContext(expr);
        } finally {
            ReflectionContextState.clear(context);
        }
    }

    private Object findInContext(String name) {
        return getContext().get(name);
    }

    public Object findValue(String expr, Class asType) {
        return findValue(expr, asType, false);
    }

    /**
     * This method looks for matching methods/properties in an action to warn the user if
     * they specified a property that doesn't exist.
     *
     * @param expr the property expression
     */
    private void checkForInvalidProperties(String expr, boolean throwExceptionPropNotFound, boolean throwExceptionMethodFound) {
        if (expr.contains("(") && expr.contains(")")) {
            if (devMode)
                LOG.warn("Could not find method [" + expr + "]");

            if (throwExceptionMethodFound)
                    throw new XWorkException("Could not find method [" + expr + "]");
        } else if (findInContext(expr) == null) {
            // find objects with Action in them and inspect matching getters
            Set<String> availableProperties = new LinkedHashSet<String>();
            for (Object o : root) {
                if (o instanceof ActionSupport || o.getClass().getSimpleName().endsWith("Action")) {
                    try {
                        findAvailableProperties(o.getClass(), expr, availableProperties, null);
                    } catch (IntrospectionException ise) {
                        // ignore
                    }
                }
            }
            if (!availableProperties.contains(expr)) {
                if (devMode && logMissingProperties) {
                    LOG.warn("Could not find property [" + expr + "]");
                }

                if (throwExceptionMethodFound)
                    throw new XWorkException("Could not find property [" + expr + "]");
            }
        }
    }

    /**
     * Look for available properties on an existing class.
     *
     * @param c                   the class to search on
     * @param expr                the property expression
     * @param availableProperties a set of properties found
     * @param parent              a parent property
     * @throws IntrospectionException when Ognl can't get property descriptors
     */
    private void findAvailableProperties(Class c, String expr, Set<String> availableProperties, String parent) throws IntrospectionException {
        PropertyDescriptor[] descriptors = ognlUtil.getPropertyDescriptors(c);
        for (PropertyDescriptor pd : descriptors) {
            String name = pd.getDisplayName();
            if (parent != null && expr.contains(".")) {
                name = expr.substring(0, expr.indexOf(".") + 1) + name;
            }
            if (expr.startsWith(name)) {
                availableProperties.add((parent != null) ? parent + "." + name : name);
                if (expr.equals(name)) break; // no need to go any further
                if (expr.contains(".")) {
                    String property = expr.substring(expr.indexOf(".") + 1);
                    // if there is a nested property (indicated by a dot), chop it off so we can look for method name
                    String rawProperty = (property.contains(".")) ? property.substring(0, property.indexOf(".")) : property;
                    String methodToLookFor = "get" + rawProperty.substring(0, 1).toUpperCase() + rawProperty.substring(1);
                    Method[] methods = pd.getPropertyType().getDeclaredMethods();
                    for (Method method : methods) {
                        if (method.getName().equals(methodToLookFor)) {
                            availableProperties.add(name + "." + rawProperty);
                            Class returnType = method.getReturnType();
                            findAvailableProperties(returnType, property, availableProperties, name);
                        }
                    }

                }
            }
        }
    }

    /**
     * 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);
        }
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#peek()
     */
    public Object peek() {
        return root.peek();
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#pop()
     */
    public Object pop() {
        return root.pop();
    }

    /* (non-Javadoc)
     * @see com.opensymphony.xwork2.util.ValueStack#push(java.lang.Object)
     */
    public void push(Object o) {
        root.push(o);
    }

    /* (non-Javadoc)
    * @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 = null;

        //check if this is a Map
        //put on the stack  for setting
        //if so just use the old map (reduces waste)
        Object topObj = peek();
        if (topObj instanceof Map
                && ((Map) topObj).get(MAP_IDENTIFIER_KEY) != null) {

            setMap = (Map) topObj;
        } else {
            setMap = new HashMap();
            //the map identifier key ensures
            //that this map was put there
            //for set purposes and not by a user
            //whose data we don't want to touch
            setMap.put(MAP_IDENTIFIER_KEY, "");
            push(setMap);
        }
        setMap.put(key, o);

    }


    private static final String MAP_IDENTIFIER_KEY = "com.opensymphony.xwork2.util.OgnlValueStack.MAP_IDENTIFIER_KEY";

    /* (non-Javadoc)
    * @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 setExcludeProperties(Set<Pattern> excludeProperties) {
       securityMemberAccess.setExcludeProperties(excludeProperties);
    }
}

 

分享到:
评论

相关推荐

    struts2的值栈

    用一个小小的flash来形象的演示struts2中值栈的操作过程,非常直观!

    中值滤波原理及MATLAB实现..pdf

    中值滤波原理及MATLAB实现 中值滤波是一种非线性信号处理技术,广泛应用于图像处理领域,特别是在去除脉冲干扰噪声方面有着出色的性能。本文将详细介绍中值滤波的原理、算法和在图像处理中的应用。 中值滤波的基本...

    图像中值滤波的原理、算法实现及VC++原代码

    图像中值滤波的原理、算法实现及VC++原代码 图像中值滤波是一种非线性的图像处理方法,它通过对邻域内像素按灰度排序的结果决定中心像素的灰度。这种思想与前面两节的均值处理思想有较大不同。 中值滤波的原理是...

    中值滤波程序 用C语言实现

    本文将深入探讨用C语言实现中值滤波的原理与步骤,并结合提供的压缩包文件来理解其实现过程。 中值滤波的核心思想是用像素邻域内的灰度中值替换该像素值,以此来消除椒盐噪声和斑点噪声。它对局部的突变(噪声)...

    【图像去噪】基于邻域和中值滤波实现图像去噪含Matlab源码.zip

    总的来说,这个资源为学习图像去噪提供了很好的实践平台,不仅可以帮助我们理解邻域和中值滤波的基本原理,还能提升我们的Matlab编程技能。在深入研究源码后,我们可以将这些知识应用到更复杂的图像处理任务中,如...

    快速中值滤波及c语言实现.docx

    "快速中值滤波及C语言实现" 快速中值滤波是一种非线性信号处理技术,对抑制图像噪声非常有效。在二维形式下,中值滤波器是一个奇数个像素的滑动窗口,窗口正中的像素灰度值用窗口内各个像素的中值代替。在本文中,...

    中值滤波实现基线漂移

    中值滤波是一种广泛应用在信号处理领域的非...通过理解中值滤波的基本原理,结合Verilog硬件描述语言实现,我们可以设计出专门针对基线漂移的滤波器。在实际应用中,还需要不断优化参数,以达到最佳的信号处理效果。

    verilog实现中值滤波算法及仿真median.rar

    首先,让我们深入理解中值滤波器的工作原理。中值滤波器的基本思想是,对于输入序列中的每个点,将其周围的窗口内的数据按大小排序,然后将排序后的中间值作为输出。由于椒盐噪声通常表现为孤立的极端值,而中值...

    中值滤波算法VC实现(代码)

    在VC++环境下实现中值滤波器,需要理解算法原理并将其转化为C++代码。 中值滤波的基本思想是,对于图像中的每一个像素点,我们不直接用其自身的灰度值来更新,而是用该像素点周围窗口内的像素值的中值来替代。这个...

    cuda实现的中值滤波介绍

    中值滤波的具体原理是选取一个含有奇数个像素点的掩模窗口,在图像数据中滑动,每次移动窗口时,取出窗口内所有像素点的灰度值,并将这些值进行排序,最终取排序后的中间值作为窗口中心像素的新值。在CUDA实现中,...

    中值滤波Java实现

    中值滤波的基本原理是:对于图像中每个像素点,我们选择一个窗口(通常为奇数大小的邻域),然后对窗口内的所有像素灰度值进行排序,取中间值作为该像素点的新灰度值。这个中间值就是“中值”,因此得名“中值滤波”...

    图像去噪基于中值滤波实现图像去噪含Matlab源码 上传.zip

    这个函数可能包括读取图像、定义滤波窗口大小、应用中值滤波以及显示处理后的图像等步骤。 **适用人群**: 此项目适合本科及硕士级别的学生进行教研学习,因为图像处理是计算机视觉和信号处理课程中的常见主题,而...

    中值滤波matlab实现.zip

    以上就是关于中值滤波MATLAB实现的详细解释,包括了使用内置函数和自定义结构元素的方式,以及线型、十字形(星型)和方形滤波的实现。通过这些方法,你可以对图像进行有效的噪声去除和平滑处理。

    OGNL符号的使用

    1. 访问非根对象(Struts 中值栈为根对象),例如:#parameters.id[0] 相当于 request.getParameter("id")。 2. 用于过滤和投影(projecting)集合,例如:books.{?#this.price。 3. 构造 Map,例如:#{'foo1':'bar1...

    dsp中值滤波 dsp中值滤波

    这种滤波方法基于排序统计理论,通过将图像或信号的每个像素点替换为其邻域内像素值的中值来实现去噪。中值滤波器通常用于抑制椒盐噪声、斑点噪声以及类似噪声,尤其适用于处理具有尖锐边缘的图像。 中值滤波的基本...

    图像中值滤波FPGA实现

    1. **中值滤波原理**:中值滤波是一种非线性的滤波方法,其核心思想是用像素邻域内灰度值的中值来替代该像素点的原始值。这样可以有效保护图像边缘,同时去除噪声。在椒盐噪声环境下,中值滤波器表现优异,因为它对...

    c语言实现中值滤波的程序代码

    本文将深入探讨如何使用C语言实现中值滤波,并解释其工作原理。 中值滤波的基本思想是,对于图像中的每一个像素点,我们不直接用它本身的颜色值,而是取其周围像素点的一个灰度值的中值作为新的颜色值。这种方法...

    中值滤波原理及MATLAB实现.pdf

    中值滤波器通过在像素点周围的小窗口内计算像素值的中值来实现去噪,而不是简单的平均值,这使得它在保持图像边缘细节的同时,能够有效地滤除噪声。 中值滤波的基本原理如下:选取一个以目标像素为中心的窗口,窗口...

    中值滤波Verilog代码

    它的基本思想是将图像中每个像素点的灰度值替换为其邻域内像素点灰度值的中值,以此来平滑图像,保留边缘细节。在Verilog这种硬件描述语言中实现中值滤波器,可以构建出适用于FPGA或ASIC的硬件电路。 首先,我们...

    matlab代码 中值滤波

    让我们详细了解一下中值滤波以及如何在MATLAB中实现它。 中值滤波的基本原理是,对于图像中的每一个像素点,取其周围邻域内像素的中值作为该点的新值。相比于均值滤波,中值滤波对噪声的抑制效果更好,因为它不受到...

Global site tag (gtag.js) - Google Analytics