KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > 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.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.util.*;
33 import java.lang.reflect.*;
34
35 /**
36  * Session Bean transaction demarcation type home interface methods test.
37  * Transaction attributes must not be specified for the methods of a session
38  * bean's home interface.
39  */

40 public class TransactionDemarcationHomeInterface extends EjbTest implements EjbCheck {
41     Result result = null;
42     ComponentNameConstructor compName = null;
43
44     /**
45      * Session Bean transaction demarcation type home interface methods test.
46      * Transaction attributes must not be specified for the methods of a session
47      * bean's home interface.
48      *
49      * @param descriptor the Enterprise Java Bean deployment descriptor
50      *
51      * @return <code>Result</code> the results for this assertion
52      */

53     public Result check(EjbDescriptor descriptor) {
54
55     result = getInitializedResult();
56     compName = getVerifierContext().getComponentNameConstructor();
57
58     // hack try/catch block around test, to exit gracefully instead of
59
// crashing verifier on getMethodDescriptors() call, XML mods cause
60
// java.lang.ClassNotFoundException: verifier.ejb.hello.BogusEJB
61
// Replacing <ejb-class>verifier.ejb.hello.HelloEJB with
62
// <ejb-class>verifier.ejb.hello.BogusEJB...
63
try {
64         // Transaction attributes must not be specified for the methods of a session
65
// bean's home interface.
66
if (descriptor instanceof EjbSessionDescriptor) {
67         String JavaDoc transactionType = descriptor.getTransactionType();
68         Context context = getVerifierContext();
69         ClassLoader JavaDoc jcl = context.getClassLoader();
70                 if (EjbDescriptor.CONTAINER_TRANSACTION_TYPE.equals(transactionType)) {
71             boolean oneFailed = false;
72             if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
73             Class JavaDoc superclass = Class.forName(descriptor.getHomeClassName(), false, getVerifierContext().getClassLoader());
74             do {
75                 oneFailed = commonToBothInterfaces(superclass.getName(),(EjbSessionDescriptor)descriptor, MethodDescriptor.EJB_HOME);
76                 if (oneFailed == true) {
77                 break;
78                 }
79                 superclass = superclass.getSuperclass();
80             } while (superclass != null);
81             }
82             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
83             Class JavaDoc superclass = Class.forName(descriptor.getLocalHomeClassName(), false, getVerifierContext().getClassLoader());
84             do {
85                 oneFailed = commonToBothInterfaces(superclass.getName(),(EjbSessionDescriptor)descriptor, MethodDescriptor.EJB_LOCALHOME);
86                 if (oneFailed == true) {
87                 break;
88                 }
89                 superclass = superclass.getSuperclass();
90             } while (superclass != null);
91             }
92
93                     if ((oneFailed == false) && (implementsEndpoints(descriptor))) {
94                        result.addNaDetails(smh.getLocalString
95                                 ("tests.componentNameConstructor",
96                                  "For [ {0} ]",
97                                  new Object JavaDoc[] {compName.toString()}));
98                        result.notApplicable(smh.getLocalString
99                        ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.notapp",
100                        "Not Applicable because, EJB [ {0} ] implements a Service Endpoint Interface.",
101                        new Object JavaDoc[] {compName.toString()}));
102                        result.setStatus(Result.NOT_APPLICABLE);
103                        return result;
104                     }
105                     
106             if (oneFailed) {
107             result.setStatus(Result.FAILED);
108             } else {
109             result.setStatus(Result.PASSED);
110             }
111   
112             return result;
113                 } else {
114                     // not container managed, but is a session bean
115
addNaDetails(result, compName);
116                     result.notApplicable(smh.getLocalString
117                                          (getClass().getName() + ".notApplicable2",
118                                           "Bean [ {0} ] is not [ {1} ] managed, it is [ {2} ] managed.",
119                                           new Object JavaDoc[] {descriptor.getName(),EjbDescriptor.CONTAINER_TRANSACTION_TYPE,transactionType}));
120                     return result;
121                 }
122
123         } else {
124         addNaDetails(result, compName);
125         result.notApplicable(smh.getLocalString
126                      (getClass().getName() + ".notApplicable",
127                       "{0} expected {1} bean, but called with {2} bean.",
128                       new Object JavaDoc[] {getClass(),"Session","Entity"}));
129         return result;
130         }
131
132     } catch (Throwable JavaDoc t) {
133         addErrorDetails(result, compName);
134         result.failed(smh.getLocalString
135               (getClass().getName() + ".failedException",
136                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ].",
137                new Object JavaDoc[] {t.getMessage(), descriptor.getName()}));
138         return result;
139     }
140
141     }
142
143     /**
144      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
145      * @param descriptor the Enterprise Java Bean deployment descriptor
146      * @param home for the Home interface of the Ejb.
147      * @param methodIntf for the interface type
148      * @return boolean the results for this assertion i.e if a test has failed or not
149      */

150
151
152     private boolean commonToBothInterfaces(String JavaDoc home, EjbSessionDescriptor descriptor, String JavaDoc methodIntf) {
153     boolean oneFailed = false;
154     try {
155         Context context = getVerifierContext();
156         ClassLoader JavaDoc jcl = context.getClassLoader();
157         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
158         Method methods[] = c.getDeclaredMethods();
159         boolean lookForIt = false;
160         for (int i=0; i< methods.length; i++) {
161         try {
162             ContainerTransaction containerTransaction = null;
163             boolean resolved = true;
164 /*
165                      // This flag is a workaround introduced by Harminder
166                      // because currently methodDescriptor.getEjbClassSymbol() is
167                      // returning NULL
168                      //boolean allMethods = false;
169                      boolean wildCardWasPresent = false;
170 */

171
172             if (!descriptor.getMethodContainerTransactions().isEmpty()) {
173             for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
174                 lookForIt = false;
175                 MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
176
177     /*** Fixed the bug: 4883730. ejbClassSymbol is null when method-intf is not
178      * defined in the xml, since it is an optional field. Removed the earlier
179      * checks. A null method-intf indicates that the method is supposed to be
180      * in both Local & Home interfaces. ***/

181 /*
182                             String methodIntf = null;
183                             try {
184                                 methodIntf = methodDescriptor.getEjbClassSymbol();
185                             } catch ( Exception ex ) {}
186                             if ( methodIntf == null ) { //|| methodIntf.equals("")
187                                 //probably a wildcard was there
188                                 wildCardWasPresent = true;
189                                 continue;
190                             }
191                             //allMethods = true;
192                             // end of workaround
193 */

194
195                             
196                 // here we have to check that each method descriptor
197
// corresponds to a or some methods on the home interface
198
// according to the six styles
199
// style 1)
200
if (methodDescriptor.getName().equals(MethodDescriptor.ALL_EJB_METHODS)) {
201                 // if getEjbClassName() is Remote -> CARRY ON
202
// if Home - PASS
203
if (methodDescriptor.getEjbClassSymbol() == null) {
204                     lookForIt = true;
205                 } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)) {
206                     lookForIt = true;
207                     // if empty String PASS
208
} else if (methodDescriptor.getEjbClassSymbol().equals("")) {
209                     lookForIt = true;
210                 } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE)) {
211                     lookForIt = false;
212                     // else (Bogus)
213
} else {
214                     // carry on & don't look for
215
// container transaction
216
lookForIt = false;
217                 }
218                 } else if (methodDescriptor.getParameterClassNames() == null) {
219                 // if (getEjbClassSybol() is Home or is the empty String AND if methods[i].getName().equals(methodDescriptor.getName())
220
if (((methodDescriptor.getEjbClassSymbol() == null) ||
221                      methodDescriptor.getEjbClassSymbol().equals("") ||
222                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)||
223                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) &&
224                     (methods[i].getName().equals(methodDescriptor.getName()))) {
225                     // PASS
226
lookForIt = true;
227                 } else {
228                     // carry on
229
lookForIt = false;
230                 }
231                 } else {
232                 // if (getEjbClassSybol() is Home or is the empty String AND if methods[i].getName().equals(methodDescriptor.getName()) AND
233
// the parameters of the method[i] are the same as the parameters of the method descriptor )
234
if (((methodDescriptor.getEjbClassSymbol() == null) ||
235                      methodDescriptor.getEjbClassSymbol().equals("") ||
236                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)||
237                      methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) &&
238                     (methods[i].getName().equals(methodDescriptor.getName())) &&
239                     (MethodUtils.stringArrayEquals(methodDescriptor.getParameterClassNames(), (new MethodDescriptor(methods[i], methodIntf)).getParameterClassNames()))) {
240                     // PASS
241
lookForIt = true;
242                 } else {
243                     // CARRY ON
244
lookForIt = false;
245                 }
246                 }
247                 
248                 if (lookForIt) {
249                 containerTransaction =
250                     (ContainerTransaction) descriptor.getMethodContainerTransactions().get(methodDescriptor);
251                 
252                 if (containerTransaction != null) {
253                     String JavaDoc transactionAttribute =
254                     containerTransaction.getTransactionAttribute();
255                     
256                     // danny is doing this in the DOL, but is it possible to not have
257
// any value for containerTransaction.getTransactionAttribute()
258
// in the DOL? if it is possible to have blank value for this,
259
// then this check is needed here, otherwise we are done and we
260
// don't need this check here
261
if (ContainerTransaction.NOT_SUPPORTED.equals(transactionAttribute)
262                     || ContainerTransaction.SUPPORTS.equals(transactionAttribute)
263                     || ContainerTransaction.REQUIRED.equals(transactionAttribute)
264                     || ContainerTransaction.REQUIRES_NEW.equals(transactionAttribute)
265                     || ContainerTransaction.MANDATORY.equals(transactionAttribute)
266                     || ContainerTransaction.NEVER.equals(transactionAttribute)
267                     || (!transactionAttribute.equals(""))) {
268                     // if "*" ignore, test N/A
269
if (!methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)&&
270                         !methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) {
271                         result.addGoodDetails(smh.getLocalString
272                                   (getClass().getName() + ".passed",
273                                    "Valid: TransactionAttribute [ {0} ] for method [ {1} ] is not defined for home interface [ {2} ]",
274                                    new Object JavaDoc[] {transactionAttribute, methods[i].getName(),home}));
275                     } else {
276                         oneFailed = true;
277                         addErrorDetails(result, compName);
278                         result.addErrorDetails(smh.getLocalString
279                                    (getClass().getName() + ".failed",
280                                     "Error: TransactionAttribute [ {0} ] for method [ {1} ] is not valid. Transaction attributes must not be specified for all methods of session bean home interface [ {2} ].",
281                                     new Object JavaDoc[] {transactionAttribute, methods[i].getName(),home}));
282                         resolved = false;
283                     }
284                     } else {
285                     addGoodDetails(result, compName);
286                     result.addGoodDetails(smh.getLocalString
287                                   (getClass().getName() + ".passed",
288                                    "Valid: TransactionAttribute [ {0} ] for method [ {1} ] is not defined for home interface [ {2} ]",
289                                    new Object JavaDoc[] {transactionAttribute, methods[i].getName(),home}));
290                     }
291                 } else {
292                     addGoodDetails(result, compName);
293                     result.addGoodDetails(smh.getLocalString
294                               (getClass().getName() + ".passedException",
295                                "Valid: TransactionAttribute is null for method [ {0} ]",
296                                new Object JavaDoc[] {methodDescriptor.getName()}));
297                 }
298                 }
299             }
300             // before you go on to the next method,
301
// did you resolve the last one okay?
302
if (!resolved) {
303 /*
304                             // This if-stmt code is a workaround introduced by Harminder
305                             // because currently methodDescriptor.getEjbClassSymbol() is
306                             // returning NULL
307                             //if (allMethods){
308                             if (!wildCardWasPresent) {
309
310 */

311                 oneFailed = true;
312                 addErrorDetails(result, compName);
313                 result.addErrorDetails(smh.getLocalString
314                            (getClass().getName() + ".failed1",
315                             "Error: Transaction attributes must not be specified for the methods defined in the session bean home interface [ {0} ]. Method [ {1} ] has transaction attribute defined within this bean [ {2} ].",
316                             new Object JavaDoc[] {home, methods[i].getName(), descriptor.getName()}));
317 /*
318                             }
319                             else {
320                             
321                               result.addGoodDetails(smh.getLocalString
322                                                                    ("tests.componentNameConstructor",
323                                                                     "For [ {0} ]",
324                                                                     new Object[] {compName.toString()}));
325                               result.addGoodDetails(smh.getLocalString
326                                                                   (getClass().getName() + ".passed",
327                                                                    "Valid: TransactionAttribute [ {0} ] for method [ {1} ] is defined for remote interface [ {2} ]", new Object[] {"*", "*",home}));
328
329
330                             }
331                             // End of workaround code. Note : this else also has to be removed once
332                             // the original bug of methodDesc.getEjbClassSymbol() is fixed
333 */

334
335             } else {
336                 addGoodDetails(result, compName);
337                 result.addGoodDetails(smh.getLocalString
338                           (getClass().getName() + ".passed2",
339                            "Valid: Transaction attributes must not be specified for the methods defined in the session bean home interface [ {0} ]. Method [ {1} ] has no transaction attribute defined within this bean [ {2} ].",
340                            new Object JavaDoc[] {home, methods[i].getName(), descriptor.getName()}));
341             }
342             } else {
343             addGoodDetails(result, compName);
344             result.passed(smh.getLocalString
345                       (getClass().getName() + ".passed1",
346                        "Valid: There are no method permissions within this bean [ {0} ]. Transaction attributes must not be specified for the methods defined in the session bean home interface [ {1} ]. Method [ {2} ] has no transaction attribute defined.",
347                        new Object JavaDoc[] {descriptor.getName(),home, methods[i].getName()}));
348             }
349             if (oneFailed == true)
350             return oneFailed;
351         } catch (Exception JavaDoc e) {
352             addErrorDetails(result, compName);
353             result.failed(smh.getLocalString
354                   (getClass().getName() + ".failedException1",
355                    "Error: Home interface [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
356                    new Object JavaDoc[] {home, e.getMessage(), descriptor.getName()}));
357             return oneFailed;
358         }
359         } // for all the methods within the home interface class, loop
360
return oneFailed;
361     } catch (ClassNotFoundException JavaDoc e) {
362         Verifier.debug(e);
363         addErrorDetails(result, compName);
364         result.failed(smh.getLocalString
365               (getClass().getName() + ".failedException",
366                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
367                new Object JavaDoc[] {home, descriptor.getName()}));
368         return oneFailed;
369     }
370     
371     }
372 }
373
Popular Tags