KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.EjbCheck;
29 import com.sun.enterprise.tools.verifier.tests.ejb.MethodUtils;
30 import java.lang.ClassLoader JavaDoc;
31 import com.sun.enterprise.tools.verifier.tests.*;
32 import java.lang.reflect.*;
33 import com.sun.enterprise.deployment.MethodDescriptor;
34 import java.util.*;
35
36 /**
37  * Session Bean transaction demarcation type for all methods of component
38  * interface test.
39  * The transaction attributes must be specified for the methods defined
40  * in the bean's component interface and all the direct and indirect
41  * superinterfaces of the component interface, excluding the methods of
42  * the javax.ejb.EJBObject interface.
43  */

44 public class TransactionDemarcationComponentInterface extends EjbTest implements EjbCheck {
45     Result result = null;
46     ComponentNameConstructor compName = null;
47
48     static String JavaDoc[] EJBObjectMethods =
49     { "getEJBHome", "getHandle", "getPrimaryKey","getEJBLocalHome","isIdentical","remove"
50     };
51
52
53     /**
54      * Session Bean transaction demarcation type for all methods of component
55      * interface test.
56      * The transaction attributes must be specified for the methods defined
57      * in the bean's component interface and all the direct and indirect
58      * superinterfaces of the component interface, excluding the methods of
59      * the javax.ejb.EJBObject interface.
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 = getInitializedResult();
68     compName = getVerifierContext().getComponentNameConstructor();
69
70
71     // hack try/catch block around test, to exit gracefully instead of
72
// crashing verifier on getMethodDescriptors() call, XML mods cause
73
// java.lang.ClassNotFoundException: verifier.ejb.hello.BogusEJB
74
// Replacing <ejb-class>verifier.ejb.hello.HelloEJB with
75
// <ejb-class>verifier.ejb.hello.BogusEJB...
76
try {
77         // The transaction attributes must be specified for the methods defined
78
// in the bean's component interface and all the direct and indirect
79
// superinterfaces of the component interface, excluding the methods of
80
// the javax.ejb.EJBObject interface.
81
if (descriptor instanceof EjbSessionDescriptor) {
82                 String JavaDoc transactionType = descriptor.getTransactionType();
83                 if (EjbDescriptor.CONTAINER_TRANSACTION_TYPE.equals(transactionType)) {
84             boolean oneFailed = false;
85             if(descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName()))
86             oneFailed = commonToBothInterfaces(descriptor.getRemoteClassName(),(EjbSessionDescriptor)descriptor, MethodDescriptor.EJB_REMOTE);
87             if(oneFailed == false) {
88             if(descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName()))
89                 oneFailed = commonToBothInterfaces(descriptor.getLocalClassName(),(EjbSessionDescriptor)descriptor, MethodDescriptor.EJB_LOCAL);
90             }
91             /* // RFE 4953956 - test Web service endpoints separately.
92                     if ((oneFailed == false) && (implementsEndpoints(descriptor))) {
93                        result.addNaDetails(smh.getLocalString
94                                 ("tests.componentNameConstructor",
95                                  "For [ {0} ]",
96                                  new Object[] {compName.toString()}));
97                        result.notApplicable(smh.getLocalString
98                        ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.notapp",
99                        "Not Applicable because, EJB [ {0} ] implements a Service Endpoint Interface.",
100                        new Object[] {compName.toString()}));
101                        result.setStatus(result.NOT_APPLICABLE);
102                        return result;
103                     }
104             */

105             if (oneFailed) {
106             result.setStatus(result.FAILED);
107             } else {
108             result.setStatus(result.PASSED);
109             }
110             return result;
111   
112         } else {
113             // not container managed, but is a session bean
114
result.addNaDetails(smh.getLocalString
115                        ("tests.componentNameConstructor",
116                     "For [ {0} ]",
117                     new Object JavaDoc[] {compName.toString()}));
118             result.notApplicable(smh.getLocalString
119                      (getClass().getName() + ".notApplicable2",
120                       "Bean [ {0} ] is not [ {1} ] managed, it is [ {2} ] managed.",
121                       new Object JavaDoc[] {descriptor.getName(),EjbDescriptor.CONTAINER_TRANSACTION_TYPE,transactionType}));
122                     return result;
123         }
124         } else {
125         result.addNaDetails(smh.getLocalString
126                        ("tests.componentNameConstructor",
127                     "For [ {0} ]",
128                     new Object JavaDoc[] {compName.toString()}));
129         result.notApplicable(smh.getLocalString
130                      (getClass().getName() + ".notApplicable",
131                       "{0} expected {1} bean, but called with {2} bean.",
132                       new Object JavaDoc[] {getClass(),"Session","Entity"}));
133         return result;
134         }
135     } catch (Throwable JavaDoc t) {
136         result.addErrorDetails(smh.getLocalString
137                    ("tests.componentNameConstructor",
138                     "For [ {0} ]",
139                     new Object JavaDoc[] {compName.toString()}));
140         result.failed(smh.getLocalString
141               (getClass().getName() + ".failedException1",
142                "Error: Component interface does not contain class [ {0} ] within bean [ {1} ]",
143                new Object JavaDoc[] {t.getMessage(), descriptor.getName()}));
144         return result;
145     }
146
147     }
148
149     /**
150      * This method is responsible for the logic of the test. It is called for both local and component interfaces.
151      * @param descriptor the Enterprise Java Bean deployment descriptor
152      * @param component for the Remote/Local interface of the Ejb.
153      * @return boolean the results for this assertion i.e if a test has failed or not
154      */

155
156
157     private boolean commonToBothInterfaces(String JavaDoc component, EjbSessionDescriptor descriptor, String JavaDoc methodIntf) {
158     boolean oneFailed = false;
159     try {
160         Arrays.sort(EJBObjectMethods);
161         
162         // retrieve the component interface methods
163
Context context = getVerifierContext();
164         ClassLoader JavaDoc jcl = context.getClassLoader();
165         Class JavaDoc componentInterfaceClass = Class.forName(component, false, getVerifierContext().getClassLoader());
166         Method [] componentInterfaceMethods = componentInterfaceClass.getMethods();
167         
168         boolean lookForIt = false;
169         for (int i = 0; i < componentInterfaceMethods.length; i++) {
170         if (Arrays.binarySearch(EJBObjectMethods, componentInterfaceMethods[i].getName()) < 0) {
171             
172             try {
173             ContainerTransaction containerTransaction = null;
174             boolean resolved = false;
175 /*
176                         // This flag is a workaround introduced by Harminder
177                         // because currently methodDescriptor.getEjbClassSymbol() is
178                         // returning NULL
179                         //boolean allMethods = false;
180                         boolean wildCardWasPresent = false;
181 */

182                         
183             if (!descriptor.getMethodContainerTransactions().isEmpty()) {
184                 
185                 for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
186                 lookForIt = false;
187                                 
188                 MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
189
190     /*** Fixed the bug: 4883730. ejbClassSymbol is null when method-intf is not
191      * defined in the xml, since it is an optional field. Removed the earlier
192      * checks. A null method-intf indicates that the method is supposed to be
193      * in both Local & Home interfaces. ***/

194 /*
195                                 String methodIntf = null;
196                                 try {
197                                     methodIntf = methodDescriptor.getEjbClassSymbol();
198                                 } catch ( Exception ex ) {}
199                                 if ( methodIntf == null ) { //|| methodIntf.equals("")
200                                     //probably a wildcard was there
201                                     wildCardWasPresent = true;
202                                     continue;
203                                 }
204                                 //allMethods = true;
205                                 // end of workaround
206 */

207                                 
208                 // here we have to check that each method descriptor
209
// corresponds to a or some methods on the component interface
210
// according to the six styles
211
// style 1)
212
if (methodDescriptor.getName().equals(MethodDescriptor.ALL_EJB_METHODS)) {
213                     // if getEjbClassName() is Remote -> CARRY ON
214
// if Remote - PASS
215
if (methodDescriptor.getEjbClassSymbol() == null) {
216                         lookForIt = true;
217                     } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE)||
218                     methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCAL)) {
219                     lookForIt = true;
220                     // if empty String PASS
221
} else if (methodDescriptor.getEjbClassSymbol().equals("")) {
222                     lookForIt = true;
223                     } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)||
224                            methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) {
225                     lookForIt = false;
226                     // else (Bogus)
227
} else {
228                     // carry on & don't look for
229
// container transaction
230
lookForIt = false;
231                     }
232                     
233                     
234                 } else if (methodDescriptor.getParameterClassNames() == null) {
235                     
236                     
237                     // if (getEjbClassSybol() is Remote or is the empty String AND if componentInterfaceMethods[i].getName().equals(methodDescriptor.getName())
238
if (((methodDescriptor.getEjbClassSymbol() == null) ||
239                      methodDescriptor.getEjbClassSymbol().equals("") ||
240                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE) ||
241                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCAL)) &&
242                     (componentInterfaceMethods[i].getName().equals(methodDescriptor.getName()))) {
243                     // PASS
244
lookForIt = true;
245                     } else {
246                     // carry on
247
lookForIt = false;
248                     }
249                     
250                     
251                 } else {
252                     
253                     // if (getEjbClassSybol() is Remote or is the empty String AND if componentInterfaceMethods[i].getName().equals(methodDescriptor.getName()) AND
254
// the parameters of the method[i] are the same as the parameters of the method descriptor )
255

256                     if (((methodDescriptor.getEjbClassSymbol() == null) ||
257                      methodDescriptor.getEjbClassSymbol().equals("") ||
258                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE)||
259                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCAL)) &&
260                     (componentInterfaceMethods[i].getName().equals(methodDescriptor.getName())) &&
261                     (MethodUtils.stringArrayEquals(methodDescriptor.getParameterClassNames(), (new MethodDescriptor(componentInterfaceMethods[i], methodIntf)).getParameterClassNames()))) {
262                     // PASS
263
lookForIt = true;
264                     } else {
265                     // CARRY ON
266
lookForIt = false;
267                     }
268                     
269                 }
270                 
271                 if (lookForIt) {
272                     containerTransaction =
273                     (ContainerTransaction) descriptor.getMethodContainerTransactions().get(methodDescriptor);
274                     if (containerTransaction != null) {
275                     String JavaDoc transactionAttribute =
276                         containerTransaction.getTransactionAttribute();
277                     
278                     // danny is doing this in the DOL, but is it possible to not have
279
// any value for containerTransaction.getTransactionAttribute()
280
// in the DOL? if it is possible to have blank value for this,
281
// then this check is needed here, otherwise we are done and we
282
// don't need this check here
283
if (ContainerTransaction.NOT_SUPPORTED.equals(transactionAttribute)
284                         || ContainerTransaction.SUPPORTS.equals(transactionAttribute)
285                         || ContainerTransaction.REQUIRED.equals(transactionAttribute)
286                         || ContainerTransaction.REQUIRES_NEW.equals(transactionAttribute)
287                         || ContainerTransaction.MANDATORY.equals(transactionAttribute)
288                         || ContainerTransaction.NEVER.equals(transactionAttribute)
289                         || (!transactionAttribute.equals(""))) {
290                         addGoodDetails(result, compName);
291                         result.addGoodDetails(smh.getLocalString
292                                   (getClass().getName() + ".passed",
293                                    "Valid: TransactionAttribute [ {0} ] for method [ {1} ] is defined for component interface [ {2} ]",
294                                    new Object JavaDoc[] {transactionAttribute, componentInterfaceMethods[i].getName(),component}));
295                         resolved = true;
296                     } else {
297                         oneFailed = true;
298                         addErrorDetails(result, compName);
299                         result.addErrorDetails(smh.getLocalString
300                                    (getClass().getName() + ".failed",
301                                     "Error: TransactionAttribute [ {0} ] for method [ {1} ] is not valid. Transaction attributes must be defined for all methods of component interface [ {2} ].",
302                                     new Object JavaDoc[] {transactionAttribute, componentInterfaceMethods[i].getName(),component}));
303                     }
304                     } else {
305                     oneFailed = true;
306                     addErrorDetails(result, compName);
307                     result.addErrorDetails(smh.getLocalString
308                                    (getClass().getName() + ".failedException",
309                                 "Error: TransactionAttribute is null for method [ {0} ]",
310                                 new Object JavaDoc[] {methodDescriptor.getName()}));
311                     }
312                 }
313                 }
314                 // before you go on to the next method,
315
// did you resolve the last one okay?
316
if (!resolved) {
317 /*
318                                 // This if-stmt code is a workaround introduced by Harminder
319                                 // because currently methodDescriptor.getEjbClassSymbol() is
320                                 // returning NULL
321                                 //if (allMethods){
322                                 if (!wildCardWasPresent) {
323 */

324                                     oneFailed = true;
325                     addErrorDetails(result, compName);
326                     result.addErrorDetails(smh.getLocalString
327                                (getClass().getName() + ".failed1",
328                             "Error: Transaction attributes must be specified for the methods defined in the component interface [ {0} ]. Method [ {1} ] has no transaction attribute defined within this bean [ {2} ].",
329                             new Object JavaDoc[] {component, componentInterfaceMethods[i].getName(),descriptor.getName()}));
330 /*
331                                 }
332                                 else {
333                                              result.addGoodDetails(smh.getLocalString
334                                                                    ("tests.componentNameConstructor",
335                                                                     "For [ {0} ]",
336                                                                     new Object[] {compName.toString()}));
337                         result.addGoodDetails(smh.getLocalString
338                                   (getClass().getName() + ".passed",
339                                    "Valid: TransactionAttribute [ {0} ] for method [ {1} ] is defined for component interface [ {2} ]", new Object[] {"*", "*",component}));
340                               }
341                               // End of workaround code. Note : this else also has to be removed once
342                               // the original bug of methodDesc.getEjbClassSymbol() is fixed
343
344 */

345                 }
346             } else {
347                 oneFailed = true;
348                 addErrorDetails(result, compName);
349                 result.addErrorDetails(smh.getLocalString
350                            (getClass().getName() + ".failed2",
351                             "Error: There are no transaction attributes within this bean [ {0} ]. Transaction attributes must be specified for the methods defined in the component interface [ {1} ]. Method [ {2} ] has no transaction attribute defined.",
352                             new Object JavaDoc[] {descriptor.getName(),component, componentInterfaceMethods[i].getName()}));
353             }
354             if(oneFailed == true)
355                 return oneFailed;
356             } catch (Exception JavaDoc e) {
357             addErrorDetails(result, compName);
358             result.failed(smh.getLocalString
359                       (getClass().getName() + ".failedException1",
360                        "Error: Component interface [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
361                        new Object JavaDoc[] {component, e.getMessage(), descriptor.getName()}));
362             return oneFailed;
363             }
364         } // if you found a business method
365
else { // bug 6383704
366
if(componentInterfaceMethods[i].getName().equals("remove")) {
367                 for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
368                     MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
369                     if(methodDescriptor.getName().equals("remove")) {
370                         oneFailed = true;
371                         addErrorDetails(result, compName);
372                         result.failed(smh.getLocalString
373                             (getClass().getName() + ".failedExcep",
374                             "Error: Method [ {0} ] should not be assigned a transaction attribute.",
375                             new Object JavaDoc[] {methodDescriptor.getName()}));
376                         break;
377                     }
378                 }
379             }
380         }
381         } // for all component interface methods
382
return oneFailed;
383     } catch (ClassNotFoundException JavaDoc e) {
384         Verifier.debug(e);
385         addErrorDetails(result, compName);
386         result.failed(smh.getLocalString
387               (getClass().getName() + ".failedException2",
388                "Error: Component interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
389                new Object JavaDoc[] {component,descriptor.getName()}));
390         return oneFailed;
391     }
392     }
393 }
394
Popular Tags