01: /*
02:
03: Derby - Class org.apache.derbyTesting.functionTests.harness.UnJar
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to You under the Apache License, Version 2.0
09: (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.harness;
23:
24: import java.io.*;
25:
26: /**
27: The upgrade tests use jar files containing older version
28: databases. These need to be "unjarred" in order to do the tests.
29: */
30: public class UnJar {
31:
32: public UnJar() {
33: }
34:
35: public static void main(String args[]) throws Exception {
36: UnJar uj = new UnJar();
37: uj.unjar(args[0], null, true);
38: }
39:
40: public static void unjar(String jarname, String outputdir,
41: boolean useprocess) throws ClassNotFoundException,
42: IOException {
43: if (outputdir == null)
44: outputdir = System.getProperty("user.dir");
45:
46: InputStream is = RunTest.loadTestResource("upgrade" + '/'
47: + jarname);
48: if (is == null) {
49: System.out.println("File not found: " + jarname);
50: System.exit(1);
51: }
52:
53: // Copy to the current directory in order to unjar it
54: //System.out.println("Copy the jarfile to: " + outputdir);
55: File jarFile = new File((new File(outputdir, jarname))
56: .getCanonicalPath());
57: //System.out.println("jarFile: " + jarFile.getPath());
58: FileOutputStream fos = new FileOutputStream(jarFile);
59: byte[] data = new byte[1024];
60: int len;
61: while ((len = is.read(data)) != -1) {
62: fos.write(data, 0, len);
63: }
64: fos.close();
65:
66: // Now unjar the file
67: String jarCmd = "jar xf " + jarFile.getPath();
68: if (useprocess == true) {
69: // Now execute the jar command
70: Process pr = null;
71: try {
72: //System.out.println("Use process to execute: " + jarCmd);
73: pr = Runtime.getRuntime().exec(jarCmd);
74:
75: pr.waitFor();
76: //System.out.println("Process done.");
77: pr.destroy();
78: } catch (Throwable t) {
79: System.out.println("Process exception: "
80: + t.getMessage());
81: if (pr != null) {
82: pr.destroy();
83: pr = null;
84: }
85: }
86: } else {
87: System.out
88: .println("Jar not implemented yet with useprocess=false");
89: }
90: }
91: }
|