1 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 ; 33 34 41 public class WebTestsUtil implements VerifierEventsListener { 42 43 protected final String listenerClassPath = "WEB-INF/classes"; 44 protected final String libraryClassPath = "WEB-INF/lib"; 45 46 private final String 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 cl = null; 50 51 52 57 public static WebTestsUtil getUtil(ClassLoader 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 76 public File extractJarFile(File warFilePath) throws IOException { 77 78 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 e) { 87 throw new IOException (e.getMessage()); 88 } 89 } 90 91 private void deleteDirectory(String oneDir) { 92 93 File[] listOfFiles; 94 File cleanDir; 95 96 cleanDir = new File(oneDir); 97 if (!cleanDir.exists()) 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 { String nextCleanDir = cleanDir + separator + listOfFiles[countFiles].getName(); 107 File newCleanDir = new File(nextCleanDir); 108 deleteDirectory(newCleanDir.getAbsolutePath()); 109 } 110 } } 113 cleanDir.delete(); 114 } 115 116 122 public void testFinished(EventObject e) { 123 } 125 126 134 public void allTestsFinished(EventObject e) { 135 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 148 public void appendCLWithWebInfContents() throws Throwable { 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 e) { 176 throw e; 177 } 178 } 179 180 private ClassLoader getClassLoader() { 181 return cl; 182 } 183 184 191 public Class loadClass(String className) throws Throwable { 192 193 if ((warFile==null || !warFile.exists()) 194 && (getClassLoader() == null)) { 195 throw new ClassNotFoundException (); 196 } 197 Class c = getClassLoader().loadClass(className); 198 return c; 199 } 200 } 201 | Popular Tags |