001: /*
002: * Copyright 2007 Dan Shellman
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.iscreen.mvel;
017:
018: import org.iscreen.ConfigurationException;
019:
020: import org.mvel.PropertyAccessException;
021: import org.mvel.PropertyAccessor;
022:
023: /**
024: * Instances of this class are used to set MVEL properties on objects with
025: * a particular value. The "property" is actually an MVEL expression.
026: * The value can be set and used later, or it can be passed in to a map()
027: * method. The object instance that contains the property (and will have
028: * its "property" set) is passed in dynamically as part of the map() method.
029: * This class is used to set constraints, failure objects and services.
030: *
031: * @author Shellman, Dan
032: */
033: public class MvelPropertyMapping {
034: protected String property;
035: protected Object value;
036:
037: /**
038: * Construct with given property. No value is defined (value of the property).
039: *
040: * @param theProperty The property that will be set.
041: */
042: public MvelPropertyMapping(String theProperty) {
043: property = theProperty;
044: } //end MvelPropertyMapping()
045:
046: /**
047: * Construct with both a property and the property's value.
048: *
049: * @param theProperty The property that will be set.
050: * @param theValue The value the property will be set to.
051: */
052: public MvelPropertyMapping(String theProperty, Object theValue) {
053: property = theProperty;
054: value = theValue;
055: } //end MvelPropertyMapping()
056:
057: /**
058: * Sets the value the property will be set to.
059: *
060: * @param theValue The value the property will be set to.
061: */
062: public void setValue(Object theValue) {
063: value = theValue;
064: } //end setValue()
065:
066: /**
067: * Sets a property on the object with the given value (any value set
068: * using the setValue() method is ignored in this case).
069: *
070: * @param obj The object that will have its property set.
071: * @param theValue The value to set the property to.
072: */
073: public void map(Object obj, Object theValue) {
074: try {
075: PropertyAccessor.set(obj, property, theValue);
076: } catch (PropertyAccessException e) {
077: throw new ConfigurationException(
078: "Error mapping value to property on object using MVEL. MVEL property expression is "
079: + property
080: + ". Value being set is "
081: + theValue
082: + ". Class of object being acted on is "
083: + obj.getClass().getName(), e);
084: }
085: } //end map()
086:
087: /**
088: * Sets a property on the object with the previously set value (from
089: * calling the setValue() method).
090: *
091: * @param obj The object that will have its property set.
092: */
093: public void map(Object obj) {
094: map(obj, value);
095: } //end map()
096:
097: public int hashCode() {
098: return property.hashCode();
099: } //end hashCode()
100:
101: /**
102: * Implemented so that instances can be stored in sets/maps. Two instances
103: * are considered equal if they have the same property.
104: *
105: * @param obj The object to compare to
106: *
107: * @return Returns true if both have the same property.
108: */
109: public boolean equals(Object obj) {
110: MvelPropertyMapping mapping;
111:
112: mapping = (MvelPropertyMapping) obj;
113: if (property != null && property.equals(mapping.property)) {
114: return true;
115: }
116:
117: return false;
118: } //end equals()
119: } //end MvelPropertyMapping
|