KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
26
27 import com.sun.enterprise.deployment.ContainerTransaction;
28 import com.sun.enterprise.deployment.EjbDescriptor;
29 import com.sun.enterprise.deployment.EjbSessionDescriptor;
30 import com.sun.enterprise.deployment.MethodDescriptor;
31 import com.sun.enterprise.tools.verifier.Context;
32 import com.sun.enterprise.tools.verifier.Result;
33 import com.sun.enterprise.tools.verifier.Verifier;
34 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
35 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
36 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
37
38 /**
39  * Optionally implemented SessionSynchronization interface transaction
40  * demarcation test.
41  * If an enterprise bean implements the javax.ejb.SessionSynchronization
42  * interface, the Application Assembler can specify only the following values
43  * for the transaction attributes of the bean's methods:
44  * Required
45  * RequiresNew
46  * Mandatory
47  */

48 public class TransactionDemarcationSessionSynchronizationInterface extends EjbTest implements EjbCheck {
49
50
51     /**
52      * Optionally implemented SessionSynchronization interface transaction
53      * demarcation test.
54      * If an enterprise bean implements the javax.ejb.SessionSynchronization
55      * interface, the Application Assembler can specify only the following values
56      * for the transaction attributes of the bean's methods:
57      * Required
58      * RequiresNew
59      * Mandatory
60      *
61      * @param descriptor the Enterprise Java Bean deployment descriptor
62      *
63      * @return <code>Result</code> the results for this assertion
64      */

65     public Result check(EjbDescriptor descriptor) {
66
67         Result result = getInitializedResult();
68         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
69         boolean oneFound = false;
70
71         if (descriptor instanceof EjbSessionDescriptor) {
72             try {
73                 Context context = getVerifierContext();
74                 ClassLoader JavaDoc jcl = context.getClassLoader();
75                 Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
76                 // walk up the class tree
77
do {
78                     Class JavaDoc[] interfaces = c.getInterfaces();
79
80                     for (int i = 0; i < interfaces.length; i++) {
81                         if (interfaces[i].getName().equals("javax.ejb.SessionSynchronization")) {
82                             oneFound = true;
83                             break;
84                         }
85                     }
86                 } while ((c=c.getSuperclass()) != null);
87
88             } catch (ClassNotFoundException JavaDoc e) {
89                 Verifier.debug(e);
90                 addErrorDetails(result, compName);
91                 result.failed(smh.getLocalString
92                         (getClass().getName() + ".failedException1",
93                                 "Error: [ {0} ] class not found.",
94                                 new Object JavaDoc[] {descriptor.getEjbClassName()}));
95                 return result;
96             }
97
98             // If an enterprise bean implements the javax.ejb.SessionSynchronization
99
// interface, the Application Assembler can specify only the following
100
// values for the transaction attributes of the bean's methods:
101
// Required, RequiresNew, Mandatory
102
if (oneFound) {
103                 String JavaDoc transactionAttribute = "";
104                 ContainerTransaction containerTransaction = null;
105                 boolean oneFailed = false;
106                 if (!descriptor.getMethodContainerTransactions().isEmpty()) {
107                     for (Enumeration JavaDoc ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
108                         MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
109                         containerTransaction =
110                                 (ContainerTransaction) descriptor.getMethodContainerTransactions().get(methodDescriptor);
111
112                         if (!(containerTransaction != null && properAttribDefined(containerTransaction))) {
113                             transactionAttribute =
114                                     containerTransaction.getTransactionAttribute();
115                             addErrorDetails(result, compName);
116                             result.failed(smh.getLocalString
117                                     (getClass().getName() + ".failed",
118                                             "Error: TransactionAttribute [ {0} ] for method [ {1} ] is not valid.",
119                                             new Object JavaDoc[] {transactionAttribute, methodDescriptor.getName()}));
120
121                         }
122                     }
123                 }
124             }
125         }
126
127         if(result.getStatus()!=Result.FAILED) {
128             addGoodDetails(result, compName);
129             result.passed(smh.getLocalString
130                     (getClass().getName() + ".passed",
131                             "TransactionAttributes are defined properly for the bean"));
132         }
133         return result;
134     }
135
136     private boolean properAttribDefined(ContainerTransaction containerTransaction) {
137         String JavaDoc transactionAttribute = containerTransaction.getTransactionAttribute();
138         return (ContainerTransaction.REQUIRED.equals(transactionAttribute)
139                 || ContainerTransaction.REQUIRES_NEW.equals(transactionAttribute)
140                 || ContainerTransaction.MANDATORY.equals(transactionAttribute));
141
142     }
143 }
144
Popular Tags