001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020
021 package org.apache.geronimo.j2ee.deployment.annotation;
022
023 import java.lang.reflect.Field;
024 import java.lang.reflect.Method;
025
026 import org.apache.geronimo.common.DeploymentException;
027 import org.apache.geronimo.deployment.xmlbeans.XmlBeansUtil;
028 import org.apache.geronimo.xbeans.javaee.FullyQualifiedClassType;
029 import org.apache.geronimo.xbeans.javaee.InjectionTargetType;
030 import org.apache.geronimo.xbeans.javaee.JavaIdentifierType;
031 import org.apache.xmlbeans.XmlException;
032
033 /**
034 * @version $Rev: 522642 $ $Date: 2007-03-26 18:08:52 -0400 (Mon, 26 Mar 2007) $
035 */
036 public class AnnotationHelper {
037 protected static String getInjectionJavaType(Method method, Field field) {
038 if (method != null) {
039 String injectionJavaType = method.getName().substring(3);
040 StringBuilder stringBuilder = new StringBuilder(injectionJavaType);
041 stringBuilder.setCharAt(0, Character.toLowerCase(stringBuilder.charAt(0)));
042 return stringBuilder.toString();
043 } else if (field != null) {
044 return field.getName();
045 } else {
046 throw new IllegalArgumentException("You must supply exactly one of Method, Field");
047 }
048 }
049
050 protected static String getInjectionClass(Method method, Field field) {
051 if (method != null) {
052 return method.getDeclaringClass().getName();
053 } else if (field != null) {
054 return field.getDeclaringClass().getName();
055 } else {
056 throw new IllegalArgumentException("You must supply exactly one of Method, Field");
057 }
058 }
059
060 protected static boolean hasTarget(Method method, Field field, InjectionTargetType[] targets) {
061 String injectionJavaType = getInjectionJavaType(method, field);
062 String injectionClass = getInjectionClass(method, field);
063 for (InjectionTargetType target : targets) {
064 if (injectionClass.equals(target.getInjectionTargetClass().getStringValue().trim())
065 && injectionJavaType.equals(target.getInjectionTargetName().getStringValue().trim())) {
066 return true;
067 }
068 }
069 return false;
070 }
071
072 /**
073 * Configure Injection Target
074 *
075 * @param injectionTarget
076 * @param method
077 * @param field
078 */
079 protected static void configureInjectionTarget(InjectionTargetType injectionTarget, Method method, Field field) {
080
081 String injectionJavaType = getInjectionJavaType(method, field);
082 String injectionClass = getInjectionClass(method, field);
083
084 FullyQualifiedClassType qualifiedClass = injectionTarget.addNewInjectionTargetClass();
085 JavaIdentifierType javaType = injectionTarget.addNewInjectionTargetName();
086 qualifiedClass.setStringValue(injectionClass);
087 javaType.setStringValue(injectionJavaType);
088 }
089
090 /**
091 * Validate deployment descriptor
092 *
093 * @param annotatedApp the wrapped deployment descriptor
094 * @throws org.apache.geronimo.common.DeploymentException
095 * thrown if deployment descriptor cannot be parsed
096 */
097 protected static void validateDD(AnnotatedApp annotatedApp) throws DeploymentException {
098 try {
099 XmlBeansUtil.parse(annotatedApp.toString());
100 } catch (XmlException e) {
101 throw new DeploymentException("Result of processing web service refs invalid.", e);
102 }
103 }
104 }