001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@objectweb.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: BusinessMethodResolver.java 451 2006-05-12 16:39:43Z benoitf $
023: * --------------------------------------------------------------------------
024: */package com.bm.ejb3metadata.annotations.helper.bean;
025:
026: import com.bm.ejb3metadata.annotations.JMethod;
027: import com.bm.ejb3metadata.annotations.metadata.ClassAnnotationMetadata;
028: import com.bm.ejb3metadata.annotations.metadata.MethodAnnotationMetadata;
029:
030: /**
031: * This class resolves the business method for bean class by looking at the
032: * interfaces.
033: * @author Florent Benoit
034: */
035: public final class BusinessMethodResolver {
036:
037: private final static org.apache.log4j.Logger logger = org.apache.log4j.Logger
038: .getLogger(BusinessMethodResolver.class);
039:
040: /**
041: * Name of the method used in constructor.
042: * This has to be ignored as this is never a business interface
043: */
044: private static final String CLASS_INIT = "<clinit>";
045:
046: /**
047: * Ignore constructor "method" of interfaces.
048: * Not a business interface.
049: */
050: private static final String CONST_INIT = "<init>";
051:
052: /**
053: * Helper class, no public constructor.
054: */
055: private BusinessMethodResolver() {
056:
057: }
058:
059: /**
060: * Found all business methods of a bean.<br>
061: * A business method is a method from one of the local or remote interfaces.
062: * @param classAnnotationMetadata class to analyze
063: */
064: public static void resolve(
065: final ClassAnnotationMetadata classAnnotationMetadata) {
066: loop(classAnnotationMetadata, classAnnotationMetadata);
067: }
068:
069: /**
070: * While there are super interfaces, loop on them.
071: * @param beanclassAnnotationMetadata the class that is analyzed
072: * @param visitingclassAnnotationMetadata classes from where we get
073: * interfaces
074: */
075: private static void loop(
076: final ClassAnnotationMetadata beanclassAnnotationMetadata,
077: final ClassAnnotationMetadata visitingclassAnnotationMetadata) {
078: // first, need to analyze all methods of interfaces used by this class
079: // then, set them as business method
080:
081: for (String itf : visitingclassAnnotationMetadata
082: .getInterfaces()) {
083: if (itf.startsWith("javax/ejb/")
084: || itf.startsWith("java/io/Serializable")
085: || itf.startsWith("java/io/Externalizable")) {
086: continue;
087: }
088:
089: // get meta data of the interface
090: ClassAnnotationMetadata itfMetadata = visitingclassAnnotationMetadata
091: .getEjbJarAnnotationMetadata()
092: .getClassAnnotationMetadata(itf);
093:
094: if (itfMetadata == null) {
095: logger.warn("No class was found for interface (" + itf
096: + ")");
097: continue;
098: }
099:
100: // for each method of the interface, set the business method to true
101: // in bean
102: for (MethodAnnotationMetadata methodData : itfMetadata
103: .getMethodAnnotationMetadataCollection()) {
104: JMethod itfMethod = methodData.getJMethod();
105:
106: // Ignore class init method
107: if (itfMethod.getName().equals(CLASS_INIT)
108: || itfMethod.getName().equals(CONST_INIT)) {
109: continue;
110: }
111:
112: // take the method from the bean class
113: MethodAnnotationMetadata beanMethod = beanclassAnnotationMetadata
114: .getMethodAnnotationMetadata(itfMethod);
115: if (beanMethod == null) {
116: // TODO: I18n
117: throw new IllegalStateException(
118: "No method was found for method "
119: + itfMethod
120: + " in class "
121: + beanclassAnnotationMetadata
122: .getClassName());
123: }
124: beanMethod.setBusinessMethod(true);
125: }
126:
127: // loop again
128: if (itfMetadata.getInterfaces() != null) {
129: loop(beanclassAnnotationMetadata, itfMetadata);
130: }
131: }
132: }
133:
134: }
|