KickJava   Java API By Example, From Geeks To Geeks.

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


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.beanclass;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.*;
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.tools.verifier.*;
29 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
30 import com.sun.enterprise.tools.verifier.tests.*;
31
32
33 /**
34  * Enterprise Java Bean class static final fields test.
35  * An enterprise Bean must not use read/write static fields. Using read-only
36  * static fields is allowed. Therefore, it is recommended that all static
37  * fields in the enterprise bean class be declared as final.
38  *
39  * This rule is required to ensure consistent runtime semantics because while
40  * some EJB Containers may use a single JVM to execute all enterprise bean's
41  * instances, others may distribute the instances across multiple JVMs.
42  */

43 public class EjbClassStaticFieldsFinal extends EjbTest implements EjbCheck {
44
45
46
47     /**
48      * Enterprise Java Bean class static final fields test.
49      * An enterprise Bean must not use read/write static fields. Using read-only
50      * static fields is allowed. Therefore, it is recommended that all static
51      * fields in the enterprise bean class be declared as final.
52      *
53      * This rule is required to ensure consistent runtime semantics because while
54      * some EJB Containers may use a single JVM to execute all enterprise bean's
55      * instances, others may distribute the instances across multiple JVMs.
56      *
57      * @param descriptor the Enterprise Java Bean deployment descriptor
58      *
59      * @return <code>Result</code> the results for this assertion
60      */

61     public Result check(EjbDescriptor descriptor) {
62
63     Result result = getInitializedResult();
64     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
65
66     // it is recommended that all static fields in the enterprise bean class
67
// be declared as final.
68
try {
69         Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
70
71         boolean oneFailed = false;
72         boolean badField = false;
73         Field [] fields = c.getDeclaredFields();
74         for (int i = 0; i < fields.length; i++) {
75         badField = false;
76         // hack to prevent compiler bug, don't want to process fields such as
77
// class$test$enforcer$hello$HelloHome, so if the field contains a "$"
78
// just skip it, otherwise process normally.
79
// if "$" does not occur as a substring, -1 is returned.
80
if (fields[i].getName().indexOf("$") == -1) {
81             int modifiers = fields[i].getModifiers();
82             if (Modifier.isStatic(modifiers)) {
83             if (Modifier.isFinal(modifiers)) {
84                 continue;
85             } else {
86                 if (!oneFailed) {
87                 oneFailed = true;
88                 }
89                 badField = true;
90             }
91             }
92         }
93   
94         if (badField) {
95             result.addWarningDetails(smh.getLocalString
96                          ("tests.componentNameConstructor",
97                           "For [ {0} ]",
98                           new Object JavaDoc[] {compName.toString()}));
99             result.warning(smh.getLocalString
100                    (getClass().getName() + ".warning",
101                     "Warning: Field [ {0} ] defined within bean class [ {1} ] is defined as static, but not defined as final. An enterprise Bean must not use read/write static fields. Using read-only static fields is allowed.",
102                     new Object JavaDoc[] {fields[i].getName(),(descriptor).getEjbClassName()}));
103         }
104         }
105         if (!oneFailed) {
106         result.addGoodDetails(smh.getLocalString
107                       ("tests.componentNameConstructor",
108                        "For [ {0} ]",
109                        new Object JavaDoc[] {compName.toString()}));
110         result.passed(smh.getLocalString
111                   (getClass().getName() + ".passed",
112                    "This bean class [ {0} ] has defined any and all static fields as final.",
113                    new Object JavaDoc[] {(descriptor).getEjbClassName()}));
114         }
115     } catch (ClassNotFoundException JavaDoc e) {
116         Verifier.debug(e);
117         result.addErrorDetails(smh.getLocalString
118                    ("tests.componentNameConstructor",
119                     "For [ {0} ]",
120                     new Object JavaDoc[] {compName.toString()}));
121         result.failed(smh.getLocalString
122               (getClass().getName() + ".failedException",
123                "Error: [ {0} ] class not found.",
124                new Object JavaDoc[] {(descriptor).getEjbClassName()}));
125     } catch (Exception JavaDoc e) {
126         Verifier.debug(e);
127         result.addErrorDetails(smh.getLocalString
128                    ("tests.componentNameConstructor",
129                     "For [ {0} ]",
130                     new Object JavaDoc[] {compName.toString()}));
131         result.failed(smh.getLocalString
132               (getClass().getName() + ".failedException1",
133                "Error: [ {0} ] class encountered [ {1} ]",
134                new Object JavaDoc[] {(descriptor).getEjbClassName(),e.getMessage()}));
135     } catch (Throwable JavaDoc t) {
136         result.addWarningDetails(smh.getLocalString
137                      ("tests.componentNameConstructor",
138                       "For [ {0} ]",
139                       new Object JavaDoc[] {compName.toString()}));
140         result.warning(smh.getLocalString
141               (getClass().getName() + ".warningException",
142                "Warning: [ {0} ] class encountered [ {1} ]. Cannot access fields of class [ {2} ] which is external to [ {3} ].",
143                new Object JavaDoc[] {(descriptor).getEjbClassName(),t.toString(), t.getMessage(), descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri()}));
144     }
145     return result;
146
147     }
148 }
149
Popular Tags