KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.ClassLoader JavaDoc;
27 import java.util.*;
28 import java.lang.reflect.*;
29 import com.sun.enterprise.tools.verifier.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31
32 /**
33  * ContainerTransaction Style 3 - Each container transaction element consists
34  * of a list of one or more method elements, and the trans-attribute element.
35  * The container transaction element specifies that all the listed methods are
36  * assigned the specified transaction attribute value.
37  *
38  * Style 3:
39  * <method>
40  * <ejb-name> EJBNAME</ejb-name>
41  * <method-name>METHOD</method-name>
42  * <method-param> PARAMETER_1</method-param>
43  * ...
44  * <method-param> PARAMETER_N</method-param>
45  * </method>
46  * This style is used to refer to a single method within a set of methods with
47  * an overloaded name. The method must be one defined in the remote or home
48  * interface of the specified enterprise bean. If there is also a container
49  * transaction element that uses the Style 2 element for the method name, or
50  * the Style 1 element for the bean, the value specified by the Style 3 element
51  * takes precedence.
52  */

53 public class ContainerTransactionStyle3 extends EjbTest implements EjbCheck {
54     private Result result = null;
55     private ComponentNameConstructor compName = null;
56     private EjbDescriptor descriptor = null;
57     private Method[] homeMethods = null;
58     private Method[] localHomeMethods = null;
59     private Method[] remoteMethods = null;
60     private Method[] localMethods = null;
61     private Method[] serviceMethods = null;
62
63       /**
64      * ContainerTransaction Style 3 - Each container transaction element consists
65      * of a list of one or more method elements, and the trans-attribute element.
66      * The container transaction element specifies that all the listed methods are
67      * assigned the specified transaction attribute value.
68      *
69      * Style 3:
70      * <method>
71      * <ejb-name> EJBNAME</ejb-name>
72      * <method-name>METHOD</method-name>
73      * <method-param> PARAMETER_1</method-param>
74      * ...
75      * <method-param> PARAMETER_N</method-param>
76      * </method>
77      *
78      * This style is used to refer to a single method within a set of methods with
79      * an overloaded name. The method must be one defined in the remote or home
80      * interface of the specified enterprise bean. If there is also a container
81      * transaction element that uses the Style 2 element for the method name, or
82      * the Style 1 element for the bean, the value specified by the Style 3 element
83      * takes precedence.
84      *
85      * @param descriptor the Enterprise Java Bean deployment descriptor
86      *
87      * @return <code>Result</code> the results for this assertion
88      */

89       public Result check(EjbDescriptor descriptor) {
90           
91           result = getInitializedResult();
92           compName = getVerifierContext().getComponentNameConstructor();
93           this.descriptor = descriptor;
94           
95           if (!(descriptor instanceof EjbSessionDescriptor) &&
96                   !(descriptor instanceof EjbEntityDescriptor)) {
97               result.notApplicable(smh.getLocalString
98                                     ("com.sun.enterprise.tools.verifier.tests.ejb.homeintf.HomeMethodTest.notApplicable1",
99                                     "Test apply only to session or entity beans."));
100               return result;
101           }
102           
103           // The method must be one defined in the remote, home or business interface of
104
// the specified enterprise bean.
105
if (!descriptor.getMethodContainerTransactions().isEmpty())
106               commonToAllInterfaces();
107           
108           if(result.getStatus() != Result.FAILED) {
109               addGoodDetails(result, compName);
110               result.passed(smh.getLocalString
111                             (getClass().getName() + ".passed",
112                             "Valid container transaction methods."));
113           }
114           return result;
115       }
116
117     /**
118      * Iterate over all the bean methods and check if they are present in any
119      * of the bean's interfaces.
120      */

121     private void commonToAllInterfaces() {
122         
123         boolean isTimedObject=false;
124         try {
125             ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
126             Class JavaDoc beanClass = Class.forName(descriptor.getEjbClassName(), false, jcl);
127             isTimedObject = javax.ejb.TimedObject JavaDoc.class.isAssignableFrom(beanClass);
128         } catch (ClassNotFoundException JavaDoc e) {} //continue
129

130         initializeMethods();
131         for (Enumeration ee = descriptor.getMethodContainerTransactions().keys(); ee.hasMoreElements();) {
132             MethodDescriptor methodDescriptor = (MethodDescriptor) ee.nextElement();
133             
134             if (methodDescriptor.getName().equals(MethodDescriptor.ALL_EJB_METHODS) ||
135                     methodDescriptor.getParameterClassNames() == null) // style 1 and 2
136
continue;
137             
138             if(isTimedObject &&
139                     MethodDescriptor.EJB_BEAN.equals(methodDescriptor.getEjbClassSymbol()) &&
140                     methodDescriptor.getName().equals("ejbTimeout")) {
141                 String JavaDoc[] params=methodDescriptor.getJavaParameterClassNames();
142                 if(params.length==1 && params[0].trim().equals("javax.ejb.Timer"))
143                     continue;//found a match
144
}//if implements timer
145

146             Set<Method> methods = getAllInterfaceMethods(methodDescriptor);
147             
148             if(!isMethodContained(methods, methodDescriptor)) { // report failure
149
String JavaDoc ejbClassSymbol = methodDescriptor.getEjbClassSymbol();
150                 String JavaDoc intf = ejbClassSymbol;
151                 if(ejbClassSymbol == null) {
152                     intf = smh.getLocalString(getClass().getName() + ".msg", "any of bean");
153                 } else if(ejbClassSymbol.equals(MethodDescriptor.EJB_REMOTE)) {
154                     intf = "Remote or RemoteBusiness";
155                 } else if(ejbClassSymbol.equals(MethodDescriptor.EJB_LOCAL)) {
156                     intf = "Local or LocalBusiness";
157                 }
158
159                 addErrorDetails(result, compName);
160                 result.failed(smh.getLocalString
161                         (getClass().getName() + ".failed",
162                         "Error: Container Transaction method name [ {0} ] not " +
163                         "defined in [ {1} ] interface(s).",
164                         new Object JavaDoc[] {methodDescriptor.getName(), intf}));
165             }
166         }
167     }
168     
169     /** get methods from all of bean's interfaces*/
170     private void initializeMethods() {
171         homeMethods = getMethods(descriptor.getHomeClassName());
172         localHomeMethods = getMethods(descriptor.getLocalHomeClassName());
173         remoteMethods = getMethods(descriptor.getRemoteClassName());
174         remoteMethods = getBusinessMethods(descriptor.getRemoteBusinessClassNames(),remoteMethods);
175         localMethods = getMethods(descriptor.getLocalClassName());
176         localMethods = getBusinessMethods(descriptor.getLocalBusinessClassNames(), localMethods);
177         serviceMethods = getMethods(descriptor.getWebServiceEndpointInterfaceName());
178     }
179     
180     /** Collect all the methods the bean interfaces specified in methodDescriptor*/
181     private Set<Method> getAllInterfaceMethods(MethodDescriptor methodDescriptor) {
182         
183         Set<Method> methods = new HashSet<Method>();
184
185         String JavaDoc methodIntf = methodDescriptor.getEjbClassSymbol();
186         if((methodIntf == null)) {
187             methods.addAll(Arrays.asList(homeMethods));
188             methods.addAll(Arrays.asList(localHomeMethods));
189             methods.addAll(Arrays.asList(remoteMethods));
190             methods.addAll(Arrays.asList(localMethods));
191             methods.addAll(Arrays.asList(serviceMethods));
192         } else if(methodIntf.equals(MethodDescriptor.EJB_HOME)) {
193             methods.addAll(Arrays.asList(homeMethods));
194         } else if(methodIntf.equals(MethodDescriptor.EJB_LOCALHOME)) {
195             methods.addAll(Arrays.asList(localHomeMethods));
196         } else if(methodIntf.equals(MethodDescriptor.EJB_REMOTE)) {
197             methods.addAll(Arrays.asList(remoteMethods));
198         } else if(methodIntf.equals(MethodDescriptor.EJB_LOCAL)) {
199             methods.addAll(Arrays.asList(localMethods));
200         } else if(methodIntf.equals(MethodDescriptor.EJB_WEB_SERVICE)) {
201             methods.addAll(Arrays.asList(serviceMethods));
202         }
203         return methods;
204     }
205     
206     /** get all the methods defined in the given class clsName*/
207     private Method[] getMethods(String JavaDoc clsName) {
208         if(clsName==null)
209             return new Method[]{};
210         ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
211         try {
212             Class JavaDoc cls = Class.forName(clsName, false, jcl);
213             return cls.getMethods();
214         } catch (ClassNotFoundException JavaDoc e) {} // ignore and continue
215
return new Method[]{};
216     }
217     /** returns an array of business methods that are collected from set classNames*/
218     private Method[] getBusinessMethods(Set<String JavaDoc> classNames, Method[] intfMethods) {
219         if(!classNames.isEmpty()) {
220             
221             List<Method> methods = new ArrayList<Method>();
222             for (String JavaDoc clsName : classNames) {
223                 Method[] methodArray = getMethods(clsName);
224                 if(methodArray != null)
225                     methods.addAll(Arrays.asList(methodArray));
226             }
227             
228             if(intfMethods != null)
229                 methods.addAll(Arrays.asList(intfMethods));
230             return methods.toArray(new Method[]{});
231         }
232         return intfMethods;
233     }
234     
235     /** returns true if methodDescriptor is contained in the set methods */
236     private boolean isMethodContained(Set<Method> methods, MethodDescriptor methodDescriptor) {
237         boolean foundIt = false;
238         for (Method method : methods) {
239             if(method.getName().equals(methodDescriptor.getName()) &&
240                     MethodUtils.stringArrayEquals(methodDescriptor.getParameterClassNames(),
241                             (new MethodDescriptor(method)).getParameterClassNames())) {
242                 foundIt = true;
243                 break;
244             }
245         }
246         return foundIt;
247     }
248 }
249
Popular Tags