1 package net.sf.msplice.property.impl;
2
3 import net.sf.msplice.property.IPropertyGetter;
4 import net.sf.msplice.property.PropertyGetException;
5
6 import org.apache.commons.beanutils.PropertyUtils;
7 import org.apache.commons.lang.StringUtils;
8
9 public class BeanutilsPropertyGetter implements IPropertyGetter {
10
11 public Object getProperty(String propertyName, Object rootObject)
12 throws PropertyGetException {
13
14 boolean emptyPropertyName = StringUtils.isEmpty(propertyName);
15 boolean nullRoot = (rootObject == null);
16
17 StringBuffer messageBuffer = new StringBuffer();
18 if (nullRoot || emptyPropertyName) {
19 if (nullRoot) {
20 messageBuffer.append("The root object must not be null! ");
21 }
22 if (emptyPropertyName) {
23 messageBuffer.append("The property name must not be empty! ");
24 }
25 throw new PropertyGetException(messageBuffer.toString());
26 }
27
28 try {
29 return PropertyUtils.getNestedProperty(rootObject, propertyName);
30 } catch (Exception e) {
31 throw new PropertyGetException(
32 "Could not get the property with name: " + propertyName
33 + ", on object: " + rootObject, e);
34 }
35 }
36
37 }