33542
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package jdk.xml.internal;
|
|
26 |
|
|
27 |
import java.io.File;
|
|
28 |
import java.io.FileInputStream;
|
|
29 |
import java.io.FileNotFoundException;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.security.AccessController;
|
|
33 |
import java.security.PrivilegedAction;
|
|
34 |
import java.security.PrivilegedActionException;
|
|
35 |
import java.security.PrivilegedExceptionAction;
|
|
36 |
import java.text.MessageFormat;
|
|
37 |
import java.util.Locale;
|
|
38 |
import java.util.Properties;
|
|
39 |
import java.util.ResourceBundle;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* This class contains utility methods for reading resources in the JAXP packages
|
|
43 |
*/
|
|
44 |
public class SecuritySupport {
|
|
45 |
/**
|
|
46 |
* Cache for properties in java.home/conf/jaxp.properties
|
|
47 |
*/
|
|
48 |
static final Properties cacheProps = new Properties();
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Flag indicating whether java.home/conf/jaxp.properties has been read
|
|
52 |
*/
|
|
53 |
static volatile boolean firstTime = true;
|
|
54 |
|
|
55 |
private SecuritySupport() {}
|
|
56 |
|
|
57 |
public static String getErrorMessage(Locale locale, String bundle, String key,
|
|
58 |
Object[] arguments) {
|
|
59 |
ResourceBundle rb;
|
|
60 |
if (locale != null) {
|
|
61 |
rb = ResourceBundle.getBundle(bundle,locale);
|
|
62 |
} else {
|
|
63 |
rb = ResourceBundle.getBundle(bundle);
|
|
64 |
}
|
|
65 |
|
|
66 |
String msg = rb.getString(key);
|
|
67 |
if (arguments != null) {
|
|
68 |
msg = MessageFormat.format(msg, arguments);
|
|
69 |
}
|
|
70 |
return msg;
|
|
71 |
}
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Reads JAXP system property with privilege
|
|
75 |
*
|
|
76 |
* @param propName the name of the property
|
|
77 |
* @return the value of the property
|
|
78 |
*/
|
|
79 |
public static String getSystemProperty(final String propName) {
|
|
80 |
return
|
|
81 |
AccessController.doPrivileged(
|
|
82 |
(PrivilegedAction<String>) () -> (String)System.getProperty(propName));
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Reads JAXP system property in this order: system property,
|
|
87 |
* $java.home/conf/jaxp.properties if the system property is not specified
|
|
88 |
*
|
|
89 |
* @param propName the name of the property
|
|
90 |
* @return the value of the property
|
|
91 |
*/
|
|
92 |
public static String getJAXPSystemProperty(String propName) {
|
|
93 |
String value = getSystemProperty(propName);
|
|
94 |
if (value == null) {
|
|
95 |
value = readJAXPProperty(propName);
|
|
96 |
}
|
|
97 |
return value;
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Reads the specified property from $java.home/conf/jaxp.properties
|
|
102 |
*
|
|
103 |
* @param propName the name of the property
|
|
104 |
* @return the value of the property
|
|
105 |
*/
|
|
106 |
public static String readJAXPProperty(String propName) {
|
|
107 |
String value = null;
|
|
108 |
InputStream is = null;
|
|
109 |
try {
|
|
110 |
if (firstTime) {
|
|
111 |
synchronized (cacheProps) {
|
|
112 |
if (firstTime) {
|
|
113 |
String configFile = getSystemProperty("java.home") + File.separator
|
|
114 |
+ "conf" + File.separator + "jaxp.properties";
|
|
115 |
File f = new File(configFile);
|
|
116 |
if (getFileExists(f)) {
|
|
117 |
is = getFileInputStream(f);
|
|
118 |
cacheProps.load(is);
|
|
119 |
}
|
|
120 |
firstTime = false;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
}
|
|
124 |
value = cacheProps.getProperty(propName);
|
|
125 |
|
|
126 |
} catch (IOException ex) {
|
|
127 |
} finally {
|
|
128 |
if (is != null) {
|
|
129 |
try {
|
|
130 |
is.close();
|
|
131 |
} catch (IOException ex) {}
|
|
132 |
}
|
|
133 |
}
|
|
134 |
|
|
135 |
return value;
|
|
136 |
}
|
|
137 |
|
|
138 |
//------------------- private methods ---------------------------
|
|
139 |
static boolean getFileExists(final File f) {
|
|
140 |
return (AccessController.doPrivileged((PrivilegedAction<Boolean>) ()
|
|
141 |
-> f.exists() ? Boolean.TRUE : Boolean.FALSE));
|
|
142 |
}
|
|
143 |
|
|
144 |
static FileInputStream getFileInputStream(final File file)
|
|
145 |
throws FileNotFoundException {
|
|
146 |
try {
|
|
147 |
return AccessController.doPrivileged((PrivilegedExceptionAction<FileInputStream>) ()
|
|
148 |
-> new FileInputStream(file));
|
|
149 |
} catch (PrivilegedActionException e) {
|
|
150 |
throw (FileNotFoundException) e.getException();
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|