Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/com/sun/media/sound/JSSecurityManager.java
41161 views
1
/*
2
* Copyright (c) 1999, 2021, 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.Reader;
29
import java.nio.file.Files;
30
import java.nio.file.Path;
31
import java.nio.file.Paths;
32
import java.security.AccessController;
33
import java.security.PrivilegedAction;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Properties;
38
import java.util.ServiceLoader;
39
40
import javax.sound.sampled.AudioPermission;
41
42
/** Managing security in the Java Sound implementation.
43
* This class contains all code that uses and is used by
44
* SecurityManager.doPrivileged().
45
*
46
* @author Matthias Pfisterer
47
*/
48
final class JSSecurityManager {
49
50
/** Prevent instantiation.
51
*/
52
private JSSecurityManager() {
53
}
54
55
static void checkRecordPermission() throws SecurityException {
56
@SuppressWarnings("removal")
57
SecurityManager sm = System.getSecurityManager();
58
if (sm != null) {
59
sm.checkPermission(new AudioPermission("record"));
60
}
61
}
62
63
/**
64
* Load properties from a file.
65
* <p>
66
* This method tries to load properties from the filename give into the
67
* passed properties object. If the file cannot be found or something else
68
* goes wrong, the method silently fails.
69
* <p>
70
* If the file referenced in "javax.sound.config.file" property exists and
71
* the user has an access to it, then it will be loaded, otherwise default
72
* configuration file "JAVA_HOME/conf/sound.properties" will be loaded.
73
*
74
* @param properties the properties bundle to store the values of the
75
* properties file
76
*/
77
@SuppressWarnings("removal")
78
static void loadProperties(final Properties properties) {
79
final String customFile = AccessController.doPrivileged(
80
(PrivilegedAction<String>) () -> System.getProperty(
81
"javax.sound.config.file"));
82
if (customFile != null) {
83
if (loadPropertiesImpl(properties, customFile)) {
84
return;
85
}
86
}
87
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
88
final String home = System.getProperty("java.home");
89
if (home == null) {
90
throw new Error("Can't find java.home ??");
91
}
92
loadPropertiesImpl(properties, home, "conf", "sound.properties");
93
return null;
94
});
95
}
96
97
private static boolean loadPropertiesImpl(final Properties properties,
98
String first, String... more) {
99
final Path fname = Paths.get(first, more);
100
try (final Reader reader = Files.newBufferedReader(fname)) {
101
properties.load(reader);
102
return true;
103
} catch (final Throwable t) {
104
return false;
105
}
106
}
107
108
/** Create a Thread in the current ThreadGroup.
109
*/
110
static Thread createThread(final Runnable runnable,
111
final String threadName,
112
final boolean isDaemon, final int priority,
113
final boolean doStart)
114
{
115
String name = (threadName != null) ? threadName : "JSSM Thread";
116
Thread thread = new Thread(null, runnable, threadName, 0, false);
117
118
thread.setDaemon(isDaemon);
119
if (priority >= 0) {
120
thread.setPriority(priority);
121
}
122
if (doStart) {
123
thread.start();
124
}
125
return thread;
126
}
127
128
@SuppressWarnings("removal")
129
static synchronized <T> List<T> getProviders(final Class<T> providerClass) {
130
List<T> p = new ArrayList<>(7);
131
// ServiceLoader creates "lazy" iterator instance, but it ensures that
132
// next/hasNext run with permissions that are restricted by whatever
133
// creates the ServiceLoader instance, so it requires to be called from
134
// privileged section
135
final PrivilegedAction<Iterator<T>> psAction =
136
new PrivilegedAction<Iterator<T>>() {
137
@Override
138
public Iterator<T> run() {
139
return ServiceLoader.load(providerClass).iterator();
140
}
141
};
142
final Iterator<T> ps = AccessController.doPrivileged(psAction);
143
144
// the iterator's hasNext() method looks through classpath for
145
// the provider class names, so it requires read permissions
146
PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() {
147
@Override
148
public Boolean run() {
149
return ps.hasNext();
150
}
151
};
152
153
while (AccessController.doPrivileged(hasNextAction)) {
154
try {
155
// the iterator's next() method creates instances of the
156
// providers and it should be called in the current security
157
// context
158
T provider = ps.next();
159
if (providerClass.isInstance(provider)) {
160
// $$mp 2003-08-22
161
// Always adding at the beginning reverses the
162
// order of the providers. So we no longer have
163
// to do this in AudioSystem and MidiSystem.
164
p.add(0, provider);
165
}
166
} catch (Throwable t) {
167
//$$fb 2002-11-07: do not fail on SPI not found
168
if (Printer.err) t.printStackTrace();
169
}
170
}
171
return p;
172
}
173
}
174
175