001: //--------------------------------------------------------------------------
002: // Copyright (c) 1998-2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package ognl;
032:
033: import java.beans.IntrospectionException;
034: import java.util.Map;
035:
036: /**
037: * Implementation of PropertyAccessor that uses reflection on the target object's class to
038: * find a field or a pair of set/get methods with the given property name.
039: * @author Luke Blanshard (blanshlu@netscape.net)
040: * @author Drew Davidson (drew@ognl.org)
041: */
042: public class ObjectPropertyAccessor implements PropertyAccessor {
043: /**
044: Returns OgnlRuntime.NotFound if the property does not exist.
045: */
046: public Object getPossibleProperty(Map context, Object target,
047: String name) throws OgnlException {
048: Object result;
049: OgnlContext ognlContext = (OgnlContext) context;
050:
051: try {
052: if ((result = OgnlRuntime.getMethodValue(ognlContext,
053: target, name, true)) == OgnlRuntime.NotFound) {
054: result = OgnlRuntime.getFieldValue(ognlContext, target,
055: name, true);
056: }
057: } catch (IntrospectionException ex) {
058: throw new OgnlException(name, ex);
059: } catch (OgnlException ex) {
060: throw ex;
061: } catch (Exception ex) {
062: throw new OgnlException(name, ex);
063: }
064: return result;
065: }
066:
067: /**
068: Returns OgnlRuntime.NotFound if the property does not exist.
069: */
070: public Object setPossibleProperty(Map context, Object target,
071: String name, Object value) throws OgnlException {
072: Object result = null;
073: OgnlContext ognlContext = (OgnlContext) context;
074:
075: try {
076: if (!OgnlRuntime.setMethodValue(ognlContext, target, name,
077: value, true)) {
078: result = OgnlRuntime.setFieldValue(ognlContext, target,
079: name, value) ? null : OgnlRuntime.NotFound;
080: }
081: } catch (IntrospectionException ex) {
082: throw new OgnlException(name, ex);
083: } catch (OgnlException ex) {
084: throw ex;
085: } catch (Exception ex) {
086: throw new OgnlException(name, ex);
087: }
088: return result;
089: }
090:
091: public boolean hasGetProperty(OgnlContext context, Object target,
092: Object oname) throws OgnlException {
093: try {
094: return OgnlRuntime.hasGetProperty(context, target, oname);
095: } catch (IntrospectionException ex) {
096: throw new OgnlException("checking if " + target
097: + " has gettable property " + oname, ex);
098: }
099: }
100:
101: public boolean hasGetProperty(Map context, Object target,
102: Object oname) throws OgnlException {
103: return hasGetProperty((OgnlContext) context, target, oname);
104: }
105:
106: public boolean hasSetProperty(OgnlContext context, Object target,
107: Object oname) throws OgnlException {
108: try {
109: return OgnlRuntime.hasSetProperty(context, target, oname);
110: } catch (IntrospectionException ex) {
111: throw new OgnlException("checking if " + target
112: + " has settable property " + oname, ex);
113: }
114: }
115:
116: public boolean hasSetProperty(Map context, Object target,
117: Object oname) throws OgnlException {
118: return hasSetProperty((OgnlContext) context, target, oname);
119: }
120:
121: public Object getProperty(Map context, Object target, Object oname)
122: throws OgnlException {
123: Object result = null;
124: String name = oname.toString();
125:
126: if ((result = getPossibleProperty(context, target, name)) == OgnlRuntime.NotFound) {
127: throw new NoSuchPropertyException(target, name);
128: }
129: return result;
130: }
131:
132: public void setProperty(Map context, Object target, Object oname,
133: Object value) throws OgnlException {
134: String name = oname.toString();
135:
136: if (setPossibleProperty(context, target, name, value) == OgnlRuntime.NotFound) {
137: throw new NoSuchPropertyException(target, name);
138: }
139: }
140: }
|