KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > dbjarUtil


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.lang.cursor
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12    http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.io.BufferedInputStream JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.net.URLClassLoader JavaDoc;
32 import java.util.zip.ZipEntry JavaDoc;
33 import java.util.zip.ZipOutputStream JavaDoc;
34
35 /**
36     Simple program to archive a database up in a jar file
37     within the test harness.
38 */

39
40 public class dbjarUtil
41 {
42     /**
43         jarname - jarname to use
44         path - path to database
45         dbname - database name in archive
46     */

47     public static void createArchive(String JavaDoc jarName, String JavaDoc path, String JavaDoc dbName) throws Exception JavaDoc {
48
49         String JavaDoc root = System.getProperty("derby.system.home", System.getProperty("user.dir"));
50
51         // get list of files
52
File JavaDoc top = new File JavaDoc(root, path);
53
54         if (!top.isDirectory())
55             throw new Exception JavaDoc(top.toString() + " is not a directory");
56
57         // jar file paths in the JDB CURL are relative to the root
58
// derby.system.home or user.dir, so need to create the jar there.
59
ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(new File JavaDoc(root, jarName)));
60
61         addEntries(zos, top, dbName, top.getPath().length());
62         
63         zos.close();
64     }
65
66
67     static void addEntries(ZipOutputStream JavaDoc zos, File JavaDoc dir, String JavaDoc dbName, int old) throws Exception JavaDoc {
68
69         String JavaDoc[] list = dir.list();
70
71         for (int i = 0; i < list.length; i++) {
72
73             File JavaDoc f = new File JavaDoc(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 JavaDoc zos,
88         File JavaDoc f, String JavaDoc dbName, int old) throws IOException JavaDoc
89     {
90
91         String JavaDoc s = f.getPath().replace(File.separatorChar, '/');
92
93         s = s.substring(old);
94
95         s = dbName.concat(s);
96
97         // jar has forward slashes!
98
ZipEntry JavaDoc ze= new ZipEntry JavaDoc(s);
99         ze.setTime(f.lastModified());
100
101         zos.putNextEntry(ze);
102
103         byte[] byte8= new byte[1024];
104         BufferedInputStream JavaDoc bufferedInputStream10= new BufferedInputStream JavaDoc((new FileInputStream JavaDoc(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 JavaDoc jarName) throws MalformedURLException JavaDoc
120     {
121         String JavaDoc root = System.getProperty("derby.system.home", System.getProperty("user.dir"));
122
123         File JavaDoc jar = new File JavaDoc(root, jarName);
124         
125         URLClassLoader JavaDoc cl = new URLClassLoader JavaDoc(new URL JavaDoc[] {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