1 21 22 package org.apache.derbyTesting.functionTests.tests.lang; 23 24 import java.io.BufferedInputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.net.URLClassLoader ; 32 import java.util.zip.ZipEntry ; 33 import java.util.zip.ZipOutputStream ; 34 35 39 40 public class dbjarUtil 41 { 42 47 public static void createArchive(String jarName, String path, String dbName) throws Exception { 48 49 String root = System.getProperty("derby.system.home", System.getProperty("user.dir")); 50 51 File top = new File (root, path); 53 54 if (!top.isDirectory()) 55 throw new Exception (top.toString() + " is not a directory"); 56 57 ZipOutputStream zos = new ZipOutputStream (new FileOutputStream (new File (root, jarName))); 60 61 addEntries(zos, top, dbName, top.getPath().length()); 62 63 zos.close(); 64 } 65 66 67 static void addEntries(ZipOutputStream zos, File dir, String dbName, int old) throws Exception { 68 69 String [] list = dir.list(); 70 71 for (int i = 0; i < list.length; i++) { 72 73 File f = new File (dir, list[i]); 74 if (f.isDirectory()) { 75 addEntries(zos, f, dbName, old); 76 } else { 77 addFile(zos, f, dbName, old); 78 } 79 80 } 81 } 82 83 84 85 86 static void addFile( 87 ZipOutputStream zos, 88 File f, String dbName, int old) throws IOException 89 { 90 91 String s = f.getPath().replace(File.separatorChar, '/'); 92 93 s = s.substring(old); 94 95 s = dbName.concat(s); 96 97 ZipEntry ze= new ZipEntry (s); 99 ze.setTime(f.lastModified()); 100 101 zos.putNextEntry(ze); 102 103 byte[] byte8= new byte[1024]; 104 BufferedInputStream bufferedInputStream10= new BufferedInputStream ((new FileInputStream (f))); 105 while (true) 106 { 107 int int9= bufferedInputStream10.read(byte8, 0, byte8.length); 108 if (int9 == -1) 109 { 110 break; 111 } 112 zos.write(byte8, 0, int9); 113 } 114 115 bufferedInputStream10.close(); 116 zos.closeEntry(); 117 } 118 119 public static void setDBContextClassLoader(String jarName) throws MalformedURLException 120 { 121 String root = System.getProperty("derby.system.home", System.getProperty("user.dir")); 122 123 File jar = new File (root, jarName); 124 125 URLClassLoader cl = new URLClassLoader (new URL [] {jar.toURL()}); 126 java.lang.Thread.currentThread().setContextClassLoader(cl); 127 128 } 129 130 public static void setNullContextClassLoader() 131 { 132 java.lang.Thread.currentThread().setContextClassLoader(null); 133 } 134 135 } 136 137 | Popular Tags |