001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.cursor
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.lang;
023:
024: import java.io.BufferedInputStream;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.net.MalformedURLException;
030: import java.net.URL;
031: import java.net.URLClassLoader;
032: import java.util.zip.ZipEntry;
033: import java.util.zip.ZipOutputStream;
034:
035: /**
036: Simple program to archive a database up in a jar file
037: within the test harness.
038: */
039:
040: public class dbjarUtil {
041: /**
042: jarname - jarname to use
043: path - path to database
044: dbname - database name in archive
045: */
046: public static void createArchive(String jarName, String path,
047: String dbName) throws Exception {
048:
049: String root = System.getProperty("derby.system.home", System
050: .getProperty("user.dir"));
051:
052: // get list of files
053: File top = new File(root, path);
054:
055: if (!top.isDirectory())
056: throw new Exception(top.toString() + " is not a directory");
057:
058: // jar file paths in the JDB CURL are relative to the root
059: // derby.system.home or user.dir, so need to create the jar there.
060: ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
061: new File(root, jarName)));
062:
063: addEntries(zos, top, dbName, top.getPath().length());
064:
065: zos.close();
066: }
067:
068: static void addEntries(ZipOutputStream zos, File dir,
069: String dbName, int old) throws Exception {
070:
071: String[] list = dir.list();
072:
073: for (int i = 0; i < list.length; i++) {
074:
075: File f = new File(dir, list[i]);
076: if (f.isDirectory()) {
077: addEntries(zos, f, dbName, old);
078: } else {
079: addFile(zos, f, dbName, old);
080: }
081:
082: }
083: }
084:
085: static void addFile(ZipOutputStream zos, File f, String dbName,
086: int old) throws IOException {
087:
088: String s = f.getPath().replace(File.separatorChar, '/');
089:
090: s = s.substring(old);
091:
092: s = dbName.concat(s);
093:
094: // jar has forward slashes!
095: ZipEntry ze = new ZipEntry(s);
096: ze.setTime(f.lastModified());
097:
098: zos.putNextEntry(ze);
099:
100: byte[] byte8 = new byte[1024];
101: BufferedInputStream bufferedInputStream10 = new BufferedInputStream(
102: (new FileInputStream(f)));
103: while (true) {
104: int int9 = bufferedInputStream10.read(byte8, 0,
105: byte8.length);
106: if (int9 == -1) {
107: break;
108: }
109: zos.write(byte8, 0, int9);
110: }
111:
112: bufferedInputStream10.close();
113: zos.closeEntry();
114: }
115:
116: public static void setDBContextClassLoader(String jarName)
117: throws MalformedURLException {
118: String root = System.getProperty("derby.system.home", System
119: .getProperty("user.dir"));
120:
121: File jar = new File(root, jarName);
122:
123: URLClassLoader cl = new URLClassLoader(
124: new URL[] { jar.toURL() });
125: java.lang.Thread.currentThread().setContextClassLoader(cl);
126:
127: }
128:
129: public static void setNullContextClassLoader() {
130: java.lang.Thread.currentThread().setContextClassLoader(null);
131: }
132:
133: }
|