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: */package org.apache.geronimo.jaxws.annotations;
017:
018: import java.lang.annotation.Annotation;
019: import java.lang.reflect.Field;
020: import java.lang.reflect.InvocationTargetException;
021: import java.lang.reflect.Method;
022:
023: // TODO: Check for static methods and fields. They are only allowed for client apps.
024: public abstract class InjectingAnnotationHandler implements
025: AnnotationHandler {
026:
027: abstract public Object getAnnotationValue(Annotation annotation,
028: String name, Class<?> type) throws InjectionException;
029:
030: public void processClassAnnotation(Object instance, Class clazz,
031: Annotation annotation) {
032: // injection is not done for annotations at class level
033: }
034:
035: public String getJNDIName(Object instance, String name, Field field) {
036: if (name != null && name.length() > 0) {
037: return name;
038: } else {
039: return instance.getClass().getName() + "/"
040: + field.getName();
041: }
042: }
043:
044: public String getJNDIName(Object instance, String name,
045: Method method) {
046: if (name != null && name.length() > 0) {
047: return name;
048: } else {
049: String propName = method.getName();
050: propName = propName.substring(3);
051: propName = Character.toLowerCase(propName.charAt(0))
052: + propName.substring(1);
053: return instance.getClass().getName() + "/" + propName;
054: }
055: }
056:
057: public Class<?> getType(Class<?> type, Field field) {
058: return (type == null || Object.class == type) ? field.getType()
059: : type;
060: }
061:
062: public Class<?> getType(Class<?> type, Method method) {
063: return (type == null || Object.class == type) ? method
064: .getParameterTypes()[0] : type;
065: }
066:
067: protected void injectField(Object instance, Field field,
068: Annotation annotation, String name, Class<?> type)
069: throws InjectionException {
070:
071: String jndiName = getJNDIName(instance, name, field);
072:
073: Object lookedupResource = getAnnotationValue(annotation,
074: jndiName, getType(type, field));
075:
076: boolean accessibility = field.isAccessible();
077: try {
078: field.setAccessible(true);
079: field.set(instance, lookedupResource);
080: } catch (IllegalArgumentException e) {
081: throw new InjectionException("Field injection failed", e);
082: } catch (IllegalAccessException e) {
083: throw new InjectionException("Field injection failed", e);
084: } finally {
085: field.setAccessible(accessibility);
086: }
087: }
088:
089: protected void injectMethod(Object instance, Method method,
090: Annotation annotation, String name, Class<?> type)
091: throws InjectionException {
092:
093: if (!method.getName().startsWith("set")
094: || method.getParameterTypes().length != 1
095: || !method.getReturnType().equals(Void.class)) {
096: throw new IllegalArgumentException(
097: "Invalid method resource injection annotation");
098: }
099:
100: String jndiName = getJNDIName(instance, name, method);
101:
102: Object lookedupResource = getAnnotationValue(annotation,
103: jndiName, getType(type, method));
104:
105: boolean accessibility = method.isAccessible();
106: try {
107: method.setAccessible(true);
108: method.invoke(instance, lookedupResource);
109: } catch (IllegalArgumentException e) {
110: throw new InjectionException("Method injection failed", e);
111: } catch (IllegalAccessException e) {
112: throw new InjectionException("Method injection failed", e);
113: } catch (InvocationTargetException e) {
114: throw new InjectionException("Method injection failed", e);
115: } finally {
116: method.setAccessible(accessibility);
117: }
118: }
119:
120: }
|