1 /* 2 * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 41 package j2dbench; 42 43 import java.util.Hashtable; 44 import java.util.Properties; 45 import java.util.Enumeration; 46 import java.util.Vector; 47 import java.io.PrintWriter; 48 49 public class ResultSet { 50 static Hashtable preferprops; 51 static Hashtable ignoreprops; 52 53 // Preferred props - will be listed even if undefined 54 static String preferredkeys[] = { 55 "java.version", 56 "java.runtime.version", 57 "java.class.version", 58 "java.vm.version", 59 "java.vm.name", 60 "java.vm.info", 61 "java.home", 62 "java.compiler", 63 "os.arch", 64 "os.name", 65 "os.version", 66 "user.name", 67 "sun.cpu.endian", 68 "sun.cpu.isalist", 69 }; 70 71 // Ignored props - will not be copied to results file 72 static String ignoredkeys[] = { 73 "user.dir", 74 "user.home", 75 "user.timezone", 76 "path.separator", 77 "line.separator", 78 "file.separator", 79 "file.encoding", 80 "file.encoding.pkg", 81 "java.class.path", 82 "java.library.path", 83 "java.io.tmpdir", 84 "java.ext.dirs", 85 "java.endorsed.dirs", 86 "java.util.prefs.PreferencesFactory", 87 "sun.java2d.fontpath", 88 "sun.boot.library.path", 89 "sun.boot.class.path", 90 }; 91 92 /* 93 * Other props, as of 1.4.0, not classified as "preferred" or "ignored" 94 * Allowed to propagate to the results file if defined. 95 * 96 * java.runtime.name 97 * java.vendor 98 * java.vendor.url 99 * java.vendor.url.bug 100 * java.specification.name 101 * java.specification.vendor 102 * java.specification.version 103 * java.vm.specification.name 104 * java.vm.specification.vendor 105 * java.vm.specification.version 106 * java.vm.vendor 107 * java.awt.graphicsenv 108 * java.awt.printerjob 109 * user.language 110 * sun.os.patch.level 111 * sun.arch.data.model 112 * sun.io.unicode.encoding 113 */ 114 115 static String unknown = "unknown"; 116 117 static { 118 preferprops = new Hashtable(); 119 for (int i = 0; i < preferredkeys.length; i++) { preferprops.put(preferredkeys[i], unknown)120 preferprops.put(preferredkeys[i], unknown); 121 } 122 ignoreprops = new Hashtable(); 123 for (int i = 0; i < ignoredkeys.length; i++) { ignoreprops.put(ignoredkeys[i], unknown)124 ignoreprops.put(ignoredkeys[i], unknown); 125 } 126 } 127 128 Hashtable props; 129 Vector results; 130 long start; 131 long end; 132 String title; 133 String description; 134 ResultSet()135 public ResultSet() { 136 Properties sysprops = System.getProperties(); 137 props = (Hashtable) preferprops.clone(); 138 Enumeration enum_ = sysprops.keys(); 139 while (enum_.hasMoreElements()) { 140 Object key = enum_.nextElement(); 141 if (!ignoreprops.containsKey(key)) { 142 props.put(key, sysprops.get(key)); 143 } 144 } 145 results = new Vector(); 146 start = System.currentTimeMillis(); 147 } 148 setTitle(String title)149 public void setTitle(String title) { 150 this.title = title; 151 } 152 setDescription(String desc)153 public void setDescription(String desc) { 154 this.description = desc; 155 } 156 record(Result result)157 public void record(Result result) { 158 results.addElement(result); 159 } 160 summarize()161 public void summarize() { 162 end = System.currentTimeMillis(); 163 for (int i = 0; i < results.size(); i++) { 164 ((Result) results.elementAt(i)).summarize(); 165 } 166 } 167 write(PrintWriter pw)168 public void write(PrintWriter pw) { 169 pw.println("<result-set version=\"0.1\" name=\""+title+"\">"); 170 pw.println(" <test-desc>"+description+"</test-desc>"); 171 pw.println(" <test-date start=\""+start+"\" end=\""+end+"\"/>"); 172 for (int i = 0; i < preferredkeys.length; i++) { 173 String key = preferredkeys[i]; 174 pw.println(" <sys-prop key=\""+key+ 175 "\" value=\""+props.get(key)+"\"/>"); 176 } 177 Enumeration enum_ = props.keys(); 178 while (enum_.hasMoreElements()) { 179 Object key = enum_.nextElement(); 180 if (!preferprops.containsKey(key)) { 181 pw.println(" <sys-prop key=\""+key+ 182 "\" value=\""+props.get(key)+"\"/>"); 183 } 184 } 185 for (int i = 0; i < results.size(); i++) { 186 ((Result) results.elementAt(i)).write(pw); 187 } 188 pw.println("</result-set>"); 189 } 190 } 191