001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.controls.api.properties;
020:
021: import java.io.IOException;
022: import java.lang.annotation.Annotation;
023: import java.lang.annotation.Inherited;
024: import java.lang.reflect.AnnotatedElement;
025: import java.lang.reflect.Field;
026: import java.lang.reflect.Method;
027:
028: import org.apache.beehive.controls.api.bean.ControlBean;
029: import org.apache.beehive.controls.api.bean.ControlExtension;
030: import org.apache.beehive.controls.api.bean.ControlInterface;
031:
032: /**
033: * The AnnotatedElementMap represents a read-only PropertyMap where property values are
034: * derived from Java 5.0 (JSR-175) annotations.
035: */
036: public class AnnotatedElementMap extends BaseMap implements
037: PropertyMap, java.io.Serializable {
038: /**
039: * Creates a new PropertyMap that is initialized based upon the type and annotations
040: * associated with an AnnotatedElement.
041: */
042: public AnnotatedElementMap(AnnotatedElement annotElem) {
043: if (annotElem instanceof Class)
044: setMapClass((Class) annotElem);
045: else if (annotElem instanceof Field)
046: setMapClass(((Field) annotElem).getType());
047: else if (annotElem instanceof Method) {
048: Class mapClass = getMethodMapClass((Method) annotElem);
049: setMapClass(mapClass);
050: } else
051: throw new IllegalArgumentException(
052: "Unsupported element type: " + annotElem.getClass());
053:
054: _annotElem = annotElem;
055: }
056:
057: // For methods, make sure we find a declaring class that is a valid
058: // map class. For extended callback methods, we need to walk up a bit
059: // further in the hierarchy.
060:
061: Class getMethodMapClass(Method method) {
062:
063: Class origMapClass = method.getDeclaringClass();
064: Class mapClass = origMapClass;
065: while (mapClass != null && !isValidMapClass(mapClass)) {
066: mapClass = mapClass.getDeclaringClass();
067: }
068: if (mapClass == null) {
069: mapClass = origMapClass;
070: }
071: return mapClass;
072: }
073:
074: boolean isValidMapClass(Class mapClass) {
075: if (ControlBean.class.isAssignableFrom(mapClass)) {
076: return true;
077: } else {
078: if (mapClass.isAnnotation()
079: || mapClass
080: .isAnnotationPresent(ControlInterface.class)
081: || mapClass
082: .isAnnotationPresent(ControlExtension.class)) {
083: return true;
084: }
085: }
086: return false;
087: }
088:
089: /**
090: * Sets the property specifed by 'key' within this map.
091: */
092: public void setProperty(PropertyKey key, Object value) {
093: throw new IllegalStateException(
094: "AnnotatedElementMap is a read-only PropertyMap");
095: }
096:
097: /**
098: * Returns the property value specified by 'key' within this map.
099: */
100: public Object getProperty(PropertyKey key) {
101: if (!isValidKey(key))
102: throw new IllegalArgumentException("Key " + key
103: + " is not valid for " + _mapClass);
104:
105: //
106: // Look for the property value on the associated annotated element
107: //
108: Class propertySet = key.getPropertySet();
109: Annotation annot = _annotElem.getAnnotation(propertySet);
110: if (annot != null)
111: return key.extractValue(annot);
112:
113: //
114: // If the property supports inheritance and the annotated element is an interface,
115: // then we'll search up the ControlInheritance/Extension hierachy to see if it is
116: // provided higher up the chain.
117: //
118: if (propertySet.isAnnotationPresent(Inherited.class)
119: && _annotElem instanceof Class) {
120: Class controlIntf = (Class) _annotElem;
121: do {
122: Class[] super Intfs = controlIntf.getInterfaces();
123: controlIntf = null;
124: for (int i = 0; i < super Intfs.length; i++) {
125: if (super Intfs[i]
126: .isAnnotationPresent(ControlInterface.class)
127: || super Intfs[i]
128: .isAnnotationPresent(ControlExtension.class)) {
129: controlIntf = super Intfs[i];
130: annot = controlIntf.getAnnotation(propertySet);
131: if (annot != null)
132: return key.extractValue(annot);
133: }
134: }
135:
136: } while (controlIntf != null);
137: }
138:
139: //
140: // Call up to superclass for delegation / default value
141: //
142: return super .getProperty(key);
143: }
144:
145: /**
146: * Returns true if the PropertyMap contains one or more values for the specified
147: * PropertySet, false otherwise
148: */
149: public boolean containsPropertySet(
150: Class<? extends Annotation> propertySet) {
151: if (_annotElem.isAnnotationPresent(propertySet))
152: return true;
153:
154: //
155: // Call up to superclass for delegation
156: //
157: return super .containsPropertySet(propertySet);
158: }
159:
160: /**
161: * Returns the AnnotatedElement used for PropertyMap values.
162: */
163: public AnnotatedElement getAnnotatedElement() {
164: return _annotElem;
165: }
166:
167: /**
168: * Returns a String version of method argument lists based upon the method argument types
169: */
170: private String getMethodArgs(Method m) {
171: StringBuffer sb = new StringBuffer();
172: Class[] argTypes = m.getParameterTypes();
173: for (int i = 0; i < argTypes.length; i++) {
174: if (i != 0)
175: sb.append(",");
176: sb.append(argTypes[i].toString());
177: }
178: return sb.toString();
179: }
180:
181: /**
182: * Overrides the standard Serialization writeObject method to compute and store the element
183: * information in a serializable form.
184: */
185: private void writeObject(java.io.ObjectOutputStream out)
186: throws IOException {
187: //
188: // When serializing, compute sufficient information about the annotated element to
189: // allow it to be reassociated later in readObject
190: //
191: if (_annotElem instanceof Class) {
192: _elemClass = (Class) _annotElem;
193: _elemDesc = null; // non required
194: } else if (_annotElem instanceof Field) {
195: Field f = (Field) _annotElem;
196: _elemClass = f.getDeclaringClass();
197: _elemDesc = f.getName();
198: } else if (_annotElem instanceof Method) {
199: Method m = (Method) _annotElem;
200: _elemClass = m.getDeclaringClass();
201: _elemDesc = m.getName() + "(" + getMethodArgs(m) + ")";
202: }
203:
204: out.defaultWriteObject();
205: }
206:
207: /**
208: * Overrides the standard Serialization readObject implementation to reassociated with the
209: * target AnnotatedElement after deserialization.
210: */
211: private void readObject(java.io.ObjectInputStream in)
212: throws IOException, ClassNotFoundException {
213: in.defaultReadObject();
214:
215: if (_elemDesc == null) // element is a Class
216: _annotElem = _elemClass;
217: else {
218: int argsIndex = _elemDesc.indexOf('(');
219: if (argsIndex < 0) // element is a Field
220: {
221: try {
222: _annotElem = _elemClass.getDeclaredField(_elemDesc);
223: } catch (NoSuchFieldException nsfe) {
224: throw new IOException("Unable to locate field "
225: + nsfe);
226: }
227: } else // element is a method
228: {
229: String methodName = _elemDesc.substring(0, argsIndex);
230: if (_elemDesc.charAt(argsIndex + 1) == ')') {
231: // At least handle the null args case quickly
232: try {
233: _annotElem = _elemClass.getDeclaredMethod(
234: methodName, new Class[] {});
235: } catch (NoSuchMethodException nsme) {
236: throw new IOException(
237: "Unable to locate method " + _elemDesc);
238: }
239: } else {
240: // Linear search for the rest :(
241: String methodArgs = _elemDesc.substring(
242: argsIndex + 1, _elemDesc.length() - 1);
243: Method[] methods = _elemClass.getDeclaredMethods();
244: for (int i = 0; i < methods.length; i++) {
245: if (methods[i].getName().equals(methodName)
246: && getMethodArgs(methods[i]).equals(
247: methodArgs)) {
248: _annotElem = methods[i];
249: break;
250: }
251: }
252:
253: if (_annotElem == null) {
254: throw new IOException(
255: "Unable to locate method " + _elemDesc);
256: }
257: }
258: }
259: }
260: }
261:
262: // The AnnotatedElement upon which this PropertyMap is based. This is marked transient,
263: // because many Reflection types are not Serializable.
264: transient private AnnotatedElement _annotElem;
265:
266: private Class _elemClass; // Class associated with the annotated element
267: private String _elemDesc; // Description of the element
268: }
|