001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ResolverHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.helper;
025:
026: import static org.ow2.easybeans.deployment.annotations.helper.bean.InheritanceInterfacesHelper.JAVA_LANG_OBJECT;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.ow2.easybeans.deployment.annotations.JMethod;
032: import org.ow2.easybeans.deployment.annotations.exceptions.ResolverException;
033: import org.ow2.easybeans.deployment.annotations.helper.bean.BusinessMethodResolver;
034: import org.ow2.easybeans.deployment.annotations.helper.bean.EJB21Finder;
035: import org.ow2.easybeans.deployment.annotations.helper.bean.InheritanceInterfacesHelper;
036: import org.ow2.easybeans.deployment.annotations.helper.bean.InheritanceMethodResolver;
037: import org.ow2.easybeans.deployment.annotations.helper.bean.InterceptorsClassResolver;
038: import org.ow2.easybeans.deployment.annotations.helper.bean.InterfaceAnnotatedHelper;
039: import org.ow2.easybeans.deployment.annotations.helper.bean.SecurityResolver;
040: import org.ow2.easybeans.deployment.annotations.helper.bean.SessionBeanHelper;
041: import org.ow2.easybeans.deployment.annotations.helper.bean.TimedObjectInterface;
042: import org.ow2.easybeans.deployment.annotations.helper.bean.TransactionResolver;
043: import org.ow2.easybeans.deployment.annotations.helper.bean.checks.TimerBeanValidator;
044: import org.ow2.easybeans.deployment.annotations.helper.bean.mdb.MDBListenerBusinessMethodResolver;
045: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
046: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
047: import org.ow2.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
048:
049: /**
050: * This class handle some steps that need to be done after the meta-data
051: * generation.
052: * @author Florent Benoit
053: */
054: public final class ResolverHelper {
055:
056: /**
057: * Helper class, no public constructor.
058: */
059: private ResolverHelper() {
060:
061: }
062:
063: /**
064: * The helper will analyze datas of a given EjbJarAnnotationMetadata object.
065: * @param ejbJarAnnotationMetadata object to analyze
066: * @throws ResolverException if one of resolver fails
067: */
068: public static void resolve(
069: final EjbJarAnnotationMetadata ejbJarAnnotationMetadata)
070: throws ResolverException {
071:
072: // Found each bean class
073: for (ClassAnnotationMetadata classMetaData : ejbJarAnnotationMetadata
074: .getClassAnnotationMetadataCollection()) {
075:
076: if (classMetaData.isBean()) {
077:
078: // Inheritance on interfaces
079: InheritanceInterfacesHelper.resolve(classMetaData);
080: InterfaceAnnotatedHelper.resolve(classMetaData);
081: InheritanceMethodResolver.resolve(classMetaData);
082:
083: // Analyze EJB 2.1x home/localhome for finding interface used
084: EJB21Finder.resolve(classMetaData);
085:
086: // Find business method
087: if (classMetaData.isSession()) {
088: BusinessMethodResolver.resolve(classMetaData);
089: } else if (classMetaData.isMdb()) {
090: MDBListenerBusinessMethodResolver
091: .resolve(classMetaData);
092: }
093:
094: // Security
095: SecurityResolver.resolve(classMetaData);
096:
097: // Transaction
098: TransactionResolver.resolve(classMetaData);
099:
100: // Interceptors
101: InterceptorsClassResolver.resolve(classMetaData);
102:
103: // Check the timer methods (only one by bean) and if the signature is valid
104: TimedObjectInterface.resolve(classMetaData);
105: TimerBeanValidator.validate(classMetaData);
106:
107: }
108:
109: // for each bean, call sub helper
110: if (classMetaData.isSession()) {
111: SessionBeanHelper.resolve(classMetaData);
112: }
113: }
114: }
115:
116: /**
117: * Gets method metadata on the given class metadata for the given method.
118: * @param bean the class metadata on which retrieve the method
119: * @param jMethod the method to get
120: * @param inherited get the correct method in super class, not inherited
121: * @param interfaceName the name of the interface that the class should have
122: * @return the method metadata, else exception
123: */
124: public static MethodAnnotationMetadata getMethod(
125: final ClassAnnotationMetadata bean, final JMethod jMethod,
126: final boolean inherited, final String interfaceName) {
127: MethodAnnotationMetadata method = bean
128: .getMethodAnnotationMetadata(jMethod);
129: if (method == null) {
130: throw new IllegalStateException("Bean '" + bean
131: + "' implements " + interfaceName + " but no "
132: + jMethod + " method found in metadata");
133: }
134: // gets the correct method on the correct level. (not the inherited method) if we don't want the inherited method.
135: if (method.isInherited() && !inherited) {
136: String super ClassName = bean.getSuperName();
137: // loop while class is not java.lang.Object
138: while (!JAVA_LANG_OBJECT.equals(super ClassName)) {
139: ClassAnnotationMetadata super MetaData = bean
140: .getEjbJarAnnotationMetadata()
141: .getClassAnnotationMetadata(super ClassName);
142: // If the method is found in the super class and is not inherited, use this one
143: if (super MetaData != null) {
144: MethodAnnotationMetadata super Method = super MetaData
145: .getMethodAnnotationMetadata(jMethod);
146: if (super Method != null
147: && !super Method.isInherited()) {
148: return super Method;
149: }
150: super ClassName = super MetaData.getSuperName();
151: } else {
152: // break the loop
153: super ClassName = JAVA_LANG_OBJECT;
154: }
155: }
156:
157: }
158:
159: return method;
160: }
161:
162: /**
163: * Gets all interfaces used by a class.
164: * @param sessionBean the metadata to analyze.
165: * @return the list of interfaces from a given class.
166: */
167: public static List<String> getAllInterfacesFromClass(
168: final ClassAnnotationMetadata sessionBean) {
169: // build list
170: List<String> allInterfaces = new ArrayList<String>();
171:
172: // Class to analyze
173: String className = sessionBean.getClassName();
174:
175: // loop while class is not java.lang.Object
176: while (!JAVA_LANG_OBJECT.equals(className)) {
177: ClassAnnotationMetadata metaData = sessionBean
178: .getEjbJarAnnotationMetadata()
179: .getClassAnnotationMetadata(className);
180: // find metadata, all interfaces found
181: if (metaData != null) {
182: String[] interfaces = metaData.getInterfaces();
183: if (interfaces != null) {
184: for (String itf : interfaces) {
185: allInterfaces.add(itf);
186: }
187: }
188: className = metaData.getSuperName();
189: } else {
190: // break the loop
191: className = JAVA_LANG_OBJECT;
192: }
193: }
194: return allInterfaces;
195: }
196: }
|