KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.harness.jvm
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.util.Enumeration JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.util.Vector JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.derby.impl.tools.sysinfo.ZipInfoProperties;
32 import org.apache.derbyTesting.junit.SecurityManagerSetup;
33
34
35 /**
36   <p>This class provides the interface and mechanism
37   for plugging VMs into the system. Typically
38   you only need to add a new implementation if your
39   supported attributes or command line building are
40   different from those that exist.
41
42   <p>this class has fields for all options that a JDK VM can take,
43   that is the reference point for all others. Note some VMs (like jview)
44   don't take all options and will ignore them (like -mx). Defining
45   the system property "verbose" to 1 will give you warnings for ignored
46   properties in a properly implemented subclass.
47
48   <p> here is the canonical output from java -help for options we take:
49   <pre>
50     -noasyncgc don't allow asynchronous garbage collection
51     -verbosegc print a message when garbage collection occurs
52     -noclassgc disable class garbage collection
53     -ss<number> set the maximum native stack size for any thread
54     -oss<number> set the maximum Java stack size for any thread
55     -ms<number> set the initial Java heap size
56     -mx<number> set the maximum Java heap size
57     -classpath <directories separated by semicolons>
58                       list directories in which to look for classes
59     -prof[:<file>] output profiling data to .\java.prof or .\<file>
60     -verify verify all classes when read in
61     -noverify do not verify any class
62     -nojit turn off the jit
63     -Dprop=name define property; can be specified more than once
64   </pre>
65
66   @author ames
67  */

68
69
70 public abstract class jvm {
71
72     // they all take their defaults as the initial value.
73
// -1, null, and false all will mean we won't include them
74
// in the command line.
75

76     // flags just take the whole string of flags as is
77
public String JavaDoc flags = null;
78     // -noasyncgc don't allow asynchronous garbage collection
79
public boolean noasyncgc = false;
80     // -verbosegc print a message when garbage collection occurs
81
public boolean verbosegc = false;
82     // -noclassgc disable class garbage collection
83
public boolean noclassgc = false;
84     // -ss<number> set the maximum native stack size for any thread
85
public long ss = -1;
86     // -oss<number> set the maximum Java stack size for any thread
87
public long oss = -1;
88     // -ms<number> set the initial Java heap size
89
public long ms = -1;
90     // -mx<number> set the maximum Java heap size
91
public long mx = -1;
92     // -classpath <directories separated by semicolons>
93
// list directories in which to look for classes
94
public String JavaDoc classpath = null;
95     // -prof[:<file>] output profiling data to .\java.prof or .\<file>
96
public String JavaDoc prof = null;
97     // -verify verify all classes when read in
98
// (remote verification is the default)
99
public boolean verify = false;
100     // -noverify do not verify any class
101
// (remote verification is the default)
102
public boolean noverify = false;
103     // -nojit turn off the jit
104
public boolean nojit = false;
105     // -Dprop=name define property; can be specified more than once
106
public Vector JavaDoc D = null;
107     // java cmd (java, java_g)
108
public String JavaDoc javaCmd = "java";
109     // major and minor version
110
public String JavaDoc majorVersion = "";
111     public String JavaDoc minorVersion = "";
112     public int imajor = 0;
113     public int iminor = 0;
114     String JavaDoc hostName;
115
116     // security defaults relative to WS
117
// not used if jvmargs serverCodeBase are set
118
private static String JavaDoc DEFAULT_POLICY="util/derby_tests.policy";
119     private static String JavaDoc DEFAULT_CODEBASE="/classes";
120
121     // constructors
122
public jvm() { }
123
124     public jvm(boolean noasyncgc, boolean verbosegc, boolean noclassgc,
125     long ss, long oss, long ms, long mx, String JavaDoc classpath, String JavaDoc prof,
126     boolean verify, boolean noverify, boolean nojit, Vector JavaDoc D) {
127         this.noasyncgc=noasyncgc;
128         this.noclassgc=noclassgc;
129         this.verbosegc=verbosegc;
130         this.ss=ss;
131         this.oss=oss;
132         this.ms=ms;
133         this.mx=mx;
134         this.classpath=classpath;
135         this.prof=prof;
136         this.verify=verify;
137         this.noverify=noverify;
138         this.nojit=nojit;
139         this.D=D;
140     }
141     // more typical use:
142
public jvm(String JavaDoc classpath, Vector JavaDoc D) {
143         this.classpath=classpath;
144         this.D=D;
145     }
146     // more typical use:
147
public jvm(long ms, long mx, String JavaDoc classpath, Vector JavaDoc D) {
148         this.ms=ms;
149         this.mx=mx;
150         this.classpath=classpath;
151         this.D=D;
152     }
153
154     /**
155        return the property definition introducer, with a space if a
156        separator is needed.
157      */

158     public abstract String JavaDoc getDintro();
159     public abstract String JavaDoc getName();
160     public void setNoasyncgc(boolean noasyncgc) { this.noasyncgc=noasyncgc; }
161     public void setNoclassgc(boolean noclassgc) { this.noclassgc=noclassgc; }
162     public void setVerbosegc(boolean verbosegc) { this.verbosegc=verbosegc; }
163     public void setSs(long ss) { this.ss=ss; }
164     public void setOss(long oss) { this.oss=oss; }
165     public void setMs(long ms) { this.ms = ms; }
166     public void setMx(long mx) { this.mx = mx; }
167     public void setClasspath(String JavaDoc classpath) { this.classpath = classpath; }
168     public void setProf(String JavaDoc prof) { this.prof=prof; }
169     public void setVerify(boolean verify) { this.verify=verify; }
170     public void setNoverify(boolean noverify) { this.noverify=noverify; }
171     public void setNojit(boolean nojit) { this.nojit=nojit; }
172     public void setD(Vector JavaDoc D) { this.D = D; }
173     public void setFlags(String JavaDoc flags) { this.flags = flags; }
174     public void setJavaCmd(String JavaDoc jcmd) { this.javaCmd = jcmd; }
175
176     
177     public Vector JavaDoc getCommandLine()
178     {
179         Vector JavaDoc v = new Vector JavaDoc();
180         v.addElement(javaCmd);
181         v.addElement("-Duser.language=en");
182         v.addElement("-Duser.country=US");
183         if ( (flags != null) && (flags.length()>0) )
184         {
185             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(flags);
186             while (st.hasMoreTokens())
187             {
188                 v.addElement(st.nextToken());
189             }
190         }
191         return v;
192     }
193
194     // implementation, used by subclasses only
195
int verboselevel = -1;
196     public void warn(String JavaDoc msg) {
197       if (verboselevel == -1) {
198          try {
199            verboselevel = Integer.parseInt((String JavaDoc)(System.getProperty("verbose")));
200          } catch (Exception JavaDoc e) {
201            verboselevel = 0;
202          }
203       }
204       if (verboselevel >0)
205           System.out.println("jvm: "+msg);
206     }
207
208     // utility for locating a jvm.
209
/**
210         pass in class name for JVM. If we can't find it, try
211     also org.apache.derbyTesting.functionTests.harness.<jvmName>
212      */

213     public static jvm getJvm(String JavaDoc jvmName) throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
214     jvm result = null;
215         try {
216         result = (jvm)Class.forName(jvmName).newInstance();
217         } catch (ClassNotFoundException JavaDoc e) {
218         result = (jvm)Class.forName("org.apache.derbyTesting.functionTests.harness."+jvmName).newInstance();
219         }
220         return result;
221     }
222
223     /**
224       Get the current JVM using the normal test harness rules for finding
225       a JVM.
226       <OL>
227       <LI> If the sytem property 'jvm' use this name.
228       <LI> else if the java version starts with 1.2 use
229            "jdk12".
230       <LI> else use "currentjvm".
231       */

232     public static jvm getCurrentJvm() throws Exception JavaDoc
233     {
234         String JavaDoc jvmName = System.getProperty("jvm");
235         if ( (jvmName == null) || (jvmName.length()==0) )
236         {
237             String JavaDoc javaVersion = System.getProperty("java.version");
238             if (javaVersion.startsWith("1.2"))
239                 jvmName = "jdk12";
240             else
241                 jvmName = "currentjvm";
242         }
243         return getJvm(jvmName);
244     }
245
246     /**
247       Return the major version number
248     */

249     public int getMajorVersion()
250     {
251         return imajor;
252     }
253     
254     /**
255       Return the major version number
256     */

257     public int getMinorVersion()
258     {
259         return iminor;
260     }
261     
262     /**
263       Get the current JVM using the normal test harness rules for finding
264       a JVM.
265       */

266     public void setVersion() throws Exception JavaDoc
267     {
268         // check for jdk12 or higher
269
String JavaDoc javaVersion = System.getProperty("java.version");
270         int i = javaVersion.indexOf('.');
271         int j = javaVersion.indexOf('.', i+1);
272         majorVersion = javaVersion.substring(0, i);
273         minorVersion = javaVersion.substring(i+1, j);
274         Integer JavaDoc minor = new Integer JavaDoc(minorVersion);
275         iminor = minor.intValue();
276         Integer JavaDoc major = new Integer JavaDoc(majorVersion);
277         imajor = major.intValue();
278         
279         String JavaDoc jvmName = System.getProperty("jvm");
280         
281         if ( (jvmName == null) || (jvmName.length()==0) )
282         {
283             if (iminor < 2)
284                 jvmName = "currentjvm";
285             else
286                 jvmName = "jdk" + majorVersion + minorVersion;
287         }
288     }
289     
290     /** Find $WS based on the assumption that JAVA_HOME is $WS/<jvm_name>
291      * or $WS/<jvm_name>/jre
292      * @return path of $WS
293      */

294     protected static String JavaDoc guessWSHome()
295     {
296         String JavaDoc wshome="";
297         String JavaDoc jhome = System.getProperty("java.home");
298         String JavaDoc sep = System.getProperty("file.separator");
299         // need to strip off the java directory assuming it's something
300
// like ibm14/jre or ibm14
301
wshome = jhome.substring(0,jhome.indexOf(sep + "jre"));
302         wshome = wshome.substring(0,wshome.lastIndexOf(sep));
303         return wshome;
304     }
305
306     public static String JavaDoc findCodeBase(boolean[] isJar)
307     {
308         String JavaDoc classpath = System.getProperty("java.class.path");
309         char sep = '/';
310         ZipInfoProperties zip[]=
311             org.apache.derby.impl.tools.sysinfo.Main.getAllInfo (classpath);
312         for (int i = 0; i < zip.length; i++)
313         {
314             // it's a url so should just have forward slashes
315
String JavaDoc location = zip[i].getLocation().replace('\\','/');
316             if (location.indexOf("derbynet.jar") != -1)
317             {
318                 isJar[0] = true;
319                 return location.substring(0,location.lastIndexOf(sep));
320             }
321             else if ((location.indexOf("classes") != -1) &&
322                      location.indexOf(".jar") == -1)
323             {
324                 isJar[0] = false;
325                 return location;
326             }
327         }
328         return null;
329     }
330     
331     /**
332      * set up security properties for server command line.
333      */

334     protected void setSecurityProps() throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
335     {
336         D = jvm.getSecurityProps(D);
337         
338     }
339     
340     static Vector JavaDoc getSecurityProps(Vector JavaDoc D) throws ClassNotFoundException JavaDoc, IOException JavaDoc
341     {
342         if (D == null)
343             D = new Vector JavaDoc();
344         
345         String JavaDoc userDir = System.getProperty("user.dir");
346         String JavaDoc policyFile = userDir + baseName(DEFAULT_POLICY);
347
348         String JavaDoc serverCodeBase = System.getProperty("serverCodeBase");
349         boolean[] isJar = new boolean[1];
350         if (serverCodeBase == null)
351             serverCodeBase = findCodeBase(isJar);
352    
353         
354         if (serverCodeBase == null)
355         {
356             String JavaDoc ws = guessWSHome();
357             serverCodeBase = ws + DEFAULT_CODEBASE;
358                  
359         }
360         
361         File JavaDoc pf = new File JavaDoc(policyFile);
362         File JavaDoc cb = new File JavaDoc(serverCodeBase);
363
364         if (!pf.exists())
365         {
366             System.out.println("WARNING: Running without Security manager." +
367                                "policy File (" + policyFile +
368                                ") or serverCodeBase(" + serverCodeBase +
369                                ") not available");
370         return D;
371         }
372         
373         D.addElement("java.security.manager");
374         D.addElement("java.security.policy=" + pf.getAbsolutePath());
375  
376         Properties JavaDoc jusetup =
377             SecurityManagerSetup.getPolicyFilePropertiesForOldHarness();
378         // Take the definitions from the way JUnit tests
379
// set them up. This then supports the jar files being
380
// in different locations.
381
for (Enumeration JavaDoc p = jusetup.keys(); p.hasMoreElements(); )
382         {
383             String JavaDoc key = (String JavaDoc) p.nextElement();
384             D.addElement(key + "=" + jusetup.getProperty(key));
385         }
386         
387
388         // file path to the codebase
389
D.addElement("derbyTesting.codedir=" + cb.getAbsolutePath());
390         String JavaDoc hostName = (System.getProperty("hostName"));
391         if (hostName == null)
392             hostName="localhost";
393         D.addElement("derbyTesting.serverhost=" + hostName);
394         // in the case of testing with a remote host, this is irrelevant,
395
// when testing 'normal' it is also localhost:
396
D.addElement("derbyTesting.clienthost=" + hostName);
397         
398         return D;
399         
400     }
401
402     /** Get the base file name from a resource name string
403      * @param resourceName (e.g. /org/apache/derbyTesting/functionTests/util/derby_tests.policy)
404      * @return short name (e.g. derby_tests.policy)
405      */

406     private static String JavaDoc baseName(String JavaDoc resourceName)
407     {
408       
409         return resourceName.substring(resourceName.lastIndexOf("/"),resourceName.length());
410     }
411 }
412
Popular Tags