KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbpostcreatemethod > EjbPostCreateMethodException


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

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

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