001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.util;
016:
017: import java.beans.PropertyDescriptor;
018:
019: import org.apache.commons.beanutils.NestedNullException;
020: import org.apache.commons.beanutils.PropertyUtils;
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.strecks.exceptions.ApplicationRuntimeException;
024:
025: /**
026: * Utility class with methods mostly related to getting object and property values using reflection
027: * @author Phil Zoio
028: */
029: public class PropertyValueGetter {
030:
031: private static Log log = LogFactory
032: .getLog(PropertyValueGetter.class);
033:
034: public static Object getPropertyValue(Object containingBean,
035: String propertyName) {
036:
037: Assert.notNull(containingBean);
038: Assert.notNull(propertyName);
039:
040: Object propertyValue;
041: try {
042: // properties with bind annotation must be Strings
043: propertyValue = PropertyUtils.getProperty(containingBean,
044: propertyName);
045: } catch (IllegalAccessException e) {
046: throw new ApplicationRuntimeException(
047: "Illegal getter method access attempted from "
048: + containingBean.getClass()
049: + " using property expression "
050: + propertyName, e);
051: } catch (NoSuchMethodException e) {
052: throw new ApplicationRuntimeException(
053: "Required getter methods not found from "
054: + containingBean.getClass()
055: + " using property expression "
056: + propertyName, e);
057: } catch (Exception e) {
058: throw new ApplicationRuntimeException(
059: "Unable to read property for bean "
060: + containingBean.getClass().getName()
061: + ", property " + propertyName, e);
062: }
063: return propertyValue;
064:
065: }
066:
067: public static PropertyDescriptor getPropertyDescriptorSilently(
068: Object containingBean, String propertyName) {
069: try {
070: // properties with bind annotation must be Strings
071: return getPropertyDescriptor(containingBean, propertyName);
072: } catch (Exception e) {
073: log.error(e);
074: return null;
075: }
076: }
077:
078: public static Class<?> getPropertyTypeSilently(
079: Object containingBean, String propertyName) {
080: try {
081: // properties with bind annotation must be Strings
082: PropertyDescriptor propertyDescriptor = getPropertyDescriptor(
083: containingBean, propertyName);
084:
085: if (propertyDescriptor != null) {
086: return propertyDescriptor.getPropertyType();
087: }
088: } catch (Exception e) {
089: log.error(e);
090: }
091: return null;
092: }
093:
094: public static PropertyDescriptor getPropertyDescriptor(
095: Object containingBean, String propertyName) {
096:
097: Assert.notNull(containingBean);
098: Assert.notNull(propertyName);
099:
100: PropertyDescriptor descriptor;
101: try {
102: // properties with bind annotation must be Strings
103: descriptor = PropertyUtils.getPropertyDescriptor(
104: containingBean, propertyName);
105: } catch (Exception e) {
106: throw new ApplicationRuntimeException(
107: "Unable to read property descriptor for bean "
108: + containingBean.getClass().getName()
109: + ", property " + propertyName, e);
110: }
111: return descriptor;
112:
113: }
114:
115: /**
116: * Uses <code>beanLocatingExpression</code> to find a property, potentially nested within
117: * other properties, from <code>target</code>
118: */
119: public static Object getTargetBean(Object target,
120: String beanLocatingExpression) {
121:
122: Object targetBean;
123:
124: if (beanLocatingExpression != null) {
125: try {
126: targetBean = PropertyUtils.getNestedProperty(target,
127: beanLocatingExpression);
128: } catch (NestedNullException e) {
129: // property does not exist or is null
130: targetBean = null;
131: } catch (IllegalAccessException e) {
132: throw new ApplicationRuntimeException(
133: "Illegal getter method access attempted from "
134: + target.getClass()
135: + " using expression "
136: + beanLocatingExpression, e);
137: } catch (NoSuchMethodException e) {
138: throw new ApplicationRuntimeException(
139: "Required getter methods not found from "
140: + target.getClass()
141: + " using expression "
142: + beanLocatingExpression, e);
143: } catch (Exception e) {
144: throw new ApplicationRuntimeException(
145: "Unable to obtain bean from "
146: + target.getClass()
147: + " using expression "
148: + beanLocatingExpression, e);
149: }
150: } else {
151: targetBean = target;
152: }
153:
154: return targetBean;
155:
156: }
157:
158: }
|