KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > ejbcreatemethod > EjbCreateMethodException


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.session.ejbcreatemethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import com.sun.enterprise.tools.verifier.tests.*;
28 import javax.ejb.SessionBean JavaDoc;
29 import java.lang.reflect.*;
30 import com.sun.enterprise.deployment.EjbDescriptor;
31 import com.sun.enterprise.deployment.EjbSessionDescriptor;
32 import com.sun.enterprise.tools.verifier.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 /**
36  * Session Bean's ejbCreate(...) methods exception test.
37  * Each session Bean class must define one or more ejbCreate(...) methods.
38  * The number and signatures of a session Bean's create methods are specific
39  * to each EJB class. The method signatures must follow these rules:
40  *
41  * The method name must be ejbCreate.
42  *
43  * Compatibility Note: EJB 1.0 allowed the ejbCreate method 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 EjbCreateMethodException extends EjbTest implements EjbCheck {
51
52
53     /**
54      * Session Bean's ejbCreate(...) methods exception test.
55      * Each session Bean class must define one or more ejbCreate(...) methods.
56      * The number and signatures of a session Bean's create methods are specific
57      * to each EJB class. The method signatures must follow these rules:
58      *
59      * The method name must be ejbCreate.
60      *
61      * Compatibility Note: EJB 1.0 allowed the ejbCreate method to throw the
62      * java.rmi.RemoteException to indicate a non-application exception. This
63      * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise bean
64      * should throw the javax.ejb.EJBException or another RuntimeException to
65      * indicate non-application exceptions to the Container (see Section 12.2.2).
66      * Note: Treat as a warning to user in this instance.
67      *
68      * @param descriptor the Enterprise Java Bean deployment descriptor
69      *
70      * @return <code>Result</code> the results for this assertion
71      */

72     public Result check(EjbDescriptor descriptor) {
73
74     Result result = getInitializedResult();
75     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
76
77     if (descriptor instanceof EjbSessionDescriptor) {
78         boolean oneFailed = false;
79         int foundWarning = 0;
80         try {
81         Context context = getVerifierContext();
82         ClassLoader JavaDoc jcl = context.getClassLoader();
83         Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
84
85         Class JavaDoc [] ejbCreateMethodParameterTypes;
86         int foundAtLeastOne = 0;
87         boolean ejbCreateFound = false;
88         boolean throwsRemoteException = false;
89                 // start do while loop here....
90
do {
91             Method [] methods = c.getDeclaredMethods();
92             for (int i = 0; i < methods.length; i++) {
93             // reset flags from last time thru loop
94
ejbCreateFound = false;
95             throwsRemoteException = false;
96
97             // The method name must be ejbCreate.
98
if (methods[i].getName().startsWith("ejbCreate")) {
99                 foundAtLeastOne++;
100                 ejbCreateFound = true;
101
102                 // Compatibility Note: EJB 1.0 allowed the ejbCreate method to throw
103
// the java.rmi.RemoteException to indicate a non-application
104
// exception. This practice is deprecated in EJB 1.1---an EJB 1.1
105
// compliant enterprise bean should throw the javax.ejb.EJBException
106
// or another RuntimeException to indicate non-application
107
// exceptions to the Container (see Section 12.2.2).
108
// Note: Treat as a warning to user in this instance.
109
Class JavaDoc [] exceptions = methods[i].getExceptionTypes();
110                 for (int z = 0; z < exceptions.length; ++z) {
111                 if ((exceptions[z].getName().equals("java.rmi.RemoteException")) ||
112                     (exceptions[z].getName().equals("RemoteException"))) {
113                     throwsRemoteException = true;
114                     break;
115                 }
116                 }
117
118                 // now display the appropriate results for this particular ejbCreate
119
// method
120
if (ejbCreateFound && (!throwsRemoteException) ) {
121                 result.addGoodDetails(smh.getLocalString
122                               ("tests.componentNameConstructor",
123                                "For [ {0} ]",
124                                new Object JavaDoc[] {compName.toString()}));
125                 result.addGoodDetails(smh.getLocalString
126                               (getClass().getName() + ".debug1",
127                                "For EJB Class [ {0} ] method [ {1} ]",
128                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
129                 result.addGoodDetails(smh.getLocalString
130                               (getClass().getName() + ".passed",
131                                " [ {0} ] properly declares [ {1} ] method which properly does not throw java.rmi.RemoteException.",
132                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
133                 } else if (ejbCreateFound && throwsRemoteException) {
134                 result.addWarningDetails(smh.getLocalString
135                               ("tests.componentNameConstructor",
136                                "For [ {0} ]",
137                                new Object JavaDoc[] {compName.toString()}));
138                 result.addWarningDetails(smh.getLocalString
139                              (getClass().getName() + ".debug1",
140                               "For EJB Class [ {0} ] method [ {1} ]",
141                               new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
142                 result.addWarningDetails(smh.getLocalString
143                              (getClass().getName() + ".warning",
144                               "Error: Compatibility Note:" +
145                               "\n An [ {0} ] method was found, but" +
146                               "\n EJB 1.0 allowed the ejbCreate method to throw the " +
147                               "\n java.rmi.RemoteException to indicate a non-application" +
148                               "\n exception. This practice is deprecated in EJB 1.1" +
149                               "\n ---an EJB 1.1 compliant enterprise bean should" +
150                               "\n throw the javax.ejb.EJBException or another " +
151                               "\n RuntimeException to indicate non-application exceptions" +
152                               "\n to the Container",
153                               new Object JavaDoc[] {methods[i].getName()}));
154                 foundWarning++;
155                 }
156             }
157             }
158                 } while (((c = c.getSuperclass()) != null) && (foundAtLeastOne == 0));
159         
160         if (foundAtLeastOne == 0){
161             result.addErrorDetails(smh.getLocalString
162                        ("tests.componentNameConstructor",
163                         "For [ {0} ]",
164                         new Object JavaDoc[] {compName.toString()}));
165             result.failed(smh.getLocalString
166                   (getClass().getName() + ".failed",
167                    "Error: [ {0} ] does not properly declare at least one ejbCreate(...) method. [ {1} ] is not a valid bean.",
168                    new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getEjbClassName()}));
169             oneFailed = true;
170         }
171         } catch (ClassNotFoundException JavaDoc e) {
172         Verifier.debug(e);
173         result.addErrorDetails(smh.getLocalString
174                        ("tests.componentNameConstructor",
175                     "For [ {0} ]",
176                     new Object JavaDoc[] {compName.toString()}));
177         result.failed(smh.getLocalString
178                   (getClass().getName() + ".failedException",
179                    "Error: [ {0} ] class not found.",
180                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
181         oneFailed = true;
182         }
183
184         if (oneFailed) {
185         result.setStatus(result.FAILED);
186         } else if (foundWarning > 0) {
187         result.setStatus(result.WARNING);
188         } else {
189         result.setStatus(result.PASSED);
190         }
191
192         return result;
193  
194     } else {
195         result.addNaDetails(smh.getLocalString
196                 ("tests.componentNameConstructor",
197                  "For [ {0} ]",
198                  new Object JavaDoc[] {compName.toString()}));
199         result.notApplicable(smh.getLocalString
200                  (getClass().getName() + ".notApplicable",
201                   "[ {0} ] expected {1} bean, but called with {2} bean.",
202                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
203         return result;
204     }
205     }
206 }
207
Popular Tags