View Javadoc

1   /**
2    * EasyBeans
3    * Copyright (C) 2006 Bull S.A.S.
4    * Contact: easybeans@objectweb.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
19   * USA
20   *
21   * --------------------------------------------------------------------------
22   * $Id: SessionBusinessInterfaceFinder.java 873 2006-07-16 18:26:10Z benoitf $
23   * --------------------------------------------------------------------------
24   */
25  
26  package com.bm.ejb3metadata.annotations.helper.bean.session;
27  
28  import com.bm.ejb3metadata.annotations.impl.JLocal;
29  import com.bm.ejb3metadata.annotations.impl.JRemote;
30  import com.bm.ejb3metadata.annotations.metadata.ClassAnnotationMetadata;
31  
32  /**
33   * This class finds the business interface if there are no business interfaces
34   * specified.
35   * 
36   * @author Florent Benoit
37   */
38  public final class SessionBusinessInterfaceFinder {
39  
40  	/**
41  	 * Helper class, no public constructor.
42  	 */
43  	private SessionBusinessInterfaceFinder() {
44  	}
45  
46  	/**
47  	 * Finds business interface in a session bean.
48  	 * 
49  	 * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0 Spec
50  	 *      ?4.6.6</a>
51  	 * @param sessionBean
52  	 *            Session bean to analyze
53  	 */
54  	public static void resolve(final ClassAnnotationMetadata sessionBean) {
55  
56  		JLocal jLocal = sessionBean.getLocalInterfaces();
57  		JRemote jRemote = sessionBean.getRemoteInterfaces();
58  
59  		// No business interface or empty annotation (@Remote or @Local)
60  		if ((jLocal == null && jRemote == null)
61  				|| (jLocal == null && jRemote != null && jRemote
62  						.getInterfaces().isEmpty())) {
63  
64  			// The following interfaces are excluded when determining whether
65  			// the bean class has
66  			// more than one interface: java.io.Serializable;
67  			// java.io.Externalizable;
68  			// any of the interfaces defined by the javax.ejb package;
69                          // any of net.sourceforge.cobertura interfaces
70  			String[] interfaces = sessionBean.getInterfaces();
71  			int numberItfFound = 0;
72  			String itfFound = null;
73  			for (String itf : interfaces) {
74  				if (!itf.equals(java.io.Serializable.class.getName().replace(
75  						".", "/"))
76  						&& !itf.equals(java.io.Externalizable.class.getName()
77  								.replace(".", "/"))
78  						&& !itf.startsWith("javax/ejb")
79                                                  && !itf.startsWith("net/sourceforge/cobertura")) {
80  					itfFound = itf;
81  					numberItfFound++;
82  				}
83  			}
84  
85  			if (numberItfFound == 0) {
86  				throw new IllegalStateException(
87  						"No business interface found on class '"
88  								+ sessionBean.getClassName() + "'.");
89  			}
90  
91  			if (numberItfFound > 1) {
92  				throw new IllegalStateException("More than 1 itf on class '"
93  						+ sessionBean.getClassName() + "'.");
94  			}
95  
96  			// If bean class implements a single interface, that interface is
97  			// assumed to be the business
98  			// interface of the bean. This business interface will be a local
99  			// interface unless the
100 			// interface is designated as a remote business interface by use of
101 			// the Remote annotation
102 			// on the bean class or interface or by means of the deployment
103 			// descriptor.
104 
105 			// Build a local interface if no @Remote annotation, else add
106 			// interface in the existing object
107 			if (jRemote == null) {
108 				JLocal addedJLocal = new JLocal();
109 				addedJLocal.addInterface(itfFound);
110 				sessionBean.setLocalInterfaces(addedJLocal);
111 			} else {
112 				jRemote.addInterface(itfFound);
113 				sessionBean.setRemoteInterfaces(jRemote);
114 			}
115 
116 		}
117 	}
118 }