1 /*
2  * Copyright (c) 2002, 2018, 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         "java.class.path",
81         "java.library.path",
82         "java.io.tmpdir",
83         "java.util.prefs.PreferencesFactory",
84         "sun.boot.library.path",
85     };
86 
87     /*
88      * Other props, as of 1.4.0, not classified as "preferred" or "ignored"
89      * Allowed to propagate to the results file if defined.
90      *
91      * java.runtime.name
92      * java.vendor
93      * java.vendor.url
94      * java.vendor.url.bug
95      * java.specification.name
96      * java.specification.vendor
97      * java.specification.version
98      * java.vm.specification.name
99      * java.vm.specification.vendor
100      * java.vm.specification.version
101      * java.vm.vendor
102      * user.language
103      * sun.os.patch.level
104      * sun.arch.data.model
105      * sun.io.unicode.encoding
106      */
107 
108     static String unknown = "unknown";
109 
110     static {
111         preferprops = new Hashtable();
112         for (int i = 0; i < preferredkeys.length; i++) {
preferprops.put(preferredkeys[i], unknown)113             preferprops.put(preferredkeys[i], unknown);
114         }
115         ignoreprops = new Hashtable();
116         for (int i = 0; i < ignoredkeys.length; i++) {
ignoreprops.put(ignoredkeys[i], unknown)117             ignoreprops.put(ignoredkeys[i], unknown);
118         }
119     }
120 
121     Hashtable props;
122     Vector results;
123     long start;
124     long end;
125     String title;
126     String description;
127 
ResultSet()128     public ResultSet() {
129         Properties sysprops = System.getProperties();
130         props = (Hashtable) preferprops.clone();
131         Enumeration enum_ = sysprops.keys();
132         while (enum_.hasMoreElements()) {
133             Object key = enum_.nextElement();
134             if (!ignoreprops.containsKey(key)) {
135                 props.put(key, sysprops.get(key));
136             }
137         }
138         results = new Vector();
139         start = System.currentTimeMillis();
140     }
141 
setTitle(String title)142     public void setTitle(String title) {
143         this.title = title;
144     }
145 
setDescription(String desc)146     public void setDescription(String desc) {
147         this.description = desc;
148     }
149 
record(Result result)150     public void record(Result result) {
151         results.addElement(result);
152     }
153 
summarize()154     public void summarize() {
155         end = System.currentTimeMillis();
156         for (int i = 0; i < results.size(); i++) {
157             ((Result) results.elementAt(i)).summarize();
158         }
159     }
160 
write(PrintWriter pw)161     public void write(PrintWriter pw) {
162         pw.println("<result-set version=\"0.1\" name=\""+title+"\">");
163         pw.println("  <test-desc>"+description+"</test-desc>");
164         pw.println("  <test-date start=\""+start+"\" end=\""+end+"\"/>");
165         for (int i = 0; i < preferredkeys.length; i++) {
166             String key = preferredkeys[i];
167             pw.println("  <sys-prop key=\""+key+
168                        "\" value=\""+props.get(key)+"\"/>");
169         }
170         Enumeration enum_ = props.keys();
171         while (enum_.hasMoreElements()) {
172             Object key = enum_.nextElement();
173             if (!preferprops.containsKey(key)) {
174                 pw.println("  <sys-prop key=\""+key+
175                            "\" value=\""+props.get(key)+"\"/>");
176             }
177         }
178         for (int i = 0; i < results.size(); i++) {
179             ((Result) results.elementAt(i)).write(pw);
180         }
181         pw.println("</result-set>");
182     }
183 }
184