Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/jdk/xml/internal/SecuritySupport.java
38813 views
1
/*
2
* Copyright (c) 2017, 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
26
package jdk.xml.internal;
27
28
import java.io.File;
29
import java.io.FileInputStream;
30
import java.io.FileNotFoundException;
31
import java.io.IOException;
32
import java.io.InputStream;
33
import java.security.AccessController;
34
import java.security.PrivilegedAction;
35
import java.security.PrivilegedActionException;
36
import java.security.PrivilegedExceptionAction;
37
import java.util.Properties;
38
39
/**
40
* This class contains utility methods for reading resources in the JAXP packages
41
*/
42
class SecuritySupport {
43
/**
44
* Cache for properties in java.home/lib/jaxp.properties
45
*/
46
static final Properties cacheProps = new Properties();
47
48
/**
49
* Flag indicating whether java.home/lib/jaxp.properties has been read
50
*/
51
static volatile boolean firstTime = true;
52
53
private SecuritySupport() {}
54
55
/**
56
* Reads JAXP system property with privilege
57
*
58
* @param propName the name of the property
59
* @return the value of the property
60
*/
61
public static String getSystemProperty(final String propName) {
62
return AccessController.doPrivileged(new PrivilegedAction<String>() {
63
@Override
64
public String run() {
65
return System.getProperty(propName);
66
}
67
});
68
}
69
70
/**
71
* Reads a system property.
72
*
73
* @param <T> the type of the property value
74
* @param type the type of the property value
75
* @param propName the name of the property
76
* @param defValue the default value
77
* @return the value of the property, or the default value of no system
78
* property is found
79
*/
80
public static <T> T getJAXPSystemProperty(Class<T> type, String propName, String defValue) {
81
String value = getJAXPSystemProperty(propName);
82
if (value == null) {
83
value = defValue;
84
}
85
if (Integer.class.isAssignableFrom(type)) {
86
return type.cast(Integer.parseInt(value));
87
} else if (Boolean.class.isAssignableFrom(type)) {
88
return type.cast(Boolean.parseBoolean(value));
89
}
90
return type.cast(value);
91
}
92
93
/**
94
* Reads JAXP system property in this order: system property,
95
* $java.home/lib/jaxp.properties if the system property is not specified
96
*
97
* @param propName the name of the property
98
* @return the value of the property
99
*/
100
public static String getJAXPSystemProperty(String propName) {
101
String value = getSystemProperty(propName);
102
if (value == null) {
103
value = readJAXPProperty(propName);
104
}
105
return value;
106
}
107
108
/**
109
* Reads the specified property from $java.home/lib/jaxp.properties
110
*
111
* @param propName the name of the property
112
* @return the value of the property
113
*/
114
public static String readJAXPProperty(String propName) {
115
String value = null;
116
InputStream is = null;
117
try {
118
if (firstTime) {
119
synchronized (cacheProps) {
120
if (firstTime) {
121
String configFile = getSystemProperty("java.home") + File.separator
122
+ "lib" + File.separator + "jaxp.properties";
123
File f = new File(configFile);
124
if (getFileExists(f)) {
125
is = getFileInputStream(f);
126
cacheProps.load(is);
127
}
128
firstTime = false;
129
}
130
}
131
}
132
value = cacheProps.getProperty(propName);
133
134
} catch (IOException ex) {
135
} finally {
136
if (is != null) {
137
try {
138
is.close();
139
} catch (IOException ex) {}
140
}
141
}
142
143
return value;
144
}
145
146
//------------------- private methods ---------------------------
147
static boolean getFileExists(final File f) {
148
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
149
@Override
150
public Boolean run() {
151
return f.exists() ? Boolean.TRUE : Boolean.FALSE;
152
}
153
});
154
}
155
156
static FileInputStream getFileInputStream(final File file) throws FileNotFoundException {
157
try {
158
return AccessController.doPrivileged(new PrivilegedExceptionAction<FileInputStream>() {
159
@Override
160
public FileInputStream run() throws Exception {
161
return new FileInputStream(file);
162
}
163
});
164
} catch (PrivilegedActionException e) {
165
throw (FileNotFoundException) e.getException();
166
}
167
}
168
}
169
170