KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > businessmethod > BusinessMethodExceptionCheck


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.businessmethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.Method JavaDoc;
27
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
31 import com.sun.enterprise.tools.verifier.tests.*;
32
33 /**
34  * Enterprise Bean's business(...) methods exceptions test.
35  * Each enterprise Bean class must define zero or more business(...) methods.
36  * The method signatures must follow these rules:
37  * Compatibility Note: EJB 2.1 specification states that All the exceptions defined in the
38  * throws clause of the matching method of the session bean class must be defined in the
39  * throws clause of the method of the remote interface. (see Section 7.11.5)
40  * Note: Treat as a warning to user in this instance.
41  */

42
43 public class BusinessMethodExceptionCheck extends EjbTest implements EjbCheck {
44     Result result = null;
45     ComponentNameConstructor compName = null;
46     
47     
48     /**
49      * Enterprise Bean's business(...) methods exceptions test.
50      * Each enterprise Bean class must define zero or more business(...) methods.
51      * The method signatures must follow these rules:
52      *
53      * Compatibility Note:EJB 2.1 specification states that All the exceptions defined in the
54      * throws clause of the matching method of the session bean class must be defined in the
55      * throws clause of the method of the remote interface. (see Section 7.11.5)
56      * Note: Treat as a warning to user in this instance.
57      *
58      * @param descriptor the Enterprise Java Bean deployment descriptor
59      * @return <code>Result</code> the results for this assertion
60      */

61     
62     public Result check(EjbDescriptor descriptor) {
63         
64         result = getInitializedResult();
65         compName = getVerifierContext().getComponentNameConstructor();
66         
67         if (descriptor instanceof EjbSessionDescriptor) {
68             if(descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName())) {
69                 commonToBothInterfaces(descriptor.getRemoteClassName(),descriptor);
70             }
71             if(descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName())) {
72                 commonToBothInterfaces(descriptor.getLocalClassName(),descriptor);
73             }
74             
75         }
76         if(result.getStatus() != Result.FAILED &&
77                 result.getStatus() != Result.WARNING) {
78             addGoodDetails(result, compName);
79             result.passed(smh.getLocalString
80                     (getClass().getName() + ".passed",
81                     "All the exceptions defined in the throws clause of the matching "+
82                     "business methods are defined in the throws clause of the method "+
83                     "of the remote interface "));
84         }
85         return result;
86     }
87     
88     
89     /**
90      * This method is responsible for the logic of the test. It is called for
91      * both local and remote interfaces.
92      * @param intf the remote Interface of the Ejb
93      * @param descriptor the Enterprise Java Bean deployment descriptor
94      */

95     
96     
97     
98     private void commonToBothInterfaces(String JavaDoc intf,EjbDescriptor descriptor) {
99         try {
100             Class JavaDoc intfClass = Class.forName(intf, false, getVerifierContext().getClassLoader());
101             Class JavaDoc beanClass = Class.forName(descriptor.getEjbClassName(),
102                                             false,
103                                             getVerifierContext().getClassLoader());
104             for (Method JavaDoc remoteMethod : intfClass.getMethods()) {
105                 
106                 // we don't test the EJB methods,testing only business methods
107
if (remoteMethod.getDeclaringClass().getName().equals("javax.ejb.EJBObject")||
108                         remoteMethod.getDeclaringClass().getName().equals("javax.ejb.EJBLocalObject"))
109                     continue;
110                 
111                 Class JavaDoc [] parameterTypes = remoteMethod.getParameterTypes();
112                 Method JavaDoc method =getMethod(beanClass,remoteMethod.getName(),parameterTypes);
113                 if(method == null)
114                     continue;
115                 Class JavaDoc [] remoteExceptions = remoteMethod.getExceptionTypes();
116                 Class JavaDoc [] exceptions = method.getExceptionTypes();
117                 
118                 /* EJB 2.1 specification has such statement in 7.11.5 Session Bean's
119                 Remote Interface section:"All the exceptions defined in the throws
120                 clause of the matching method of the session bean class must be defined
121                 in the throws clause of the method of the remote interface."
122                 */

123                 for (Class JavaDoc exception : exceptions) {
124                     boolean foundOne = false;
125                     for (Class JavaDoc remoteException : remoteExceptions)
126                         if(remoteException.getName().equals(exception.getName())) {
127                             foundOne = true;
128                             break;
129                         }
130                     if(!foundOne) {
131                         addWarningDetails(result, compName);
132                         result.warning(smh.getLocalString
133                                 (getClass().getName() + ".warning",
134                                 "Not Compatible Exception: A public business " +
135                                 "method [ {0} ] was found, but according to " +
136                                 "the EJB specification, all the exceptions " +
137                                 "defined in the throws clause of the matching " +
138                                 "method of the session bean class must be " +
139                                 "defined in the throws clause of the method " +
140                                 "of the remote interface. Exception [ {1} ] " +
141                                 "is not defined in the bean's remote interface.",
142                                 new Object JavaDoc[] {method.getName(),exception.getName()}));
143                     }
144                 }//end of for
145
}//end of for
146
} catch (Exception JavaDoc e) {
147             Verifier.debug(e);
148             addErrorDetails(result, compName);
149             result.addErrorDetails(smh.getLocalString
150                             (getClass().getName() + ".failed",
151                             "Remote interface [ {0} ] or bean class [ {1} ] does " +
152                             "not exist or is not loadable within bean [ {2} ].",
153                             new Object JavaDoc[] {intf,descriptor.getEjbClassName(),descriptor.getName()}));
154         }//end of catch block
155
}//End of CommonToBoth function
156

157 }
158
159
Popular Tags