Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/JSSecurityManager.java
38924 views
1
/*
2
* Copyright (c) 1999, 2014, 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 com.sun.media.sound;
27
28
import java.io.BufferedInputStream;
29
import java.io.InputStream;
30
import java.io.File;
31
import java.io.FileInputStream;
32
33
import java.util.ArrayList;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Properties;
37
import java.util.ServiceLoader;
38
39
import java.security.AccessController;
40
import java.security.PrivilegedAction;
41
42
import javax.sound.sampled.AudioPermission;
43
44
/** Managing security in the Java Sound implementation.
45
* This class contains all code that uses and is used by
46
* SecurityManager.doPrivileged().
47
*
48
* @author Matthias Pfisterer
49
*/
50
final class JSSecurityManager {
51
52
/** Prevent instantiation.
53
*/
54
private JSSecurityManager() {
55
}
56
57
/** Checks if the VM currently has a SecurityManager installed.
58
* Note that this may change over time. So the result of this method
59
* should not be cached.
60
*
61
* @return true if a SecurityManger is installed, false otherwise.
62
*/
63
private static boolean hasSecurityManager() {
64
return (System.getSecurityManager() != null);
65
}
66
67
68
static void checkRecordPermission() throws SecurityException {
69
if(Printer.trace) Printer.trace("JSSecurityManager.checkRecordPermission()");
70
SecurityManager sm = System.getSecurityManager();
71
if (sm != null) {
72
sm.checkPermission(new AudioPermission("record"));
73
}
74
}
75
76
/** Load properties from a file.
77
This method tries to load properties from the filename give into
78
the passed properties object.
79
If the file cannot be found or something else goes wrong,
80
the method silently fails.
81
@param properties The properties bundle to store the values of the
82
properties file.
83
@param filename The filename of the properties file to load. This
84
filename is interpreted as relative to the subdirectory "lib" in
85
the JRE directory.
86
*/
87
static void loadProperties(final Properties properties,
88
final String filename) {
89
if(hasSecurityManager()) {
90
try {
91
// invoke the privileged action using 1.2 security
92
PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
93
public Void run() {
94
loadPropertiesImpl(properties, filename);
95
return null;
96
}
97
};
98
AccessController.doPrivileged(action);
99
if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security");
100
} catch (Exception e) {
101
if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security");
102
// try without using JDK 1.2 security
103
loadPropertiesImpl(properties, filename);
104
}
105
} else {
106
// not JDK 1.2 security, assume we already have permission
107
loadPropertiesImpl(properties, filename);
108
}
109
}
110
111
112
private static void loadPropertiesImpl(Properties properties,
113
String filename) {
114
if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()");
115
String fname = System.getProperty("java.home");
116
try {
117
if (fname == null) {
118
throw new Error("Can't find java.home ??");
119
}
120
File f = new File(fname, "lib");
121
f = new File(f, filename);
122
fname = f.getCanonicalPath();
123
InputStream in = new FileInputStream(fname);
124
BufferedInputStream bin = new BufferedInputStream(in);
125
try {
126
properties.load(bin);
127
} finally {
128
if (in != null) {
129
in.close();
130
}
131
}
132
} catch (Throwable t) {
133
if (Printer.trace) {
134
System.err.println("Could not load properties file \"" + fname + "\"");
135
t.printStackTrace();
136
}
137
}
138
if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed");
139
}
140
141
/** Create a Thread in the current ThreadGroup.
142
*/
143
static Thread createThread(final Runnable runnable,
144
final String threadName,
145
final boolean isDaemon, final int priority,
146
final boolean doStart) {
147
Thread thread = new Thread(runnable);
148
if (threadName != null) {
149
thread.setName(threadName);
150
}
151
thread.setDaemon(isDaemon);
152
if (priority >= 0) {
153
thread.setPriority(priority);
154
}
155
if (doStart) {
156
thread.start();
157
}
158
return thread;
159
}
160
161
static synchronized <T> List<T> getProviders(final Class<T> providerClass) {
162
List<T> p = new ArrayList<>(7);
163
// ServiceLoader creates "lazy" iterator instance, but it ensures that
164
// next/hasNext run with permissions that are restricted by whatever
165
// creates the ServiceLoader instance, so it requires to be called from
166
// privileged section
167
final PrivilegedAction<Iterator<T>> psAction =
168
new PrivilegedAction<Iterator<T>>() {
169
@Override
170
public Iterator<T> run() {
171
return ServiceLoader.load(providerClass).iterator();
172
}
173
};
174
final Iterator<T> ps = AccessController.doPrivileged(psAction);
175
176
// the iterator's hasNext() method looks through classpath for
177
// the provider class names, so it requires read permissions
178
PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
179
public Boolean run() {
180
return ps.hasNext();
181
}
182
};
183
184
while (AccessController.doPrivileged(hasNextAction)) {
185
try {
186
// the iterator's next() method creates instances of the
187
// providers and it should be called in the current security
188
// context
189
T provider = ps.next();
190
if (providerClass.isInstance(provider)) {
191
// $$mp 2003-08-22
192
// Always adding at the beginning reverses the
193
// order of the providers. So we no longer have
194
// to do this in AudioSystem and MidiSystem.
195
p.add(0, provider);
196
}
197
} catch (Throwable t) {
198
//$$fb 2002-11-07: do not fail on SPI not found
199
if (Printer.err) t.printStackTrace();
200
}
201
}
202
return p;
203
}
204
}
205
206