01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
18:
19: import java.beans.PropertyDescriptor;
20: import java.util.List;
21: import java.util.Map;
22:
23: import org.apache.tapestry.ioc.internal.util.InternalUtils;
24: import org.apache.tapestry.ioc.services.ClassPropertyAdapter;
25: import org.apache.tapestry.ioc.services.PropertyAdapter;
26:
27: public class ClassPropertyAdapterImpl implements ClassPropertyAdapter {
28: private final Map<String, PropertyAdapter> _adapters = newCaseInsensitiveMap();
29:
30: private final Class _beanType;
31:
32: public ClassPropertyAdapterImpl(Class beanType,
33: List<PropertyDescriptor> descriptors) {
34: _beanType = beanType;
35:
36: for (PropertyDescriptor pd : descriptors) {
37: // Indexed properties will have a null propertyType (and a non-null
38: // indexedPropertyType). We ignore indexed properties.
39:
40: if (pd.getPropertyType() == null)
41: continue;
42:
43: PropertyAdapter pa = new PropertyAdapterImpl(pd);
44:
45: _adapters.put(pa.getName(), pa);
46: }
47: }
48:
49: public Class getBeanType() {
50: return _beanType;
51: }
52:
53: @Override
54: public String toString() {
55: String names = InternalUtils.joinSorted(_adapters.keySet());
56:
57: return String.format("<ClassPropertyAdaptor %s : %s>",
58: _beanType.getName(), names);
59: }
60:
61: public List<String> getPropertyNames() {
62: return InternalUtils.sortedKeys(_adapters);
63: }
64:
65: public PropertyAdapter getPropertyAdapter(String name) {
66: return _adapters.get(name);
67: }
68:
69: public Object get(Object instance, String propertyName) {
70: return adaptorFor(propertyName).get(instance);
71: }
72:
73: public void set(Object instance, String propertyName, Object value) {
74: adaptorFor(propertyName).set(instance, value);
75: }
76:
77: private PropertyAdapter adaptorFor(String name) {
78: PropertyAdapter pa = _adapters.get(name);
79:
80: if (pa == null)
81: throw new IllegalArgumentException(ServiceMessages
82: .noSuchProperty(_beanType, name));
83:
84: return pa;
85: }
86:
87: }
|