com.opensymphony.xwork.util
Class OgnlUtil

java.lang.Object
  extended by com.opensymphony.xwork.util.OgnlUtil

public class OgnlUtil
extends Object

Utility class that provides common access to the Ognl APIs for setting and getting properties from objects (usually Actions). Ognl by default uses expression parsing for expression evaluation. However starting from Ognl 2.7.1, Ognl is capable of doing expression compilation using Javassist for expression evaluation. WebWork 2.2.7 / XWork1.2.4 and above support this functionality, however it is turn off by default to preserved backwards compatibilities. To turn it on, we need to use the following Doctype :-

 
     <!DOCTYPE xwork PUBLIC
                            "-//OpenSymphony Group//XWork 1.1.2//EN"
                            "http://www.opensymphony.com/xwork/xwork-1.1.2.dtd">
 
 
And also declare a parameter "useOgnlEnhancement" with value "true" in xwork.xml as follows.
 
 <xwork>
    <parameters>
      <!-- enable OGNL expression compilation feature -->
      <parameter name="useOgnlEnhancement" value="true" />
      ...
    </parameters>
    ...
  </xwork>
 
 
NOTE: Do take note that parameters declared in xwork.xml or its included files will override where the latter takes precedence. For example if we have a parameter declared in xwork.xml and one in xwork-include1.xml which is included through xwork.xml, the parameter declared in xwork-include1.xml will override the one declared in xwork.xml. In other words the parameter declared in xwork-include1.xml takes precedence.

Version:
$Date: 2007-12-01 19:37:53 -0600 (Sat, 01 Dec 2007) $ $Id: OgnlUtil.java 1700 2007-12-02 01:37:53Z tm_jee $
Author:
Jason Carreira, tmjee

Constructor Summary
OgnlUtil()
           
 
Method Summary
static Object compile(String expression)
          Parse an ognl expression specified as expression, cache the parsed result for better response next round.
static void copy(Object from, Object to, Map context)
          Copies the properties in the object "from" and sets them in the object "to" using specified type converter, or XWorkConverter if none is specified.
static void copy(Object from, Object to, Map context, Collection exclusions, Collection inclusions)
          Copies the properties in the object "from" and sets them in the object "to" using specified type converter, or XWorkConverter if none is specified.
static BeanInfo getBeanInfo(Object from)
          Get's the java bean info for the given source.
static Map getBeanMap(Object source)
          Creates a Map with read properties for the given source object.
static PropertyDescriptor[] getPropertyDescriptors(Object source)
          Get's the java beans property descriptors for the given source.
static Object getRealTarget(String property, Map context, Object root)
          Looks for the real target with the specified property given a root Object which may be a CompoundRoot.
static Object getValue(String name, Map context, Object root)
          Wrapper to use OGNL to get the property on target root based on the OGNL expression expression encapsulating if the expression were to be parsed or compiled depending on isUseOgnlEnhancement().
static Object getValue(String name, Map context, Object root, Class resultType)
           
static void setProperties(Map properties, Object o)
          Sets the properties on the object using the default context, defaulting to not throwing exceptions for problems setting the properties.
static void setProperties(Map properties, Object o, boolean throwPropertyExceptions)
          Sets the properties on the object using the default context.
static void setProperties(Map props, Object o, Map context)
          Sets the object's properties using the default type converter, defaulting to not throw exceptions for problems setting the properties.
static void setProperties(Map props, Object o, Map context, boolean throwPropertyExceptions)
          Sets the object's properties using the default type converter.
static void setProperty(String name, Object value, Object o, Map context)
          Sets the named property to the supplied value on the Object, defaults to not throwing property exceptions.
static void setProperty(String name, Object value, Object o, Map context, boolean throwPropertyExceptions)
          Sets the named property to the supplied value on the Object.
static void setValue(String name, Map context, Object root, Object value)
          Wrapper to use OGNL to set the property on target root based on OGNL expression name with the value as value, encapsulating if the expression were to be parsed or compiled depending on isUseOgnlEnhancement().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OgnlUtil

public OgnlUtil()
Method Detail

setProperties

public static void setProperties(Map props,
                                 Object o,
                                 Map context)
Sets the object's properties using the default type converter, defaulting to not throw exceptions for problems setting the properties.

Parameters:
props - the properties being set
o - the object
context - the action context

setProperties

public static void setProperties(Map props,
                                 Object o,
                                 Map context,
                                 boolean throwPropertyExceptions)
Sets the object's properties using the default type converter.

Parameters:
props - the properties being set
o - the object
context - the action context
throwPropertyExceptions - boolean which tells whether it should throw exceptions for problems setting the properties

setProperties

public static void setProperties(Map properties,
                                 Object o)
Sets the properties on the object using the default context, defaulting to not throwing exceptions for problems setting the properties.

Parameters:
properties -
o -

setProperties

public static void setProperties(Map properties,
                                 Object o,
                                 boolean throwPropertyExceptions)
Sets the properties on the object using the default context.

Parameters:
properties - the property map to set on the object
o - the object to set the properties into
throwPropertyExceptions - boolean which tells whether it should throw exceptions for problems setting the properties

setProperty

public static void setProperty(String name,
                               Object value,
                               Object o,
                               Map context)
Sets the named property to the supplied value on the Object, defaults to not throwing property exceptions.

Parameters:
name - the name of the property to be set
value - the value to set into the named property
o - the object upon which to set the property
context - the context which may include the TypeConverter

setProperty

public static void setProperty(String name,
                               Object value,
                               Object o,
                               Map context,
                               boolean throwPropertyExceptions)
Sets the named property to the supplied value on the Object.

Parameters:
name - the name of the property to be set
value - the value to set into the named property
o - the object upon which to set the property
context - the context which may include the TypeConverter
throwPropertyExceptions - boolean which tells whether it should throw exceptions for problems setting the property

getRealTarget

public static Object getRealTarget(String property,
                                   Map context,
                                   Object root)
                            throws ognl.OgnlException
Looks for the real target with the specified property given a root Object which may be a CompoundRoot.

Returns:
the real target or null if no object can be found with the specified property
Throws:
ognl.OgnlException

setValue

public static void setValue(String name,
                            Map context,
                            Object root,
                            Object value)
                     throws ognl.OgnlException
Wrapper to use OGNL to set the property on target root based on OGNL expression name with the value as value, encapsulating if the expression were to be parsed or compiled depending on isUseOgnlEnhancement().

Parameters:
name -
context -
root -
value -
Throws:
ognl.OgnlException

getValue

public static Object getValue(String name,
                              Map context,
                              Object root)
                       throws ognl.OgnlException
Wrapper to use OGNL to get the property on target root based on the OGNL expression expression encapsulating if the expression were to be parsed or compiled depending on isUseOgnlEnhancement().

Parameters:
name -
context -
root -
Returns:
Throws:
ognl.OgnlException

getValue

public static Object getValue(String name,
                              Map context,
                              Object root,
                              Class resultType)
                       throws ognl.OgnlException
Throws:
ognl.OgnlException

compile

public static Object compile(String expression)
                      throws ognl.OgnlException
Parse an ognl expression specified as expression, cache the parsed result for better response next round.

Parameters:
expression -
Returns:
Object
Throws:
ognl.OgnlException

copy

public static void copy(Object from,
                        Object to,
                        Map context,
                        Collection exclusions,
                        Collection inclusions)
Copies the properties in the object "from" and sets them in the object "to" using specified type converter, or XWorkConverter if none is specified.

Parameters:
from - the source object
to - the target object
context - the action context we're running under
exclusions - collection of method names to excluded from copying ( can be null)
inclusions - collection of method names to included copying (can be null) note if exclusions AND inclusions are supplied and not null nothing will get copied.

copy

public static void copy(Object from,
                        Object to,
                        Map context)
Copies the properties in the object "from" and sets them in the object "to" using specified type converter, or XWorkConverter if none is specified.

Parameters:
from - the source object
to - the target object
context - the action context we're running under

getPropertyDescriptors

public static PropertyDescriptor[] getPropertyDescriptors(Object source)
                                                   throws IntrospectionException
Get's the java beans property descriptors for the given source.

Parameters:
source - the source object.
Returns:
property descriptors.
Throws:
IntrospectionException - is thrown if an exception occurs during introspection.

getBeanMap

public static Map getBeanMap(Object source)
                      throws IntrospectionException,
                             ognl.OgnlException
Creates a Map with read properties for the given source object.

If the source object does not have a read property (i.e. write-only) then the property is added to the map with the value here is no read method for property-name.

Parameters:
source - the source object.
Returns:
a Map with (key = read property name, value = value of read property).
Throws:
IntrospectionException - is thrown if an exception occurs during introspection.
ognl.OgnlException - is thrown by OGNL if the property value could not be retrieved

getBeanInfo

public static BeanInfo getBeanInfo(Object from)
                            throws IntrospectionException
Get's the java bean info for the given source.

Parameters:
from - the source object.
Returns:
java bean info.
Throws:
IntrospectionException - is thrown if an exception occurs during introspection.

WebWork Project Page