KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > 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.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 com.sun.enterprise.tools.verifier.tests.*;
31 import java.lang.reflect.*;
32 import java.util.*;
33
34
35 /**
36  * Entity Bean transaction demarcation type for all methods of component
37  * interface test.
38  * The transaction attributes must be specified for the methods defined
39  * in the bean's component interface and all the direct and indirect
40  * superinterfaces of the component interface, excluding the "getEJBHome",
41  * "getHandle", "getPrimaryKey", "isIdentical" methods.
42  */

43  public class TransactionDemarcationComponentInterface extends EjbTest implements EjbCheck {
44
45     static String JavaDoc[] EJBObjectMethods =
46     { "getEJBHome", "getHandle", "getEJBLocalHome", "getPrimaryKey", "isIdentical"
47     };
48      Result result = null;
49      ComponentNameConstructor compName = null;
50
51
52     /**
53      * Entity Bean transaction demarcation type for all methods of component
54      * interface test.
55      * The transaction attributes must be specified for the methods defined
56      * in the bean's component interface and all the direct and indirect
57      * superinterfaces of the component interface, excluding the "getEJBHome",
58      * "getHandle", "getPrimaryKey", "isIdentical", "getEJBLocalHome" methods.
59      *
60      * @param descriptor the Enterprise Java Bean deployment descriptor
61      *
62      * @return <code>Result</code> the results for this assertion
63      */

64     public Result check(EjbDescriptor descriptor) {
65
66     result = getInitializedResult();
67     compName = getVerifierContext().getComponentNameConstructor();
68
69     // hack try/catch block around test, to exit gracefully instead of
70
// crashing verifier on getMethodDescriptors() call, XML mods cause
71
// java.lang.ClassNotFoundException: verifier.ejb.hello.BogusEJB
72
// Replacing <ejb-class>verifier.ejb.hello.HelloEJB with
73
// <ejb-class>verifier.ejb.hello.BogusEJB...
74
try {
75         // The transaction attributes must be specified for the methods defined
76
// in the bean's component interface and all the direct and indirect
77
// superinterfaces of the component interface, excluding the "getEJBHome",
78
// "getHandle", "getPrimaryKey", "isIdentical" methods of the
79
// javax.ejb.EJBObject interface.
80

81         if (descriptor instanceof EjbEntityDescriptor) {
82         boolean oneFailed = false;
83         if(descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName()))
84             oneFailed = commonToBothInterfaces(descriptor.getRemoteClassName(),descriptor, MethodDescriptor.EJB_REMOTE);
85         if(oneFailed == false) {
86             if(descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName()))
87             oneFailed = commonToBothInterfaces(descriptor.getLocalClassName(),descriptor, MethodDescriptor.EJB_LOCAL);
88         }
89         if (oneFailed) {
90             result.setStatus(Result.FAILED);
91         } else {
92             result.setStatus(Result.PASSED);
93         }
94         return result;
95         
96         } else {
97         addNaDetails(result, compName);
98         result.notApplicable(smh.getLocalString
99                      (getClass().getName() + ".notApplicable",
100                       "{0} expected {1} bean, but called with {2} bean.",
101                       new Object JavaDoc[] {getClass(),"Entity","Session"}));
102         return result;
103         }
104     } catch (Throwable JavaDoc t) {
105         addErrorDetails(result, compName);
106         result.failed(smh.getLocalString
107               (getClass().getName() + ".failedException1",
108                "Error: Component interface does not contain class [ {0} ] within bean [ {1} ]",
109                new Object JavaDoc[] { t.getMessage(), descriptor.getName()}));
110         return result;
111     }
112     }
113      
114   /**
115      * This method is responsible for the logic of the test. It is called for both local and component interfaces.
116      * @param descriptor the Enterprise Java Bean deployment descriptor
117      * @param component for the Remote/Local interface of the Ejb.
118      * @return boolean the results for this assertion i.e if a test has failed or not
119      */

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

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

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

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

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

287                                      oneFailed = true;
288                      addErrorDetails(result, compName);
289                      result.addErrorDetails(smh.getLocalString
290                             (getClass().getName() + ".failed1",
291                              "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} ].",
292                              new Object JavaDoc[] {component, componentInterfaceMethods[i].getName(),descriptor.getName()}));
293                                  }
294 /*
295                                  else {
296                          result.addGoodDetails(smh.getLocalString
297                                    ("tests.componentNameConstructor",
298                                     "For [ {0} ]",
299                                     new Object[] {compName.toString()}));
300                          result.addGoodDetails(smh.getLocalString
301                                    (getClass().getName() + ".passed",
302                                     "Valid: [ {0} ] TransactionAttribute [ {1} ] for method [ {2} ] is valid. Transaction attributes must be defined for all methods except getEJBHome, getHandle, getPrimaryKey, and isIdentical methods of component interface [ {3} ].",
303                                     new Object[] {componentInterfaceMethods[i].getName(), "*", "*" ,component}));
304 // }
305                               // End of workaround code. Note : this else also has to be removed once
306                               // the original bug of methodDesc.getEjbClassSymbol() is fixed
307                  }
308 */

309              } else {
310                  oneFailed = true;
311                  addErrorDetails(result, compName);
312                  result.addErrorDetails(smh.getLocalString
313                             (getClass().getName() + ".failed2",
314                              "Error: There are no method permissions 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.",
315                              new Object JavaDoc[] {descriptor.getName(),component, componentInterfaceMethods[i].getName()}));
316              }
317              if(oneFailed == true && i==componentInterfaceMethods.length-1)
318                             return oneFailed;
319                         else
320                            continue;
321                          } catch (Exception JavaDoc e) {
322              addErrorDetails(result, compName);
323              result.failed(smh.getLocalString
324                        (getClass().getName() + ".failedException1",
325                     "Error: Component interface [ {0} ] does not contain class [ {1} ] within bean [ {2} ]",
326                     new Object JavaDoc[] {component, e.getMessage(), descriptor.getName()}));
327              oneFailed = true;
328              return oneFailed;
329              }
330          } // if you found a business method
331
else {
332              oneFailed = true;
333              addErrorDetails(result, compName);
334              result.failed(smh.getLocalString
335                    (getClass().getName() + ".failedExcep",
336                     "Error: Method [ {0} ] should not be assigned a transaction attribute.",
337                     new Object JavaDoc[] {componentInterfaceMethods[i].getName()}));
338              
339          }
340          } // for all component interface methods
341
return oneFailed;
342      } catch (ClassNotFoundException JavaDoc e) {
343          Verifier.debug(e);
344          addErrorDetails(result, compName);
345          result.failed(smh.getLocalString
346                (getClass().getName() + ".failedException2",
347                 "Error: Component interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
348                 new Object JavaDoc[] {component,descriptor.getName()}));
349          oneFailed = true;
350          return oneFailed;
351      }
352      
353      }
354  }
355      
356
Popular Tags