KickJava   Java API By Example, From Geeks To Geeks.

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


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;
24
25 import java.lang.reflect.Field JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27
28 import com.sun.enterprise.deployment.EjbDescriptor;
29 import com.sun.enterprise.deployment.EjbSessionDescriptor;
30 import com.sun.enterprise.tools.verifier.Result;
31 import com.sun.enterprise.tools.verifier.Verifier;
32 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
35
36 /**
37  * The Bean Provider should not declare the session bean fields in the session
38  * bean class as transient.
39  *
40  * This is to allow the Container to swap out an instance's state through
41  * techniques other than the Java Serialization protocol. For example, the
42  * Container's Java Virtual Machine implementation may use a block of memory
43  * to keep the instance's variables, and the Container swaps the whole memory
44  * block to the disk instead of performing Java Serialization on the instance.
45  */

46 public class BeanFieldsTransient extends EjbTest implements EjbCheck {
47
48
49     /**
50      * The Bean Provider should not declare the session bean fields in the session
51      * bean class as transient.
52      *
53      * This is to allow the Container to swap out an instance's state through
54      * techniques other than the Java Serialization protocol. For example, the
55      * Container's Java Virtual Machine implementation may use a block of memory
56      * to keep the instance's variables, and the Container swaps the whole memory
57      * block to the disk instead of performing Java Serialization on the instance.
58      *
59      * @param descriptor the Enterprise Java Bean deployment descriptor
60      *
61      * @return <code>Result</code> the results for this assertion
62      */

63     public Result check(EjbDescriptor descriptor) {
64
65         Result result = getInitializedResult();
66         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
67
68         if (descriptor instanceof EjbSessionDescriptor) {
69             try {
70                 Class JavaDoc c = Class.forName(((EjbSessionDescriptor)descriptor).getEjbClassName(), false, getVerifierContext().getClassLoader());
71                 // fields should not be defined in the session bean class as transient.
72
Field JavaDoc [] fields = c.getDeclaredFields();
73                 for (int i = 0; i < fields.length; i++) {
74                     int modifiers = fields[i].getModifiers();
75                     if (!Modifier.isTransient(modifiers)) {
76                         continue;
77                     } else {
78                         addWarningDetails(result, compName);
79                         result.warning(smh.getLocalString
80                                 (getClass().getName() + ".warning",
81                                         "Warning: Field [ {0} ] defined within session bean class [ {1} ] is defined as transient. Session bean fields should not be defined in the session bean class as transient.",
82                                         new Object JavaDoc[] {fields[i].getName(),((EjbSessionDescriptor)descriptor).getEjbClassName()}));
83                     }
84                 }
85             } catch (ClassNotFoundException JavaDoc e) {
86                 Verifier.debug(e);
87                 addErrorDetails(result, compName);
88                 result.failed(smh.getLocalString
89                         (getClass().getName() + ".failedException",
90                                 "Error: [ {0} ] class not found.",
91                                 new Object JavaDoc[] {((EjbSessionDescriptor)descriptor).getEjbClassName()}));
92             } catch (Throwable JavaDoc t) {
93                 addWarningDetails(result, compName);
94                 result.warning(smh.getLocalString
95                         (getClass().getName() + ".warningException",
96                         "Warning: [ {0} ] class encountered [ {1} ]. " +
97                         "Cannot access fields of class [ {2} ] which is external to [ {3} ].",
98                          new Object JavaDoc[] {(descriptor).getEjbClassName(),t.toString(),
99                          t.getMessage(),
100                          descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri()}));
101             }
102             return result;
103         }
104
105         if(result.getStatus()!=Result.WARNING){
106             addGoodDetails(result, compName);
107             result.passed(smh.getLocalString
108                     (getClass().getName() + ".passed",
109                             "The session bean class has defined all fields " +
110                     "as non-transient fields."));
111
112         }
113         return result;
114     }
115 }
116
Popular Tags