KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > tools > derbyrunjartest


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.tools.derbyrunjartest
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.tools;
23
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.lang.Process JavaDoc;
27 import java.lang.Runtime JavaDoc;
28 import java.lang.SecurityException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.security.CodeSource JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.apache.derbyTesting.functionTests.harness.BackgroundStreamSaver;
34 import org.apache.derbyTesting.functionTests.harness.jvm;
35
36 public class derbyrunjartest {
37
38     public static void main(String JavaDoc[] args) throws Exception JavaDoc
39     {
40         // get location of run class.
41
CodeSource JavaDoc cs = null;
42         try {
43             cs = org.apache.derby.iapi.tools.run.class.getProtectionDomain().getCodeSource();
44         } catch (SecurityException JavaDoc se) {
45             System.out.println("Security exception: " + se.getMessage());
46         }
47  
48         URL JavaDoc result = cs.getLocation();
49         jvm jvm = null;
50         String JavaDoc derbyrunloc = null;
51
52         if (result.toString().endsWith(".jar")) {
53             derbyrunloc = result.toString().substring(5);
54             if (System.getProperty("os.name").startsWith("Windows"))
55               derbyrunloc = derbyrunloc.substring(1);
56
57             if ((System.getProperty("java.vm.name") != null) &&
58                     System.getProperty("java.vm.name").equals("J9")) {
59                 jvm = jvm.getJvm("j9_13");
60             } else {
61                 jvm = jvm.getJvm("currentjvm"); // ensure compatibility
62
}
63         }
64
65         String JavaDoc[][] testCommands = new String JavaDoc[][] {
66             {"ij", "--help"},
67             {"sysinfo", "-cp", "help"},
68             {"dblook"},
69             {"server"},
70         };
71
72         for (int i = 0; i < testCommands.length; i++) {
73             runtool(jvm, derbyrunloc, testCommands[i]);
74         }
75     }
76
77     private static void runtool(jvm jvm, String JavaDoc loc, String JavaDoc[] args)
78         throws IOException JavaDoc
79     {
80         System.out.println(concatenate(args) + ':');
81
82         if (jvm == null) {
83             org.apache.derby.iapi.tools.run.main(args);
84             return;
85         }
86
87         Vector JavaDoc cmd = jvm.getCommandLine();
88         cmd.addElement("-jar");
89         cmd.addElement(loc);
90         for (int i=0; i < args.length; i++) {
91             cmd.addElement(args[i]);
92         }
93         String JavaDoc command = concatenate((String JavaDoc[]) cmd.toArray(new String JavaDoc[0]));
94
95         Process JavaDoc pr = null;
96
97         try
98         {
99             pr = Runtime.getRuntime().exec(command);
100             BackgroundStreamSaver saver =
101                         new BackgroundStreamSaver(pr.getInputStream(), System.out);
102             saver.finish();
103             pr.waitFor();
104             pr.destroy();
105         } catch(Throwable JavaDoc t) {
106             System.out.println("Process exception: " + t.getMessage());
107             if (pr != null)
108             {
109                 pr.destroy();
110                 pr = null;
111             }
112         }
113     }
114
115     private static String JavaDoc concatenate(String JavaDoc[] args) {
116         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
117         for (int i = 0; i < args.length; i++) {
118             buf.append(args[i]);
119             if (i + 1 < args.length) buf.append(' ');
120         }
121         return buf.toString();
122     }
123 }
124
Popular Tags