KickJava   Java API By Example, From Geeks To Geeks.

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


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 must assume that the content of transient fields may be
38  * lost between the ejbPassivate and ejbActivate notifications. Therefore, the
39  * Bean Provider should not store in a transient field a reference to any of
40  * the following objects: SessionContext object; environment JNDI naming
41  * context and any its subcontexts; home and remote interfaces; and the
42  * UserTransaction interface. The restrictions on the use of transient fields
43  * ensure that Containers can use Java Serialization during passivation and
44  * activation.
45  */

46 public class TransientFieldsSerialization extends EjbTest implements EjbCheck {
47
48
49
50     /**
51      * The Bean Provider must assume that the content of transient fields may be
52      * lost between the ejbPassivate and ejbActivate notifications. Therefore, the
53      * Bean Provider should not store in a transient field a reference to any of
54      * the following objects: SessionContext object; environment JNDI naming
55      * context and any its subcontexts; home and remote interfaces; and the
56      * UserTransaction interface. The restrictions on the use of transient fields
57      * ensure that Containers can use Java Serialization during passivation and
58      * activation.
59      *
60      * @param descriptor the Enterprise Java Bean deployment descriptor
61      *
62      * @return <code>Result</code> the results for this assertion
63      */

64     public Result check(EjbDescriptor descriptor) {
65
66         Result result = getInitializedResult();
67         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
68         boolean isEjb30 = descriptor.getEjbBundleDescriptor()
69                               .getSpecVersion().equalsIgnoreCase("3.0");
70
71         if (descriptor instanceof EjbSessionDescriptor) {
72             try {
73                 Class JavaDoc c = Class.forName(((EjbSessionDescriptor)descriptor).getEjbClassName(), false,
74                                    getVerifierContext().getClassLoader());
75                 // Bean Provider should not store in a transient field a reference to
76
// any of the following objects: SessionContext object; environment
77
// JNDI naming context and any its subcontexts; home and remote
78
// interfaces; and the UserTransaction interface.
79
Field JavaDoc [] fields = c.getDeclaredFields();
80                 for (int i = 0; i < fields.length; i++) {
81                     int modifiers = fields[i].getModifiers();
82                     if (!Modifier.isTransient(modifiers)) {
83                         continue;
84                     } else {
85                         Class JavaDoc fc = fields[i].getType();
86                         // can't do anything with environment JNDI naming context and
87
// any its subcontexts
88
//sg133765: do we need to do something for business interface
89
if ((fc.getName().equals("javax.ejb.SessionContext")) ||
90                                 (fc.getName().equals("javax.transaction.UserTransaction")) ||
91                                 (fc.getName().equals(descriptor.getRemoteClassName())) ||
92                                 (fc.getName().equals(descriptor.getHomeClassName()))||
93                                 (fc.getName().equals(descriptor.getLocalClassName())) ||
94                                 (fc.getName().equals(descriptor.getLocalHomeClassName())) ||
95                                 (isEjb30 && fc.getName().equals("javax.ejb.EntityManager")) ||
96                                 (isEjb30 && fc.getName().equals("javax.ejb.EntityManagerFactory"))) {
97
98                             result.failed(smh.getLocalString
99                                     ("tests.componentNameConstructor",
100                                             "For [ {0} ]",
101                                             new Object JavaDoc[] {compName.toString()}));
102                             result.addErrorDetails(smh.getLocalString
103                                     (getClass().getName() + ".failed",
104                                     "Error: Field [ {0} ] defined within" +
105                                     " session bean class [ {1} ] is defined as transient. " +
106                                     "Session bean fields should not store in a " +
107                                     "transient field a reference to any of the following objects: " +
108                                     "SessionContext object; environment JNDI naming context and any " +
109                                     "its subcontexts; home and remote interfaces;" +
110                                     " and the UserTransaction interface.",
111                                             new Object JavaDoc[] {fields[i].getName(),
112                                             ((EjbSessionDescriptor)descriptor).getEjbClassName()}));
113                         }
114                     }
115                 }
116
117             } catch (ClassNotFoundException JavaDoc e) {
118                 Verifier.debug(e);
119                 result.addErrorDetails(smh.getLocalString
120                         ("tests.componentNameConstructor",
121                                 "For [ {0} ]",
122                                 new Object JavaDoc[] {compName.toString()}));
123                 result.failed(smh.getLocalString
124                         (getClass().getName() + ".failedException",
125                                 "Error: [ {0} ] class not found.",
126                                 new Object JavaDoc[] {((EjbSessionDescriptor)descriptor).getEjbClassName()}));
127             }
128         }
129         if(result.getStatus()!=Result.FAILED) {
130             addGoodDetails(result, compName);
131             result.passed(smh.getLocalString
132                   (getClass().getName() + ".passed",
133                    "This session bean class has not stored in a " +
134                     "transient field a reference to any of the following objects: " +
135                     "SessionContext object; environment JNDI naming context and" +
136                     " any its subcontexts; home and remote interfaces; and the " +
137                     "UserTransaction interface."));
138         }
139         return result;
140     }
141 }
142
143
144
Popular Tags