KickJava   Java API By Example, From Geeks To Geeks.

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


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.entity;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.deployment.*;
27 import com.sun.enterprise.tools.verifier.*;
28 import com.sun.enterprise.tools.verifier.tests.ejb.MethodUtils;
29 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
30 import java.lang.ClassLoader JavaDoc;
31 import com.sun.enterprise.tools.verifier.tests.*;
32 import java.util.*;
33 import java.lang.reflect.*;
34
35 /**
36  * Entity Bean transaction demarcation type home interface methods test.
37  * Transaction attributes must be specified for the methods of an entity
38  * bean's home interface, excluding getEJBMetaData and getHomeHandle methods.
39  */

40  public class TransactionDemarcationHomeInterface extends EjbTest implements EjbCheck {
41
42      static String JavaDoc[] EJBObjectMethods = { "getHomeHandle", "getEJBMetaData"};
43      
44      Result result = null;
45      ComponentNameConstructor compName = null;
46      
47     /**
48      * Entity Bean transaction demarcation type home interface methods test.
49      * Transaction attributes must be specified for the methods of an entity
50      * bean's home interface, excluding getEJBMetaData and getHomeHandle methods.
51      *
52      * @param descriptor the Enterprise Java Bean deployment descriptor
53      *
54      * @return <code>Result</code> the results for this assertion
55      */

56     public Result check(EjbDescriptor descriptor) {
57
58     result = getInitializedResult();
59     compName = getVerifierContext().getComponentNameConstructor();
60     boolean oneFailed = false;
61     // hack try/catch block around test, to exit gracefully instead of
62
// crashing verifier on getMethodDescriptors() call, XML mods cause
63
// java.lang.ClassNotFoundException: verifier.ejb.hello.BogusEJB
64
// Replacing <ejb-class>verifier.ejb.hello.HelloEJB with
65
// <ejb-class>verifier.ejb.hello.BogusEJB...
66
try {
67         // Transaction attributes must be specified for the methods of an entity
68
// bean's home interface, excluding getEJBMetaData and getHomeHandle methods
69

70         if (descriptor instanceof EjbEntityDescriptor) {
71         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
72             oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor, MethodDescriptor.EJB_HOME);
73         else {
74             addNaDetails(result, compName);
75             result.notApplicable(smh.getLocalString
76                          (getClass().getName() + ".notApplicable2",
77                           "There is no home interface specified for [ {0} ]",
78                           new Object JavaDoc[] {descriptor.getName()}));
79             return result;
80         }
81         //donot need to look at the local interface since the clause is applicable to only the remote interface
82
/*if(oneFailed == false) {
83           if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
84           oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
85           }*/

86         if (oneFailed) {
87             result.setStatus(result.FAILED);
88         } else {
89             result.setStatus(result.PASSED);
90         }
91   
92         return result;
93         } else {
94         addNaDetails(result, compName);
95         result.notApplicable(smh.getLocalString
96                      (getClass().getName() + ".notApplicable",
97                       "{0} expected {1} bean, but called with {2} bean.",
98                       new Object JavaDoc[] {getClass(),"Entity","Session"}));
99         return result;
100         }
101     } catch (Throwable JavaDoc t) {
102         addErrorDetails(result, compName);
103         result.failed(smh.getLocalString
104               (getClass().getName() + ".failedException1",
105                "Error: Home interface does not contain class [ {0} ] within bean [ {1} ]",
106                new Object JavaDoc[] { t.getMessage(), descriptor.getName()}));
107         return result;
108     }
109     }
110
111      /**
112      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
113      * @param descriptor the Enterprise Java Bean deployment descriptor
114      * @param home for the Home interface of the Ejb.
115      * @param methodIntf is the interface type
116      * @return boolean the results for this assertion i.e if a test has failed or not
117      */

118
119     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor, String JavaDoc methodIntf) {
120     boolean oneFailed = false;
121     try {
122         Arrays.sort(EJBObjectMethods);
123         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
124         Method methods[] = c.getDeclaredMethods();
125         boolean lookForIt = false;
126         
127         for (int i=0; i< methods.length; i++) {
128         if (Arrays.binarySearch(EJBObjectMethods, methods[i].getName()) < 0) {
129                         try {
130             ContainerTransaction containerTransaction = null;
131             boolean resolved = false;
132 /*
133                         // This flag is a workaround introduced by Harminder
134                         // because currently methodDescriptor.getEjbClassSymbol() is
135                         // returning NULL
136                         //boolean allMethods = false;
137                         boolean wildCardWasPresent = false;
138 */

139
140             if (!descriptor.getMethodContainerTransactions().isEmpty()) {
141                 for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
142                 lookForIt = false;
143                 
144                 MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
145  
146     /*** Fixed the bug: 4883730. ejbClassSymbol is null when method-intf is not
147      * defined in the xml, since it is an optional field. Removed the earlier
148      * checks. A null method-intf indicates that the method is supposed to be
149      * in both Local & Home interfaces. ***/

150 /*
151                                 // This code is a workaround introduced by Harminder
152                                 // because currently methodDescriptor.getEjbClassSymbol() is
153                                 // returning NULL
154                                 String methodIntf = null;
155                                 try {
156                                     methodIntf = methodDescriptor.getEjbClassSymbol();
157                                 } catch ( Exception ex ) {}
158                                 if ( methodIntf == null ) { //|| methodIntf.equals("")
159                                     //probably a wildcard was there
160                                     wildCardWasPresent = true;
161                                     continue;
162                                 }
163                                  //allMethods = true;
164                                  // end of workaround
165 */

166                                 
167                 
168                 // here we have to check that each method descriptor
169
// corresponds to a or some methods on the home interface
170
// according to the six styles
171
// style 1)
172
if (methodDescriptor.getName().equals(MethodDescriptor.ALL_EJB_METHODS)) {
173                     // if getEjbClassName() is Remote -> CARRY ON
174
// if Home - PASS
175
if (methodDescriptor.getEjbClassSymbol() == null) {
176                     lookForIt = true;
177                     } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)) {
178                     lookForIt = true;
179                     // if empty String PASS
180
} else if (methodDescriptor.getEjbClassSymbol().equals("")) {
181                     lookForIt = true;
182                     }else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) {
183                     lookForIt = true;
184                     } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE)) {
185                     lookForIt = false;
186                     } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCAL)) {
187                     lookForIt = false;
188                     // else (Bogus)
189
} else {
190                     // carry on & don't look for
191
// container transaction
192
lookForIt = false;
193                     }
194                 } else if (methodDescriptor.getParameterClassNames() == null) {
195                                         
196                     // if (getEjbClassSybol() is Home or is the empty String AND if methods[i].getName().equals(methodDescriptor.getName())
197
if (((methodDescriptor.getEjbClassSymbol() == null) ||
198                      methodDescriptor.getEjbClassSymbol().equals("") ||
199                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)||
200                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) &&
201                     (methods[i].getName().equals(methodDescriptor.getName()))) {
202                     // PASS
203
lookForIt = true;
204                     } else {
205                     // carry on
206
lookForIt = false;
207                     }
208                 } else {
209                     
210                     // if (getEjbClassSybol() is Home or is the empty String AND if methods[i].getName().equals(methodDescriptor.getName()) AND
211
// the parameters of the method[i] are the same as the parameters of the method descriptor )
212

213                     if (((methodDescriptor.getEjbClassSymbol() == null) ||
214                      methodDescriptor.getEjbClassSymbol().equals("") ||
215                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)||
216                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) &&
217                     (methods[i].getName().equals(methodDescriptor.getName())) &&
218                     (MethodUtils.stringArrayEquals(methodDescriptor.getParameterClassNames(), (new MethodDescriptor(methods[i], methodIntf)).getParameterClassNames()))) {
219                     // PASS
220
lookForIt = true;
221                     } else {
222                     // CARRY ON
223
lookForIt = false;
224                     }
225                     
226                 }
227                 
228                 if (lookForIt) {
229                     containerTransaction =
230                     (ContainerTransaction) descriptor.getMethodContainerTransactions().get(methodDescriptor);
231                     if (containerTransaction != null) {
232                     String JavaDoc transactionAttribute =
233                         containerTransaction.getTransactionAttribute();
234                     
235                     // danny is doing this in the DOL, but is it possible to not have
236
// any value for containerTransaction.getTransactionAttribute()
237
// in the DOL? if it is possible to have blank value for this,
238
// then this check is needed here, otherwise we are done and we
239
// don't need this check here
240
if (ContainerTransaction.NOT_SUPPORTED.equals(transactionAttribute)
241                         || ContainerTransaction.SUPPORTS.equals(transactionAttribute)
242                         || ContainerTransaction.REQUIRED.equals(transactionAttribute)
243                         || ContainerTransaction.REQUIRES_NEW.equals(transactionAttribute)
244                         || ContainerTransaction.MANDATORY.equals(transactionAttribute)
245                         || ContainerTransaction.NEVER.equals(transactionAttribute)
246                         || (!transactionAttribute.equals(""))) {
247                         addGoodDetails(result, compName);
248                         result.addGoodDetails(smh.getLocalString
249                         (getClass().getName() + ".passed",
250                              "Valid: [ {0} ] TransactionAttribute [ {1} ] for method [ {2} ] is valid. Transaction attributes must be specified for all methods except getEJBMetaData and getHomeHandle methods of home interface [ {3} ]",
251                          new Object JavaDoc[] {methods[i].getName(),transactionAttribute,methodDescriptor.getName(), home}));
252                         resolved = true;
253                     } else {
254                         oneFailed = true;
255                         addErrorDetails(result, compName);
256                         result.addErrorDetails(smh.getLocalString
257                          (getClass().getName() + ".failed",
258                           "Error: [ {0} ] TransactionAttribute [ {1} ] for method [ {2} ] " +
259                           "is not valid. Transaction attributes must be specified for " +
260                           "all methods except getEJBMetaData and getHomeHandle methods of "+
261                           "home interface [ {3} ]",
262                           new Object JavaDoc[] {methods[i].getName(), transactionAttribute, methodDescriptor.getName(),home}));
263                     }
264                     } else {
265                     oneFailed = true;
266                     result.addErrorDetails(smh.getLocalString
267                                    (getClass().getName() + ".failedException",
268                                 "Error: TransactionAttribute is null for method [ {0} ]",
269                                 new Object JavaDoc[] {methodDescriptor.getName()}));
270                     }
271                 }
272                 }
273                 // before you go on to the next method,
274
// did you resolve the last one okay?
275
if (!resolved) {
276 /*
277                                 // This if-stmt code is a workaround introduced by Harminder
278                                 // because currently methodDescriptor.getEjbClassSymbol() is
279                                 // returning NULL
280                                 //if (allMethods){
281                                 if (!wildCardWasPresent) {
282 */

283                 oneFailed = true;
284                 addErrorDetails(result, compName);
285                 result.addErrorDetails(smh.getLocalString
286                                (getClass().getName() + ".failed1",
287                             "Error: Transaction attributes must be specified for the methods defined in the home interface [ {0} ]. Method [ {1} ] has no transaction attribute defined within this bean [ {2} ].",
288                             new Object JavaDoc[] {home, methods[i].getName(),descriptor.getName()}));
289                 }
290 /*
291                              else {
292                                       result.addGoodDetails(smh.getLocalString
293                                                                    ("tests.componentNameConstructor",
294                                                                     "For [ {0} ]",
295                                                                     new Object[] {compName.toString()}));
296                       result.addGoodDetails(smh.getLocalString
297                         (getClass().getName() + ".passed",
298                              "Valid: [ {0} ] TransactionAttribute [ {1} ] for method [ {2} ] is valid. Transaction attributes must be specified for all methods except getEJBMetaData and getHomeHandle methods of home interface [ {3} ]",
299                          new Object[] {methods[i].getName(),"*","*", home}));
300 // }
301                               // End of workaround code. Note : this else also has to be removed once
302                               // the original bug of methodDesc.getEjbClassSymbol() is fixed
303
304                           }
305 */

306                 
307             } else {
308                 oneFailed = true;
309                 addErrorDetails(result, compName);
310                 result.addErrorDetails(smh.getLocalString
311                            (getClass().getName() + ".failed2",
312                             "Error: There are no method container transactions within this bean [ {0} ]. Transaction attributes must be specified for the methods defined in the home interface [ {1} ]. Method [ {2} ] has no transaction attribute defined.",
313                             new Object JavaDoc[] {descriptor.getName(),home, methods[i].getName()}));
314             }
315                       if(oneFailed == true && i==methods.length-1)
316                        return oneFailed;
317                    else
318                        continue;
319
320             } catch (Exception JavaDoc e) {
321             addErrorDetails(result, compName);
322             result.failed(smh.getLocalString
323                       (getClass().getName() + ".failedException1",
324                        "Error: Home interface [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
325                        new Object JavaDoc[] {home, e.getMessage(), descriptor.getName()}));
326             oneFailed = true;
327             return oneFailed;
328             }
329         } // if found relevant Home interface method
330
else {
331             oneFailed = true;
332             addErrorDetails(result, compName);
333             result.failed(smh.getLocalString
334                   (getClass().getName() + ".failedExcep",
335                    "Error: Method [ {0} ] should not be assigned a transaction attribute.",
336                    new Object JavaDoc[] {methods[i].getName()}));
337             
338         }
339         } // for all the methods within the home interface class, loop
340
return oneFailed;
341     } catch (ClassNotFoundException JavaDoc e) {
342         Verifier.debug(e);
343         addErrorDetails(result, compName);
344         result.failed(smh.getLocalString
345               (getClass().getName() + ".failedException2",
346                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
347                new Object JavaDoc[] {home, descriptor.getName()}));
348         oneFailed = true;
349         return oneFailed;
350     }
351     }
352  }
353
Popular Tags