KickJava   Java API By Example, From Geeks To Geeks.

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


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;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.*;
27 import java.util.*;
28 import java.rmi.*;
29 import com.sun.enterprise.deployment.*;
30 import com.sun.enterprise.tools.verifier.*;
31 import java.lang.ClassLoader JavaDoc;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * Exceptions checked for CreateException, FinderException, RemoteException
36  * compliance test.
37  *
38  */

39 public class EjbUtils {
40
41
42
43     /**
44      * The names of the fields in the primary key class must be a subset of the
45      * names of the container-managed fields.
46      * Verify the following:
47      *
48      * The primary key class field must be a subset of the names of the
49      * container-managed fields.
50      *
51      * @param field the field to be checked for containment within the names of the
52      * container-managed fields
53      * @param CMPFields the Set of contianer managed fields
54      *
55      * @return <code>boolean</code> true if field is subset of CMP fields, false otherwise
56      */

57     public static boolean isFieldSubsetOfCMP(Field field, Set CMPFields) {
58     if (CMPFields.contains(new FieldDescriptor(field))) {
59         return true;
60     } else {
61         return false;
62     }
63     }
64
65
66     /**
67      * The names of the fields in the primary key class must correspond to the
68      * field names of the entity bean class that comprise the key.
69      * Verify the following:
70      *
71      * The primary key class field must correspond to the field names of the
72      * entity bean class that comprise the key.
73      *
74      * @param field the field to be checked for matching bean field
75      * @param beanFields the Set of contianer managed fields
76      *
77      * @return <code>boolean</code> true if field is subset of bean class fields, false otherwise
78      */

79     public static boolean isPKFieldMatchingBeanFields(Field field, Vector beanFields) {
80
81       for (int i = 0; i < beanFields.size(); i++) {
82           if (((FieldDescriptor)beanFields.elementAt(i)).getName().equals(field.getName())) {
83           return true;
84       } else {
85               continue;
86       }
87       }
88       // if you made it here, then field[] didn't contain field
89
return false;
90     }
91
92
93     /**
94      * Method exception javax.ejb.CreateException checked for compliance
95      * test.
96      *
97      * Verify the following:
98      *
99      * The home/remote interface methods exception types must be legal types for
100      * CreateException.
101      * This means that their exception must throw javax.ejb.CreateException.
102      *
103      * @param methodExceptions the exceptions to be checked for throws
104      * javax.ejb.CreateException
105      *
106      * @return <code>boolean</code> true if exceptions throw javax.ejb.CreateException, false otherwise
107      */

108     public static boolean isValidCreateException(Class JavaDoc [] methodExceptions) {
109     // methods must throw javax.ejb.CreateException
110
boolean throwsCreateException = false;
111     for (int kk = 0; kk < methodExceptions.length; ++kk) {
112         if ((methodExceptions[kk].getName().equals("javax.ejb.CreateException")) ||
113         (methodExceptions[kk].getName().equals("CreateException"))) {
114         throwsCreateException = true;
115         break;
116         }
117     }
118     return throwsCreateException;
119
120     }
121
122
123
124     /**
125      * Method exception java.rmi.RemoteException checked for compliance
126      * test.
127      *
128      * Verify the following:
129      *
130      * The home/remote interface methods exception types must be legal types for
131      * RemoteException.
132      * This means that their exception must throw java.rmi.RemoteException
133      *
134      * @param methodExceptions the exceptions to be checked for throws
135      * java.rmi.RemoteException
136      *
137      * @return <code>boolean</code> true if exceptions throw java.rmi.RemoteException, false otherwise
138      */

139     public static boolean isValidRemoteException(Class JavaDoc [] methodExceptions) {
140     // methods must throw java.rmi.RemoteException
141
boolean throwsRemoteException = false;
142     for (int kk = 0; kk < methodExceptions.length; ++kk) {
143         if ((methodExceptions[kk].getName().equals("java.rmi.RemoteException")) ||
144         (methodExceptions[kk].getName().equals("RemoteException"))) {
145         throwsRemoteException = true;
146         break;
147         }
148     }
149     return throwsRemoteException;
150
151     }
152
153
154
155     /**
156      * Method exception javax.ejb.ObjectNotFoundException checked for compliance
157      * test.
158      *
159      * Verify the following:
160      *
161      * The ObjectNotFoundException is a subclass of FinderException. It is
162      * thrown by the ejbFind<METHOD>(...) methods to indicate that the
163      * requested entity object does not exist.
164      *
165      * Only single-object finders (see Subsection 9.1.8) may throw this
166      * exception. Multi-object finders must not throw this exception.
167      *
168      * @param methodExceptions the exceptions to be checked for throws
169      * javax.ejb.ObjectNotFoundException
170      *
171      * @return <code>boolean</code> true if exceptions throw javax.ejb.ObjectNotFoundException, false otherwise
172      */

173     public static boolean isValidObjectNotFoundExceptionException(Class JavaDoc [] methodExceptions) {
174     // methods must throw javax.ejb.ObjectNotFoundException
175
boolean throwsObjectNotFoundException = false;
176     for (int kk = 0; kk < methodExceptions.length; ++kk) {
177         if ((methodExceptions[kk].getName().equals("javax.ejb.ObjectNotFoundException")) ||
178         (methodExceptions[kk].getName().equals("ObjectNotFoundException"))) {
179         throwsObjectNotFoundException = true;
180         break;
181         }
182     }
183     return throwsObjectNotFoundException;
184     }
185
186
187
188
189     /**
190      * Method exception javax.ejb.FinderException checked for compliance
191      * test.
192      *
193      * Verify the following:
194      *
195      * The home/remote interface methods exception types must be legal types for
196      * FinderException
197      * This means that their exception must throw javax.ejb.FinderException
198      *
199      * @param methodExceptions the exceptions to be checked for throws
200      * javax.ejb.FinderException
201      *
202      * @return <code>boolean</code> true if exceptions throw javax.ejb.FinderException, false otherwise
203      */

204     public static boolean isValidFinderException(Class JavaDoc [] methodExceptions) {
205     // methods must throw javax.ejb.FinderException
206
boolean throwsFinderException = false;
207     for (int kk = 0; kk < methodExceptions.length; ++kk) {
208         if ((methodExceptions[kk].getName().equals("javax.ejb.FinderException")) ||
209         (methodExceptions[kk].getName().equals("FinderException"))) {
210         throwsFinderException = true;
211         break;
212         }
213     }
214     return throwsFinderException;
215
216     }
217
218
219     /** Class checked for implementing java.io.Serializable interface test.
220      * Verify the following:
221      *
222      * The class must implement the java.io.Serializable interface, either
223      * directly or indirectly.
224      *
225      * @param serClass the class to be checked for Rmi-IIOP value type
226      * compliance
227      *
228      * @return <code>boolean</code> true if class implements java.io.Serializable, false otherwise
229      */

230     public static boolean isValidSerializableType(Class JavaDoc serClass) {
231
232         if (java.io.Serializable JavaDoc.class.isAssignableFrom(serClass))
233            return true;
234         else
235            return false;
236
237         /**
238         // This complex logic is replaced by the above simple logic
239     Class c = serClass;
240     boolean validInterface = false;
241     boolean badOne = false;
242     // The class must implement the java.io.Serializable interface, either
243     // directly or indirectly,
244     // walk up the class tree
245
246     if (c.getName().equals("java.io.Serializable")) {
247         validInterface = true;
248         return validInterface;
249     }
250     do {
251         Class[] interfaces = c.getInterfaces();
252         for (int i = 0; i < interfaces.length; i++) {
253         if (interfaces[i].getName().equals("java.io.Serializable")) {
254             validInterface = true;
255             break;
256         } else {
257             // walk up the class tree of the interface and see if it
258             // implements java.io.Serializable
259             Class superClass = interfaces[i];
260             do {
261             if (superClass.getName().equals("java.io.Serializable")) {
262                 validInterface = true;
263                 break;
264             }
265             } while ((((superClass=superClass.getSuperclass()) != null) && (!validInterface)));
266         }
267         }
268     } while ((((c=c.getSuperclass()) != null) && (!validInterface)));
269
270
271     if (validInterface) {
272         return true;
273     } else {
274         return false;
275     }
276        **/

277     }
278
279
280
281     /**
282      * Method application exception checked for compliance test.
283      *
284      * Verify the following:
285      *
286      * An application exception is an exception defined in the throws clause of
287      * a method in the Bean's home interface, other than java.rmi.RemoteException.
288      * An application exception must not be defined as a subclass of the
289      * java.lang.RuntimeException, or of the java.rmi.RemoteException. These are
290      * reserved for system exceptions.
291      * The javax.ejb.CreateException, javax.ejb.RemoveException,
292      * javax.ejb.FinderException, and subclasses thereof, are considered to be
293      * application exceptions.
294      *
295      * @param methodExceptions the exceptions to be checked for throws
296      * application exception
297      *
298      * @return <code>boolean</code> true if exceptions are valid application exception, false otherwise
299      */

300     public static boolean isValidApplicationException(Class JavaDoc [] methodExceptions) {
301     for (int kk = 0; kk < methodExceptions.length; ++kk) {
302      Class JavaDoc ex=methodExceptions[kk];
303      //check 0: app exception set is all exceptions minus java.rmi.RemoteException.
304
if (java.rmi.RemoteException JavaDoc.class != ex) {
305         //check 1: app exception must subclass java.lang.Exception
306
if (!java.lang.Exception JavaDoc.class.isAssignableFrom(ex)) return false;
307         //check 2: app exception must not subclass java.lang.RuntimeException or java.rmi.RemoteException
308
if(java.rmi.RemoteException JavaDoc.class.isAssignableFrom(ex) || java.lang.RuntimeException JavaDoc.class.isAssignableFrom(ex)) {
309                     return false;
310                 }
311          }
312     }
313     return true;
314     }
315 }
316
Popular Tags