01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/osp/tags/sakai_2-4-1/warehouse/api-impl/src/java/org/theospi/portfolio/warehouse/impl/ReferenceHolderPropertyAccess.java $
03: * $Id: ReferenceHolderPropertyAccess.java 10835 2006-06-17 03:25:03Z lance@indiana.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.sakaiproject.metaobj.shared.mgt.ReferenceHolder;
31: import org.theospi.portfolio.warehouse.intf.PropertyAccess;
32:
33: /**
34: * Created by IntelliJ IDEA.
35: * User: John Ellis
36: * Date: Nov 30, 2005
37: * Time: 5:48:56 PM
38: * To change this template use File | Settings | File Templates.
39: */
40: public class ReferenceHolderPropertyAccess implements PropertyAccess {
41:
42: private Map gettorMap = new Hashtable();
43: private String propertyName;
44:
45: public Object getPropertyValue(Object source) throws Exception {
46: Method objectMethodGetProperty = getPropertyGettor(source);
47: if (objectMethodGetProperty == null)
48: throw new NullPointerException(source.getClass().getName()
49: + " has no get for property \"" + propertyName
50: + "\"");
51: Object value = objectMethodGetProperty.invoke(source,
52: new Object[] {});
53:
54: ReferenceHolder refHolder = null;
55:
56: try {
57: refHolder = (ReferenceHolder) value;
58: } catch (ClassCastException e) {
59: throw new Exception(
60: "The source could not be cast into an ReferenceHolder for property \""
61: + propertyName + "\"", e);
62: }
63:
64: return refHolder.getBase().getId();
65: }
66:
67: public String getPropertyName() {
68: return propertyName;
69: }
70:
71: public void setPropertyName(String propertyName) {
72: this .propertyName = propertyName;
73: }
74:
75: public Method getPropertyGettor(Object source)
76: throws IntrospectionException {
77: Method propertyGettor = (Method) gettorMap.get(source
78: .getClass());
79: if (propertyGettor == null) {
80: BeanInfo info = Introspector.getBeanInfo(source.getClass());
81:
82: PropertyDescriptor[] descriptors = info
83: .getPropertyDescriptors();
84:
85: for (int i = 0; i < descriptors.length; i++) {
86: if (descriptors[i].getName().equals(getPropertyName())) {
87: propertyGettor = descriptors[i].getReadMethod();
88: gettorMap.put(source.getClass(), propertyGettor);
89: break;
90: }
91: }
92: }
93: return propertyGettor;
94: }
95:
96: }
|