KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > connector > ConnectorTest


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 /*
24  * ConnectorTest.java
25  *
26  * Created on September 20, 2000, 11:10 AM
27  */

28
29 package com.sun.enterprise.tools.verifier.tests.connector;
30
31 import com.sun.enterprise.deployment.ConnectorDescriptor;
32 import com.sun.enterprise.deployment.Descriptor;
33 import com.sun.enterprise.deployment.util.ModuleDescriptor;
34 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
35 import com.sun.enterprise.tools.verifier.Result;
36 import com.sun.enterprise.tools.verifier.Verifier;
37 import com.sun.enterprise.tools.verifier.tests.VerifierCheck;
38 import com.sun.enterprise.tools.verifier.tests.VerifierTest;
39 import com.sun.enterprise.util.io.FileUtils;
40
41 import java.io.File JavaDoc;
42 import java.lang.reflect.Method JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.jar.JarInputStream JavaDoc;
45 import java.util.zip.ZipEntry JavaDoc;
46 /**
47  * Superclass for all connector tests, contains common services.
48  *
49  * @author Jerome Dochez
50  * @version
51  */

52 public abstract class ConnectorTest extends VerifierTest implements VerifierCheck, ConnectorCheck
53 {
54         
55     /**
56      * <p>
57      * run an individual test against the deployment descriptor for the
58      * archive the verifier is performing compliance tests against.
59      * </p>
60      *
61      * @param descriptor deployment descriptor for the archive
62      * @return result object containing the result of the individual test
63      * performed
64      */

65     public Result check(Descriptor descriptor) {
66         return check((ConnectorDescriptor) descriptor);
67     }
68    
69     /**
70      * <p>
71      * all connector tests should implement this method. it run an individual
72      * test against the resource adapter deployment descriptor.
73      * </p>
74      *
75      * @param descriptor deployment descriptor for the rar file
76      * @return result object containing the result of the individual test
77      * performed
78      */

79     public abstract Result check(ConnectorDescriptor descriptor);
80     
81     /**
82      * <p>
83      * Find a class implementating the interface in the jar files contained
84      * in the connector rar file.
85      * </p>
86      * @param interfaceName the interface the class should implement
87      * @return class implementing the interface or null if not present in the
88      * jar files
89      */

90     protected Class JavaDoc findImplementorOf(ConnectorDescriptor desc, String JavaDoc interfaceName) {
91         /**
92         * This is a little bit hectic but we have to go through all the
93         * jar files included in the rar file and load all classes implemented
94         * in these jar files. For each class, we should look if the class
95         * implements the requested interface "interfaceName"
96         */

97         
98         // let's get the rar file
99
try {
100             String JavaDoc uri=getAbstractArchiveUri(desc);
101             FileArchive arch = new FileArchive();
102             arch.open(uri);
103             for(Enumeration JavaDoc en = arch.entries();en.hasMoreElements();) {
104                 String JavaDoc entry = (String JavaDoc)en.nextElement();
105                 if (entry.endsWith(".jar")) {
106                     // we found a jar file, let's load it
107
JarInputStream JavaDoc jis = new JarInputStream JavaDoc(arch.getEntry(entry));
108                     try {
109                         // Now we are going to iterate over the element of the jar file
110
ZipEntry JavaDoc ze = jis.getNextEntry();
111                         while(ze!=null) {
112                             String JavaDoc elementName = (String JavaDoc) ze.getName();
113                             // Is this jar entry a java class file ?
114
if (elementName.endsWith(".class")) {
115                                 // we found a .class file let's load it and see if it does implement the interface
116
String JavaDoc className = elementName.substring(0, elementName.length()-".class".length()).replace('/','.');
117                                 //try {
118
ClassLoader JavaDoc jcl = getVerifierContext().getRarClassLoader();
119                                 Class JavaDoc c = Class.forName(className, false, jcl);
120
121                                 if (isImplementorOf(c, interfaceName))
122                                     if(c.getSuperclass() != null)
123                                         return c;
124
125                             }
126                             ze = jis.getNextEntry();
127                         }
128                     } catch(ClassNotFoundException JavaDoc cnfe) {
129                         // We ignore this for now
130
} catch(NoClassDefFoundError JavaDoc cdnf) {
131                         //continue to search other classes
132
} finally {
133                         try {
134                             if(jis != null)
135                                 jis.close();
136                         } catch(Exception JavaDoc e) {}
137                     }
138                 }
139             }
140         } catch(java.io.IOException JavaDoc ioe) {
141             Verifier.debug(ioe);
142         }
143         return null;
144     }
145     
146     /**
147      * <p>
148      * Check that a class overrides some methods defined in the java.lang.Object class
149      * </p>
150      *
151      * @param clazz the implementation class
152      * @param methodName the method name
153      * @param parmTypes the method parameter types
154      * @param methodSignature a human readable signature description of the method
155      * @param result where to put the method lookup result
156      * @return true if the method is overriden by the implementation class
157      */

158     protected boolean checkMethodImpl(Class JavaDoc clazz, String JavaDoc methodName, Class JavaDoc[] parmTypes,
159         String JavaDoc methodSignature, Result result) {
160         
161         Method JavaDoc m=null;
162         Class JavaDoc c = clazz;
163         
164         do {
165             try {
166                 m = c.getDeclaredMethod(methodName, parmTypes);
167             } catch(NoSuchMethodException JavaDoc nsme) {
168             } catch(SecurityException JavaDoc se) {
169             }
170             c = c.getSuperclass();
171         } while (m != null && c!=null && c != Object JavaDoc.class);
172
173         if (m==null) {
174         result.failed(smh.getLocalString
175             ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.MethodOverride.failed",
176                 "Warning: The class [ {0} ] does not override the method [ {1} ]",
177                 new Object JavaDoc[] {clazz.getName(), methodSignature }));
178             return false;
179         } else {
180         result.passed(smh.getLocalString
181             ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.MethodOverride.passed",
182                 "The class [ {0} ] overrides the method [ {1} ]",
183                 new Object JavaDoc[] {clazz.getName(), methodSignature }));
184             return true;
185         }
186     }
187     
188     /**
189      * <p>
190      * Look for an implementation of an interface in all the classes present
191      * in a jar file, setting the result object with the look up result
192      * </p>
193      *
194      * @param interfaceName interface to look for an implementor
195      * @param result where to put the look up result
196      */

197     protected boolean findImplementorOf(ConnectorDescriptor desc, String JavaDoc interfaceName, Result result)
198     {
199         Class JavaDoc c = findImplementorOf(desc, interfaceName);
200         if (c != null) {
201         result.passed(smh.getLocalString
202             ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.findImplementor.passed",
203                 "The class [ {0} ] implements the [ {1} ] interface",
204                 new Object JavaDoc[] {c.getName(), interfaceName}));
205             return true;
206         } else {
207         result.failed(smh.getLocalString
208             ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.findImplementor.failed",
209                 "Error: There is no implementation of the [ {0} ] provided",
210                 new Object JavaDoc[] {interfaceName}));
211             return false;
212         }
213     }
214     
215     /**
216      * <p>
217      * Check if a class or interface can be loaded from the archive file
218      * </p>
219      *
220      * @param className the class or interface name
221      * @param result instance for test status
222      * @return true if the class or interface can be loaded
223      */

224     protected boolean isClassLoadable(String JavaDoc className, Result result) {
225         ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
226         try {
227             Class.forName(className, false, jcl);
228             result.passed(smh.getLocalString
229                     ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.isClassLoadable.passed",
230                     "The class [ {0} ] is contained in the archive file",
231                     new Object JavaDoc[] {className}));
232             return true;
233         } catch(ClassNotFoundException JavaDoc cnfe) {
234             result.failed(smh.getLocalString
235                     ("com.sun.enterprise.tools.verifier.tests.connector.ConnectorTest.isClassLoadable.failed",
236                     "The class [ {0} ] is not contained in the archive file",
237                     new Object JavaDoc[] {className}));
238             return true;
239         }
240     }
241
242     protected String JavaDoc getAbstractArchiveUri(ConnectorDescriptor desc) {
243         String JavaDoc archBase = getVerifierContext().getAbstractArchive().
244                 getArchiveUri();
245         final ModuleDescriptor moduleDescriptor = desc.getModuleDescriptor();
246         if (moduleDescriptor.isStandalone()) {
247             return archBase; // it must be a stand-alone module; no such physical dir exists
248
} else {
249             return archBase + File.separator +
250                     FileUtils.makeFriendlyFileName(moduleDescriptor.getArchiveUri());
251         }
252     }
253
254 }
255
Popular Tags