KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbcreatemethod > EjbCreateMethodExceptions


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

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

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