KickJava   Java API By Example, From Geeks To Geeks.

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


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.elements;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
27 import com.sun.enterprise.tools.verifier.Result;
28 import com.sun.enterprise.tools.verifier.Verifier;
29 import com.sun.enterprise.deployment.MethodDescriptor;
30 import com.sun.enterprise.deployment.EjbDescriptor;
31
32 import java.lang.reflect.Method JavaDoc;
33 import java.util.*;
34
35 /**
36  * Base class for all the MethodsExist tests.
37  * ContainerTransactionMethodExists, ExcludeListMethodsExist,
38  * MethodPermissionMethodExists and UncheckedMethodsExist.
39  *
40  * @author Vikas Awasthi
41  */

42 public abstract class MethodsExist extends EjbTest {
43     
44     protected Result result = null;
45     protected ComponentNameConstructor compName = null;
46     private List<Method JavaDoc> methods = null;
47     
48     /** test for method styles 1, 2 or 3 */
49     protected void checkMethodStyles(MethodDescriptor methodDescriptor,
50                                      EjbDescriptor descriptor) {
51
52         if (methodDescriptor.getName().equals(MethodDescriptor.ALL_EJB_METHODS))
53             return; //style 1
54

55         if (methodDescriptor.getParameterClassNames() == null) //style 2
56
checkStyle(methodDescriptor, descriptor, true);
57         else // style 3
58
checkStyle(methodDescriptor, descriptor, false);
59     }
60     
61     private void checkStyle(MethodDescriptor methodDescriptor,
62                             EjbDescriptor descriptor,
63                             boolean isCheckStyle2) {
64         
65         String JavaDoc methodName = methodDescriptor.getName();
66             
67         if (methodDescriptor.getEjbClassSymbol() != null) { // method intf present
68

69             if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_REMOTE)) {
70                 // if method-intf is Remote then add EJB3.0 remote business interfaces
71
Set<String JavaDoc> interfaces= new HashSet<String JavaDoc>(descriptor.getRemoteBusinessClassNames());
72                 if(descriptor.getRemoteClassName()!=null)
73                     interfaces.add(descriptor.getRemoteClassName());
74                 
75                 if(!contains(methodDescriptor, getAllMethods(interfaces), isCheckStyle2))
76                     logFailure(methodName, "remote");
77                 
78             } else if(methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCAL)) {
79                 // if method-intf is Local then add EJB3.0 local business interfaces
80
Set<String JavaDoc> interfaces= new HashSet<String JavaDoc>(descriptor.getLocalBusinessClassNames());
81                 if(descriptor.getLocalClassName()!=null)
82                     interfaces.add(descriptor.getLocalClassName());
83                 
84                 if(!contains(methodDescriptor, getAllMethods(interfaces), isCheckStyle2))
85                     logFailure(methodName, "local");
86                 
87             } else if (methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_HOME)) {
88                 
89                 if(!contains(methodDescriptor, getAllMethods(descriptor.getHomeClassName()), isCheckStyle2))
90                     logFailure(methodName, "home");
91                 
92             } else if(methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_LOCALHOME)) {
93                 
94                 if(!contains(methodDescriptor, getAllMethods(descriptor.getLocalHomeClassName()), isCheckStyle2))
95                     logFailure(methodName, "localhome");
96                 
97             } else if(methodDescriptor.getEjbClassSymbol().equals(MethodDescriptor.EJB_WEB_SERVICE)) {
98
99                 String JavaDoc endpointIntfName = descriptor.getWebServiceEndpointInterfaceName();
100                 if(!contains(methodDescriptor, getAllMethods(endpointIntfName), isCheckStyle2))
101                     logFailure(methodName, "localhome");
102                 
103             }
104             // for ejbTimeout method of TimedObject the methodDescriptor.getEjbClassSymbol()
105
// is always EJB_BEAN. So no need to check that as this test is not
106
// applicable for this method.
107
} else { // method intf not present
108
if(!contains(methodDescriptor, getAllMethods(descriptor), true))
109                 logFailure(methodDescriptor.getName(), "any of component or home");
110         }
111     }
112     
113     /**
114      * checks if method1 is present in the given list of methods.
115      * For style 2 methods only method names are compared and for style 3
116      * method parameters are also compared.
117      */

118     private boolean contains(MethodDescriptor method1, List<Method JavaDoc> methods, boolean isStyle2) {
119         for (Method JavaDoc method : methods) {
120             if(isStyle2) {// for style 2 do only name comparison
121
if(method.getName().equals(method1.getName()))
122                     return true;
123             } else if (method.getName().equals(method1.getName()) &&
124                     Arrays.equals(new MethodDescriptor().getParameterClassNamesFor(method),
125                             method1.getParameterClassNames()))
126                 return true;
127         }
128         return false;
129     }
130     
131     /**
132      * It returns a list of all the methods in component and home interfaces
133      * of this ejbDescriptor.
134      */

135     private List<Method JavaDoc> getAllMethods(EjbDescriptor descriptor) {
136         if(methods!=null)
137             return methods;
138         
139         methods = new ArrayList<Method JavaDoc>();
140         Set<String JavaDoc> interfaces = descriptor.getLocalBusinessClassNames();
141         
142         interfaces.addAll(descriptor.getRemoteBusinessClassNames());
143         if(descriptor.getRemoteClassName()!=null)
144             interfaces.add(descriptor.getRemoteClassName());
145         if(descriptor.getLocalClassName()!=null)
146             interfaces.add(descriptor.getLocalClassName());
147         if(descriptor.getHomeClassName()!=null)
148             interfaces.add(descriptor.getHomeClassName());
149         if(descriptor.getLocalHomeClassName()!=null)
150             interfaces.add(descriptor.getLocalHomeClassName());
151         if(descriptor.getWebServiceEndpointInterfaceName()!=null)
152             interfaces.add(descriptor.getWebServiceEndpointInterfaceName());
153         
154         for (String JavaDoc intf : interfaces) {
155             Class JavaDoc intfClass = loadClass(intf);
156             if(intfClass==null)//ignore if null. Error message is already logged
157
continue;
158             methods.addAll(Arrays.asList(intfClass.getMethods()));
159         }
160         return methods;
161     }
162     
163     /** It returns a list of all the methods in the given interface intf */
164     private List<Method JavaDoc> getAllMethods(String JavaDoc intf) {
165         Class JavaDoc intfClass = loadClass(intf);
166         return (intfClass==null)? new ArrayList<Method JavaDoc>():Arrays.asList(intfClass.getMethods());
167     }
168     
169     /** It returns a list of all the methods in the given interfaces */
170     private List<Method JavaDoc> getAllMethods(Set<String JavaDoc> interfaces) {
171         List<Method JavaDoc> methods = new ArrayList<Method JavaDoc>();
172         for (String JavaDoc intf : interfaces) {
173             Class JavaDoc intfClass = loadClass(intf);
174             if(intfClass==null)
175                 continue;
176             methods.addAll(Arrays.asList(intfClass.getMethods()));
177         }
178         return methods;
179     }
180     
181     private Class JavaDoc loadClass(String JavaDoc className) {
182         Class JavaDoc intfClass = null;
183         try {
184             intfClass = Class.forName(className,
185                                       false,
186                                       getVerifierContext().getClassLoader());
187         } catch (ClassNotFoundException JavaDoc e) {
188             Verifier.debug(e);
189             addErrorDetails(result, compName);
190             result.failed(smh.getLocalString
191                     (getClass().getName() + ".failedException",
192                     "Error: Interface class not found. [ {0} ]",
193                     new Object JavaDoc[] {className}));
194         }
195         return intfClass;
196     }
197     
198     private void logFailure(String JavaDoc msg1, String JavaDoc msg2) {
199         addErrorDetails(result, compName);
200         result.failed(smh.getLocalString
201                 (getClass().getName() + ".failed",
202                 "Error: Method name [ {0} ] not defined in {1} interface.",
203                 new Object JavaDoc[] {msg1, msg2}));
204     }
205     
206 }
207
Popular Tags