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: SessionBusinessInterfaceFinder.java 873 2006-07-16 18:26:10Z benoitf $
023: * --------------------------------------------------------------------------
024: */package com.bm.ejb3metadata.annotations.helper.bean.session;
025:
026: import com.bm.ejb3metadata.annotations.impl.JLocal;
027: import com.bm.ejb3metadata.annotations.impl.JRemote;
028: import com.bm.ejb3metadata.annotations.metadata.ClassAnnotationMetadata;
029:
030: /**
031: * This class finds the business interface if there are no business interfaces
032: * specified.
033: *
034: * @author Florent Benoit
035: */
036: public final class SessionBusinessInterfaceFinder {
037:
038: /**
039: * Helper class, no public constructor.
040: */
041: private SessionBusinessInterfaceFinder() {
042: }
043:
044: /**
045: * Finds business interface in a session bean.
046: *
047: * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0 Spec
048: * ?4.6.6</a>
049: * @param sessionBean
050: * Session bean to analyze
051: */
052: public static void resolve(final ClassAnnotationMetadata sessionBean) {
053:
054: JLocal jLocal = sessionBean.getLocalInterfaces();
055: JRemote jRemote = sessionBean.getRemoteInterfaces();
056:
057: // No business interface or empty annotation (@Remote or @Local)
058: if ((jLocal == null && jRemote == null)
059: || (jLocal == null && jRemote != null && jRemote
060: .getInterfaces().isEmpty())) {
061:
062: // The following interfaces are excluded when determining whether
063: // the bean class has
064: // more than one interface: java.io.Serializable;
065: // java.io.Externalizable;
066: // any of the interfaces defined by the javax.ejb package.
067: String[] interfaces = sessionBean.getInterfaces();
068: int numberItfFound = 0;
069: String itfFound = null;
070: for (String itf : interfaces) {
071: if (!itf.equals(java.io.Serializable.class.getName()
072: .replace(".", "/"))
073: && !itf.equals(java.io.Externalizable.class
074: .getName().replace(".", "/"))
075: && !itf.startsWith("javax/ejb")) {
076: itfFound = itf;
077: numberItfFound++;
078: }
079: }
080:
081: if (numberItfFound == 0) {
082: throw new IllegalStateException(
083: "No business interface found on class '"
084: + sessionBean.getClassName() + "'.");
085: }
086:
087: if (numberItfFound > 1) {
088: throw new IllegalStateException(
089: "More than 1 itf on class '"
090: + sessionBean.getClassName() + "'.");
091: }
092:
093: // If bean class implements a single interface, that interface is
094: // assumed to be the business
095: // interface of the bean. This business interface will be a local
096: // interface unless the
097: // interface is designated as a remote business interface by use of
098: // the Remote annotation
099: // on the bean class or interface or by means of the deployment
100: // descriptor.
101:
102: // Build a local interface if no @Remote annotation, else add
103: // interface in the existing object
104: if (jRemote == null) {
105: JLocal addedJLocal = new JLocal();
106: addedJLocal.addInterface(itfFound);
107: sessionBean.setLocalInterfaces(addedJLocal);
108: } else {
109: jRemote.addInterface(itfFound);
110: sessionBean.setRemoteInterfaces(jRemote);
111: }
112:
113: }
114: }
115: }
|