KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > homeintf > HomeMethodException


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.ejb.homeintf;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
30 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
31 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
32 import com.sun.enterprise.tools.verifier.Result;
33 import com.sun.enterprise.tools.verifier.Verifier;
34 import com.sun.enterprise.deployment.EjbDescriptor;
35 import com.sun.enterprise.deployment.EjbSessionDescriptor;
36 import com.sun.enterprise.deployment.EjbEntityDescriptor;
37
38 /**
39  * Enterprise Bean's ejbHome methods exceptions test.
40  * Each enterprise Bean class may define zero or more business(...) methods.
41  * The method signatures must follow these rules:
42  *
43  * Compatibility Note: EJB 1.0 allowed the ejbHome methods to throw the
44  * java.rmi.RemoteException to indicate a non-application exception. This
45  * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise bean
46  * should throw the javax.ejb.EJBException or another RuntimeException to
47  * indicate non-application exceptions to the Container (see Section 12.2.2).
48  * Note: Treat as a warning to user in this instance.
49  */

50 public class HomeMethodException extends EjbTest implements EjbCheck {
51     Result result = null;
52     ComponentNameConstructor compName = null;
53     
54     /**
55      * Enterprise Bean's ejbHome methods exceptions test.
56      * Each enterprise Bean class may define zero or more business(...) methods.
57      * The method signatures must follow these rules:
58      *
59      * Compatibility Note: EJB 1.0 allowed the ejbHome methods to throw the
60      * java.rmi.RemoteException to indicate a non-application exception. This
61      * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise bean
62      * should throw the javax.ejb.EJBException or another RuntimeException to
63      * indicate non-application exceptions to the Container (see Section 12.2.2).
64      * Note: Treat as a warning to user in this instance.
65      *
66      * @param descriptor the Enterprise Java Bean deployment descriptor
67      * @return <code>Result</code> the results for this assertion
68      */

69     public Result check(EjbDescriptor descriptor) {
70         
71         result = getInitializedResult();
72         compName = getVerifierContext().getComponentNameConstructor();
73         
74         if ((descriptor instanceof EjbSessionDescriptor) ||
75                 (descriptor instanceof EjbEntityDescriptor)) {
76             if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
77                 commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
78             }
79             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
80                 commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
81             }
82             
83         }
84         if(result.getStatus()!=Result.FAILED &&
85                 result.getStatus() != Result.WARNING) {
86             addGoodDetails(result, compName);
87             result.passed(smh.getLocalString
88                             (getClass().getName() + ".passed",
89                             "Valid Home method(s)."));
90         }
91         return result;
92     }
93     
94     /**
95      * This method is responsible for the logic of the test. It is called for
96      * both local and remote home interfaces.
97      *
98      * @param home for the Home Interface of the Ejb
99      * @param descriptor the Enterprise Java Bean deployment descriptor
100      * This parameter may be optional depending on the test
101      */

102     
103     
104     private void commonToBothInterfaces(String JavaDoc home,EjbDescriptor descriptor) {
105         try {
106             ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
107             Class JavaDoc rc = Class.forName(home, false, jcl);
108             
109             for (Method JavaDoc homeMethod : rc.getMethods()) {
110                 
111                 if (homeMethod.getDeclaringClass().getName().equals("javax.ejb.EJBHome")||
112                         homeMethod.getDeclaringClass().getName().equals("javax.ejb.EJBLocalHome"))
113                     continue;
114                 if (homeMethod.getName().startsWith("create") ||
115                         homeMethod.getName().startsWith("find") ||
116                         homeMethod.getName().startsWith("remove"))
117                     continue;
118                 
119                 Class JavaDoc beanClass = Class.forName(descriptor.getEjbClassName(), false, jcl);
120                 
121                 for (Method JavaDoc method : beanClass.getMethods()) {
122                     
123                     String JavaDoc methodName = "ejbHome" +
124                             Character.toUpperCase(homeMethod.getName().charAt(0)) +
125                             homeMethod.getName().substring(1);
126                     
127                     if (method.getName().equals(methodName)) {
128                         
129                         // Compatibility Note: EJB 1.0 allowed the business methods to throw
130
// the java.rmi.RemoteException to indicate a non-application
131
// exception. This practice is deprecated in EJB 1.1---an EJB 1.1
132
// compliant enterprise bean should throw the javax.ejb.EJBException
133
// or another RuntimeException to indicate non-application
134
// exceptions to the Container (see Section 12.2.2).
135
// Note: Treat as a warning to user in this instance
136
Class JavaDoc [] exceptions = method.getExceptionTypes();
137                         if(EjbUtils.isValidRemoteException(exceptions)) {
138                             addWarningDetails(result, compName);
139                             result.warning(smh.getLocalString
140                                     (getClass().getName() + ".warning",
141                                     "Error: Compatibility Note:" +
142                                     "\n A public Home method [ {0} ] was found, but" +
143                                     "\n EJB 1.0 allowed the 'ejbHome' methods to throw the " +
144                                     "\n java.rmi.RemoteException to indicate a non-application" +
145                                     "\n exception. This practice is deprecated in EJB 1.1" +
146                                     "\n ---an EJB 1.1 compliant enterprise bean should" +
147                                     "\n throw the javax.ejb.EJBException or another " +
148                                     "\n RuntimeException to indicate non-application exceptions" +
149                                     "\n to the Container. ",
150                                     new Object JavaDoc[] {method.getName()}));
151                         }
152                         
153                     }
154                 }
155                 
156             }
157             
158         } catch (Exception JavaDoc e) {
159             Verifier.debug(e);
160             addErrorDetails(result, compName);
161             result.failed(smh.getLocalString
162                     (getClass().getName() + ".failedException",
163                     "Error: Remote interface [ {0} ] or bean class [ {1} ] does " +
164                     "not exist or is not loadable within bean [ {2} ].",
165                     new Object JavaDoc[] {home,descriptor.getEjbClassName(),descriptor.getName()}));
166         }
167     }
168 }
169
Popular Tags