KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployment.*;
26 import com.sun.enterprise.tools.verifier.*;
27 import java.util.*;
28 import java.lang.reflect.Array JavaDoc;
29 import com.sun.enterprise.tools.verifier.tests.*;
30
31 /**
32  * Session/Entity Bean Transaction demarcation type test.
33  * Application Assembler may define attributes for the methods of the
34  * remote/home interfaces of the beans that require container managed
35  * transaction demarcation. All beans of the this type (container managed
36  * transactions) require container managed tranaction demarcation through
37  * the use of "container-transaction" element.
38  */

39 public class TransactionDemarcationType extends EjbTest implements EjbCheck {
40
41     static String JavaDoc[] EJBObjectMethods =
42     {
43         "getHomeHandle", "getEJBMetaData",
44         "getEJBHome", "getEJBLocalHome","getHandle",
45         "getPrimaryKey", "isIdentical"
46     };
47
48
49     /**
50      * Session/Entity Bean Transaction demarcation type test.
51      * Application Assembler may define attributes for the methods of the
52      * remote/home interfaces of the beans that require container managed
53      * transaction demarcation. All beans of the this type (container managed
54      * transactions) require container managed tranaction demarcation through
55      * the use of "container-transaction" element.
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     // hack try/catch block around test, to exit gracefully instead of
67
// crashing verifier on getMethodDescriptors() call, XML mods cause
68
// java.lang.ClassNotFoundException: verifier.ejb.hello.BogusEJB
69
// Replacing <ejb-class>verifier.ejb.hello.HelloEJB with
70
// <ejb-class>verifier.ejb.hello.BogusEJB...
71
try {
72         if ((descriptor instanceof EjbSessionDescriptor) ||
73         (descriptor instanceof EjbEntityDescriptor)) {
74                 boolean oneFailed = false;
75         String JavaDoc transactionType = descriptor.getTransactionType();
76         if (EjbDescriptor.CONTAINER_TRANSACTION_TYPE.equals(transactionType)) {
77             try {
78                         Arrays.sort(EJBObjectMethods);
79             ContainerTransaction containerTransaction = null;
80                         if (!descriptor.getMethodContainerTransactions().isEmpty()) {
81                             for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
82                                 MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
83                 if (Arrays.binarySearch(EJBObjectMethods, methodDescriptor.getName()) < 0) {
84                     containerTransaction =
85                                                 (ContainerTransaction) descriptor.getMethodContainerTransactions().get(methodDescriptor);
86                     
87                     if (containerTransaction != null ||
88                             containerTransaction.getTransactionAttribute()!=null) {
89                         String JavaDoc transactionAttribute =
90                             containerTransaction.getTransactionAttribute();
91           
92                         // danny is doing this in the DOL, but is it possible to not have
93
// any value for containerTransaction.getTransactionAttribute()
94
// in the DOL? if it is possible to have blank value for this,
95
// then this check is needed here, otherwise we are done and we
96
// don't need this check here
97
if (ContainerTransaction.NOT_SUPPORTED.equals(transactionAttribute)
98                             || ContainerTransaction.SUPPORTS.equals(transactionAttribute)
99                             || ContainerTransaction.REQUIRED.equals(transactionAttribute)
100                             || ContainerTransaction.REQUIRES_NEW.equals(transactionAttribute)
101                             || ContainerTransaction.MANDATORY.equals(transactionAttribute)
102                             || ContainerTransaction.NEVER.equals(transactionAttribute)) {
103                         addGoodDetails(result, compName);
104                         result.addGoodDetails(smh.getLocalString
105                                       (getClass().getName() + ".passed",
106                                        "TransactionAttribute [ {0} ] for method [ {1} ] is valid.",
107                                        new Object JavaDoc[] {transactionAttribute, methodDescriptor.getName()}));
108                         } else {
109                         oneFailed = true;
110                         addErrorDetails(result, compName);
111                         result.addErrorDetails(smh.getLocalString
112                                        (getClass().getName() + ".failed",
113                                         "Error: TransactionAttribute [ {0} ] for method [ {1} ] is not valid.",
114                                         new Object JavaDoc[] {transactionAttribute, methodDescriptor.getName()}));
115                         }
116                         } else {
117                         // Null transaction attributes are allowed in EJB 3. Default is REQUIRED.
118
if(getVerifierContext().getJavaEEVersion().compareTo(SpecVersionMapper.JavaEEVersion_5)<0) {
119                         oneFailed = true;
120                         addErrorDetails(result, compName);
121                         result.addErrorDetails(smh.getLocalString
122                                        (getClass().getName() + ".failedException",
123                                     "Error: TransactionAttribute is null for method [ {0} ]",
124                                     new Object JavaDoc[] {methodDescriptor.getName()}));
125                         }
126                         
127                         }
128                                 } // if you found a business method
129
else {
130                     //check if the ejb is a session bean
131
//and the method with transaction attribute belongs
132
//to home/local home interface
133
String JavaDoc ejbClass = methodDescriptor.getEjbClassSymbol();
134
135     /*** Fixed the bug: 4883730. ejbClassSymbol is null when method-intf is not
136      * defined in the xml, since it is an optional field. Removed the earlier
137      * checks. A null method-intf indicates that the method is supposed to be
138      * in both Local & Home interfaces. ***/

139 /*
140                                     String methodIntf = null;
141                                     try {
142                                         methodIntf = methodDescriptor.getEjbClassSymbol();
143                                     } catch ( Exception ex ) {}
144                                     if ( methodIntf == null ) { //|| methodIntf.equals("")
145                                         continue;
146                                     }
147
148 */

149                     boolean session = descriptor instanceof EjbSessionDescriptor;
150                     boolean entity = descriptor instanceof EjbEntityDescriptor;
151                     if (((ejbClass == null)
152                      || ejbClass.equals(MethodDescriptor.EJB_HOME)
153                      || ejbClass.equals(MethodDescriptor.EJB_LOCALHOME))
154                     && session) {
155                     oneFailed = true;
156                     addErrorDetails(result, compName);
157                     result.addErrorDetails(smh.getLocalString
158                                    (getClass().getName() + ".failedHome",
159                                 "Error: TransactionAttribute for method [ {0} ] is not valid. Home or Local Home interface methods of a session bean must not hvae a transaction attribute.",
160                                 new Object JavaDoc[] {methodDescriptor.getName()}));
161                     }
162                     //check if it is a session bean with remote/local interface
163
//and method with Tx attribute is "remove"
164
else if (((ejbClass == null)
165                           || ejbClass.equals(MethodDescriptor.EJB_REMOTE)
166                           || ejbClass.equals(MethodDescriptor.EJB_LOCAL))
167                          && session && methodDescriptor.getName().equals("remove")) {
168                     //check for style 3
169
//if remove method defined has parameters then pass else fail
170
if (methodDescriptor.getParameterClassNames() == null
171                         || methodDescriptor.getParameterClassNames().length == 0 ) {
172                         //style 2
173
oneFailed = true;
174                         addErrorDetails(result, compName);
175                         result.addErrorDetails(smh.getLocalString
176                                    (getClass().getName() + ".failedComp",
177                                     "Error: TransactionAttribute for method [ {0} ] is not valid. 'remove' method in Remote/Local interface of a session bean must not have a transaction attribute.",
178                                     new Object JavaDoc[] {methodDescriptor.getName()}));
179                     } else {
180                         addGoodDetails(result, compName);
181                         result.addGoodDetails(smh.getLocalString
182                                   (getClass().getName() + ".passedTest",
183                                    "TransactionAttribute for method [ {0} ] is valid.",
184                                    new Object JavaDoc[] {methodDescriptor.getName()}));
185                     }
186                     }
187                     else if (((ejbClass == null)
188                           || ejbClass.equals(MethodDescriptor.EJB_HOME)
189                           || ejbClass.equals(MethodDescriptor.EJB_LOCALHOME))
190                          && entity) {
191                     if (methodDescriptor.getParameterClassNames() == null
192                         || methodDescriptor.getParameterClassNames().length == 0) {
193                         //style 2
194
oneFailed = true;
195                         addErrorDetails(result, compName);
196                         result.addErrorDetails(smh.getLocalString
197                                    (getClass().getName() + ".failed1",
198                                     "Error: TransactionAttribute for method [ {0} ] is not valid. ",
199                                     new Object JavaDoc[] {methodDescriptor.getName()}));
200                     } else {
201                         addGoodDetails(result, compName);
202                         result.addGoodDetails(smh.getLocalString
203                                   (getClass().getName() + ".passedTest",
204                                    "TransactionAttribute for method [ {0} ] is valid.",
205                                    new Object JavaDoc[] { methodDescriptor.getName()}));
206                     }
207                     }
208                     else if (((ejbClass == null)
209                      || ejbClass.equals(MethodDescriptor.EJB_REMOTE)
210                      || ejbClass.equals(MethodDescriptor.EJB_LOCAL))
211                     && entity) {
212                     if ((methodDescriptor.getName()).equals("isIdentical")) {
213                         if(methodDescriptor.getParameterClassNames() == null
214                            || methodDescriptor.getParameterClassNames().length == 0 ) {
215                         addGoodDetails(result, compName);
216                         result.addGoodDetails(smh.getLocalString
217                                       (getClass().getName() + ".passedTest",
218                                        "TransactionAttribute for method [ {0} ] is valid.",
219                                        new Object JavaDoc[] {methodDescriptor.getName()}));
220                         } else {
221                         String JavaDoc[] paramList = methodDescriptor.getParameterClassNames();
222                         if(Array.getLength(paramList) == 1) {
223                             if (paramList[0].equals("javax.ejb.EJBObject")) {
224                             //style 3
225
oneFailed = true;
226                             addErrorDetails(result, compName);
227                             result.addErrorDetails(smh.getLocalString
228                                            (getClass().getName() + ".failed1",
229                                         "Error: TransactionAttribute for method [ {0} ] is not valid.",
230                                         new Object JavaDoc[] { methodDescriptor.getName()}));
231                             }
232                             else {
233                             addGoodDetails(result, compName);
234                             result.addGoodDetails(smh.getLocalString
235                                           (getClass().getName() + ".passedTest",
236                                            "TransactionAttribute for method [ {0} ] is valid.",
237                                            new Object JavaDoc[] { methodDescriptor.getName()}));
238                             }
239                         } else {
240                             addGoodDetails(result, compName);
241                             result.addGoodDetails(smh.getLocalString
242                                       (getClass().getName() + ".passedTest",
243                                        "TransactionAttribute for method [ {0} ] is valid.",
244                                        new Object JavaDoc[] { methodDescriptor.getName()}));
245                         }
246                         }
247                     }
248                     else { //for all other methods in entity bean
249
if ((methodDescriptor.getName()).equals("remove")) {
250                         addGoodDetails(result, compName);
251                         result.addGoodDetails(smh.getLocalString
252                                       (getClass().getName() + ".passedTest",
253                                        "TransactionAttribute for method [ {0} ] is valid.",
254                                        new Object JavaDoc[] { methodDescriptor.getName()}));
255                         }
256                         else {
257                         if (methodDescriptor.getParameterClassNames() == null
258                             || methodDescriptor.getParameterClassNames().length == 0) {
259                             //style 2
260
oneFailed = true;
261                             addErrorDetails(result, compName);
262                             result.failed(smh.getLocalString
263                                   (getClass().getName() + ".failedException1",
264                                    "Error: [ {0} ] should not have a Transaction Attribute",
265                                    new Object JavaDoc[] {methodDescriptor.getName()}));
266                         }
267                         }
268                     }
269                     }
270                 }
271                 }
272             } else {
273                 addNaDetails(result, compName);
274                 result.notApplicable(smh.getLocalString
275                          (getClass().getName() + ".notApplicable1",
276                           "There are no method permissions within this bean [ {0} ]",
277                           new Object JavaDoc[] {descriptor.getName()}));
278             }
279
280             } catch (Exception JavaDoc e) {
281             oneFailed = true;
282             addErrorDetails(result, compName);
283             result.failed(smh.getLocalString
284                       (getClass().getName() + ".failedException2",
285                        "Error: [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
286                        new Object JavaDoc[] {descriptor.getName(), e.getMessage(), descriptor.getName()}));
287             return result;
288             }
289                     if (oneFailed) {
290                         result.setStatus(Result.FAILED);
291                     } else {
292                         result.setStatus(Result.PASSED);
293                     }
294                     return result;
295         } else {
296             // not container managed, but is a session/entity bean
297
addNaDetails(result, compName);
298             result.notApplicable(smh.getLocalString
299                      (getClass().getName() + ".notApplicable2",
300                       "Bean [ {0} ] is not {1} managed, it is [ {2} ] managed.",
301                       new Object JavaDoc[] {descriptor.getName(),EjbDescriptor.CONTAINER_TRANSACTION_TYPE,transactionType}));
302         }
303         return result;
304         } else {
305         addNaDetails(result, compName);
306         result.notApplicable(smh.getLocalString
307                      (getClass().getName() + ".notApplicable",
308                       "[ {0} ] not called \n with a Session or Entity bean.",
309                       new Object JavaDoc[] {getClass()}));
310         return result;
311         }
312     } catch (Throwable JavaDoc t) {
313         addErrorDetails(result, compName);
314         result.failed(smh.getLocalString
315               (getClass().getName() + ".failedException2",
316                "Error: [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
317                new Object JavaDoc[] {descriptor.getName(), t.getMessage(), descriptor.getName()}));
318         return result;
319     }
320
321     }
322 }
323
Popular Tags