KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
27 import java.util.Set JavaDoc;
28
29 import com.sun.enterprise.deployment.*;
30 import com.sun.enterprise.tools.verifier.*;
31 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
33 import com.sun.enterprise.tools.verifier.tests.*;
34
35 /**
36  * Enterprise Bean's business(...) methods exceptions test.
37  * Each enterprise Bean class must define zero or more business(...) methods.
38  * The method signatures must follow these rules:
39  *
40  * Compatibility Note: EJB 1.0 allowed the business methods to throw the
41  * java.rmi.RemoteException to indicate a non-application exception. This
42  * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise bean
43  * should throw the javax.ejb.EJBException or another RuntimeException to
44  * indicate non-application exceptions to the Container (see Section 12.2.2).
45  * Note: Treat as a warning to user in this instance.
46  */

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

67     public Result check(EjbDescriptor descriptor) {
68         
69         result = getInitializedResult();
70         compName = getVerifierContext().getComponentNameConstructor();
71         
72         if ((descriptor instanceof EjbSessionDescriptor) ||
73                 (descriptor instanceof EjbEntityDescriptor)) {
74             if(descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName()))
75                 commonToBothInterfaces(descriptor.getRemoteClassName(),descriptor);
76             
77             if(descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName()))
78                 commonToBothInterfaces(descriptor.getLocalClassName(),descriptor);
79             
80             Set JavaDoc<String JavaDoc> localAndRemoteInterfaces = descriptor.getLocalBusinessClassNames();
81             localAndRemoteInterfaces.addAll(descriptor.getRemoteBusinessClassNames());
82             
83             for (String JavaDoc localOrRemoteIntf : localAndRemoteInterfaces)
84                 commonToBothInterfaces(localOrRemoteIntf, descriptor);
85         }
86         
87         if(result.getStatus() != Result.FAILED &&
88                 result.getStatus() != Result.WARNING) {
89             addGoodDetails(result, compName);
90             result.passed(smh.getLocalString
91                                     (getClass().getName() + ".passed",
92                                     "Proper declaration of business method(s) found."));
93         }
94         return result;
95     }
96     
97     /**
98      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
99      * @param intf or component for the Remote/Local interface of the Ejb.
100      * @param descriptor the Enterprise Java Bean deployment descriptor
101      */

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