01: /**********************************************************************************
02: * $URL:https://source.sakaiproject.org/svn/osp/trunk/warehouse/api-impl/src/java/org/theospi/portfolio/warehouse/impl/BeanPropertyAccess.java $
03: * $Id:BeanPropertyAccess.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the "License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.theospi.portfolio.warehouse.impl;
21:
22: import java.beans.BeanInfo;
23: import java.beans.IntrospectionException;
24: import java.beans.Introspector;
25: import java.beans.PropertyDescriptor;
26: import java.lang.reflect.Method;
27: import java.util.Hashtable;
28: import java.util.Map;
29:
30: import org.theospi.portfolio.warehouse.intf.PropertyAccess;
31:
32: /**
33: * Created by IntelliJ IDEA.
34: * User: John Ellis
35: * Date: Nov 30, 2005
36: * Time: 5:34:24 PM
37: * To change this template use File | Settings | File Templates.
38: */
39: public class BeanPropertyAccess implements PropertyAccess {
40:
41: private Map gettorMap = new Hashtable();
42: private String propertyName;
43:
44: public Object getPropertyValue(Object source) throws Exception {
45: if (source == null)
46: throw new NullPointerException(
47: "The source object is null, getting"
48: + " property \"" + propertyName + "\"");
49: Method objectMethodGetProperty = getPropertyGettor(source);
50: if (objectMethodGetProperty == null)
51: throw new NullPointerException(source.getClass().getName()
52: + " has no get for property \"" + propertyName
53: + "\"");
54: return objectMethodGetProperty.invoke(source, new Object[] {});
55: }
56:
57: public String getPropertyName() {
58: return propertyName;
59: }
60:
61: public void setPropertyName(String propertyName) {
62: this .propertyName = propertyName;
63: }
64:
65: public Method getPropertyGettor(Object source)
66: throws IntrospectionException {
67: Method propertyGettor = (Method) gettorMap.get(source
68: .getClass());
69: if (propertyGettor == null) {
70: BeanInfo info = Introspector.getBeanInfo(source.getClass());
71:
72: PropertyDescriptor[] descriptors = info
73: .getPropertyDescriptors();
74:
75: for (int i = 0; i < descriptors.length; i++) {
76: if (descriptors[i].getName().equals(getPropertyName())) {
77: propertyGettor = descriptors[i].getReadMethod();
78: gettorMap.put(source.getClass(), propertyGettor);
79: break;
80: }
81: }
82: }
83: return propertyGettor;
84: }
85:
86: }
|