KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > web > WebTestsUtil


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.web;
24
25 import com.sun.enterprise.tools.verifier.*;
26 import com.sun.enterprise.tools.verifier.web.WebCheckMgrImpl;
27 import com.sun.enterprise.util.JarClassLoader;
28
29 import java.io.*;
30 import java.net.*;
31 import java.util.*;
32 import java.util.EventObject JavaDoc;
33
34 /**
35  * Singleton Utility class to load war archive and get class loader
36  *
37  * @author Jerome Dochez
38  * @author Sheetal Vartak
39  * @version 1.0
40  */

41 public class WebTestsUtil implements VerifierEventsListener {
42
43     protected final String JavaDoc listenerClassPath = "WEB-INF/classes";
44     protected final String JavaDoc libraryClassPath = "WEB-INF/lib";
45     
46     private final String JavaDoc separator= System.getProperty("file.separator");
47     private static File warFile = new File(System.getProperty("java.io.tmpdir"), "listenertmp");
48     private static WebTestsUtil util = null;
49     private static ClassLoader JavaDoc cl = null;
50     
51     
52     /**
53      * <p>
54      * Get the unique instance for this class
55      * </p>
56      */

57     public static WebTestsUtil getUtil(ClassLoader JavaDoc cLoader) {
58     
59         if (util==null) {
60         util = new WebTestsUtil();
61         WebCheckMgrImpl.addVerifierEventsListener(util);
62         cl = cLoader;
63         }
64         return util;
65     }
66     
67     
68     /**
69      * <p>
70      * Extract if necessary the war file and return the path to the extracted
71      * archive file
72      * </p>
73      * @param warFilePath is the path for the warfile to extract
74      * @return the file identifier for the extracted directory
75      */

76     public File extractJarFile(File warFilePath) throws IOException {
77         
78         //if (!warFile.exists())
79
try {
80         warFile = new File(System.getProperty("java.io.tmpdir"), "listenertmp");
81             if (!warFile.exists()) {
82                 warFile.mkdirs();
83             }
84         VerifierUtils.copyArchiveToDir(warFilePath, warFile);
85         return warFile;
86     }catch (Exception JavaDoc e) {
87         throw new IOException (e.getMessage());
88     }
89     }
90     
91     private void deleteDirectory(String JavaDoc oneDir) {
92         
93         File[] listOfFiles;
94         File cleanDir;
95
96         cleanDir = new File(oneDir);
97         if (!cleanDir.exists()) // Nothing to do. Return;
98
return;
99
100         listOfFiles = cleanDir.listFiles();
101         if(listOfFiles != null) {
102             for(int countFiles = 0; countFiles < listOfFiles.length; countFiles++) {
103                 if (listOfFiles[countFiles].isFile()) {
104                     listOfFiles[countFiles].delete();
105                 } else { // It is a directory
106
String JavaDoc nextCleanDir = cleanDir + separator + listOfFiles[countFiles].getName();
107                     File newCleanDir = new File(nextCleanDir);
108                     deleteDirectory(newCleanDir.getAbsolutePath());
109                 }
110             }// End for loop
111
} // End if statement
112

113         cleanDir.delete();
114     }
115     
116     /**
117      * <p>
118      * Individual test completion notification event
119      * </p>
120      * @param e event object which source is the result of the individual test
121      */

122     public void testFinished(EventObject JavaDoc e) {
123         // do nothing, we don't care
124
}
125     
126     /**
127      * <p>
128      * Notification that all tests pertinent to a verifier check manager have
129      * been completed
130      * </p>
131      * @param e event object which source is the check manager for the
132      * completed tests
133      */

134     public void allTestsFinished(EventObject JavaDoc e) {
135         // remove tmp files
136
if ((warFile != null) && (warFile.exists())) {
137             deleteDirectory(warFile.getAbsolutePath());
138         }
139         warFile=null;
140         util=null;
141         cl=null;
142         WebCheckMgrImpl.removeVerifierEventsListener(this);
143     }
144     
145     /**
146      * method that appends the class loader's class path
147      */

148     public void appendCLWithWebInfContents() throws Throwable JavaDoc{
149
150     try {
151         File warclasses = new File(warFile, listenerClassPath);
152         File libraries = new File(warFile, libraryClassPath);
153         Vector<File> v = new Vector<File>();
154         if (libraries.exists()) {
155         File[] libs = libraries.listFiles();
156         for (int i=0;i<libs.length;i++) {
157             if (libs[i].getName().endsWith(".jar")) {
158             v.add(libs[i]);
159             }
160         }
161         }
162         URL[] repositories = new URL[v.size() + 1];
163         try {
164         repositories[0] = warclasses.toURI().toURL();
165         for (int i = 1;i <= v.size(); i++) {
166             repositories[i] = ((File) v.elementAt(i-1)).toURI().toURL();
167         }
168         }catch(MalformedURLException ex) {
169         throw ex;
170         }
171         
172         for (int i = 0; i < repositories.length; i++) {
173         ((JarClassLoader)cl).appendURL(repositories[i]);
174         }
175     }catch (Exception JavaDoc e) {
176         throw e;
177     }
178     }
179
180     private ClassLoader JavaDoc getClassLoader() {
181     return cl;
182     }
183     
184     /**
185      * <p>
186      * load a class from the war archive file
187      * </p>
188      * @param className the class to load
189      * @return the class object if it can be loaded
190      */

191     public Class JavaDoc loadClass(String JavaDoc className) throws Throwable JavaDoc {
192
193         if ((warFile==null || !warFile.exists())
194             && (getClassLoader() == null)) {
195             throw new ClassNotFoundException JavaDoc();
196         }
197         Class JavaDoc c = getClassLoader().loadClass(className);
198         return c;
199     }
200 }
201
Popular Tags