source: trunk/openjdk/jdk/test/TestEnv.java@ 98

Last change on this file since 98 was 2, checked in by dmik, 15 years ago

Imported OpenJDK 6 b19 sources from Oracle.

File size: 5.5 KB
Line 
1/*
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24import java.util.Properties;
25import java.io.File;
26import java.io.FileReader;
27import java.io.IOException;
28import java.security.AccessController;
29import java.security.PrivilegedAction;
30
31/**
32 * Provides access to the value of properties in the test environment.
33 * Properties are typically host names used by the networking or NIO tests.
34 *
35 * If the environment variable JTREG_TESTENV is set then its value is taken
36 * to be the path to a java properties file defining some or all of the
37 * properties required by the tests. Environment variables are passed to
38 * jtreg with the -e option, eg:
39 * jtreg -e JTREG_TESTENV=/config/testenv.properties ...
40 *
41 * Where the environment variable is not set or the properties file doesn't
42 * define all the required properties then the properties are loaded from the
43 * properties file ${user.home}/.jtreg.testenv where ${user.home} is the value
44 * of the system property "user.home".
45 *
46 * Finally, this class hard-codes a number of properties for when the properties
47 * are not defined in the user's home directory or by the environment variable.
48 *
49 * Note to test developers: You can invoke this class from shell scripts to
50 * get the value of properties using the -get option, eg:
51 * VALUE=`java TestEnv -get host`
52 * will set VALUE to the value of the "host" property.
53 */
54
55public class TestEnv {
56 // environment variable to configure location of properties file
57 private static final String CONFIG_PROPERTY = "JTREG_TESTENV";
58
59 // properties file in home directory
60 private static final String RC_FILE = ".jtreg.testenv";
61
62 // hard-coded defaults
63 private static final String defaultProps[][] = {
64
65 // Reachable host with the following services running:
66 // - echo service (port 7)
67 // - day time port (port 13)
68 { "host", "javaweb.sfbay.sun.com" },
69
70 // Reachable host that refuses connections to port 80
71 { "refusing_host", "jano1.sfbay.sun.com" },
72
73 // Reachable host that is of sufficient hops away that a connection
74 // takes a while to be established (connect doesn't complete immediatly)
75 { "far_host", "irejano.ireland.sun.com" },
76
77 // Hostname that cannot be resolved by named service
78 { "unresovable_host", "blah-blah.blah-blah.blah" },
79 };
80
81 private static Properties props = loadProperties();
82
83 /**
84 * Returns the value of a property in the test environment or {@code null}
85 * if the property is not defined.
86 */
87 public static String getProperty(String key) {
88 return props.getProperty(key);
89 }
90
91 /**
92 * Prints the value of a property, or "unknown" to standard output
93 */
94 public static void main(String[] args) {
95 if (args.length == 0) {
96 props.list(System.out);
97 System.exit(0);
98 }
99 if (args.length < 2 || !args[0].equals("-get")) {
100 System.err.println("Usage: java TestEnv [-get prop]");
101 System.exit(-1);
102 }
103 String value = props.getProperty(args[1]);
104 if (value == null)
105 value = "unknown";
106 System.out.println(value);
107 }
108
109 /**
110 * Loads properties. The properties are loaded in the following order:
111 *
112 * 1. Default (hard-coded) properties
113 * 2. Properties file in home directory (overrides defaults)
114 * 3. Properties file configured by environment variable (overrides 1 & 2)
115 */
116 private static Properties loadProperties() {
117 // default properties
118 final Properties p = new Properties();
119 for (int i=0; i<defaultProps.length; i++) {
120 p.put(defaultProps[i][0], defaultProps[i][1]);
121 }
122
123 AccessController.doPrivileged(new PrivilegedAction<Void>() {
124 public Void run() {
125 // load from ~/<config-file>
126 String rcfile = System.getProperty("user.home") +
127 File.separator + RC_FILE;
128 loadPropertiesFromFile(rcfile, p);
129
130
131 // load from file set by environment variable
132 String config = System.getenv(CONFIG_PROPERTY);
133 if (config != null) {
134 loadPropertiesFromFile(config, p);
135 }
136 return null;
137 }
138 });
139
140 return p;
141 }
142
143 private static void loadPropertiesFromFile(String file, Properties p) {
144 try {
145 FileReader reader = new FileReader(file);
146 try {
147 p.load(reader);
148 } finally {
149 reader.close();
150 }
151 } catch (IOException ignore) { }
152 }
153}
Note: See TracBrowser for help on using the repository browser.