KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > harness > UnJar


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.UnJar
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.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  
33     public UnJar()
34     {
35     }
36     
37     public static void main(String JavaDoc args[]) throws Exception JavaDoc
38     {
39         UnJar uj = new UnJar();
40         uj.unjar(args[0], null, true);
41     }
42     
43     public static void unjar(String JavaDoc jarname, String JavaDoc outputdir, boolean useprocess)
44         throws ClassNotFoundException JavaDoc, IOException
45     {
46         if (outputdir == null)
47             outputdir = System.getProperty("user.dir");
48         
49         InputStream is =
50             RunTest.loadTestResource("upgrade" + '/' + jarname);
51         if (is == null)
52         {
53             System.out.println("File not found: " + jarname);
54             System.exit(1);
55         }
56         
57         // Copy to the current directory in order to unjar it
58
//System.out.println("Copy the jarfile to: " + outputdir);
59
File jarFile = new File((new File(outputdir, jarname)).getCanonicalPath());
60         //System.out.println("jarFile: " + jarFile.getPath());
61
FileOutputStream fos = new FileOutputStream(jarFile);
62         byte[] data = new byte[1024];
63         int len;
64         while ((len = is.read(data)) != -1)
65         {
66             fos.write(data, 0, len);
67         }
68         fos.close();
69         
70         // Now unjar the file
71
String JavaDoc jarCmd = "jar xf " + jarFile.getPath();
72         if ( useprocess == true )
73         {
74             // Now execute the jar command
75
Process JavaDoc pr = null;
76             try
77             {
78                 //System.out.println("Use process to execute: " + jarCmd);
79
pr = Runtime.getRuntime().exec(jarCmd);
80                 
81                 pr.waitFor();
82                 //System.out.println("Process done.");
83
pr.destroy();
84             }
85             catch(Throwable JavaDoc t)
86             {
87                 System.out.println("Process exception: " + t.getMessage());
88                 if (pr != null)
89                 {
90                     pr.destroy();
91                     pr = null;
92                 }
93             }
94         }
95         else
96         {
97             System.out.println("Jar not implemented yet with useprocess=false");
98         }
99     }
100 }
101         
102             
103
Popular Tags