author | chegar |
Fri, 02 Dec 2011 11:39:48 +0000 | |
changeset 11123 | 399112af8803 |
parent 5506 | 202f599c92aa |
child 18215 | b2afd66ce6db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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; |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
37 |
import java.util.ServiceLoader; |
2 | 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 |
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 |
||
77 |
static void loadLibrary(final String libName) { |
|
78 |
try { |
|
79 |
if (hasSecurityManager()) { |
|
80 |
if(Printer.debug) Printer.debug("using security manager to load library"); |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
81 |
PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
82 |
public Void run() { |
2 | 83 |
System.loadLibrary(libName); |
84 |
return null; |
|
85 |
} |
|
86 |
}; |
|
87 |
AccessController.doPrivileged(action); |
|
88 |
} else { |
|
89 |
if(Printer.debug) Printer.debug("not using security manager to load library"); |
|
90 |
System.loadLibrary(libName); |
|
91 |
} |
|
92 |
if (Printer.debug) Printer.debug("loaded library " + libName); |
|
93 |
} catch (UnsatisfiedLinkError e2) { |
|
94 |
if (Printer.err)Printer.err("UnsatisfiedLinkError loading native library " + libName); |
|
95 |
throw(e2); |
|
96 |
} |
|
97 |
} |
|
98 |
||
99 |
||
100 |
static String getProperty(final String propertyName) { |
|
101 |
String propertyValue; |
|
102 |
if (hasSecurityManager()) { |
|
103 |
if(Printer.debug) Printer.debug("using JDK 1.2 security to get property"); |
|
104 |
try{ |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
105 |
PrivilegedAction<String> action = new PrivilegedAction<String>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
106 |
public String run() { |
2 | 107 |
try { |
108 |
return System.getProperty(propertyName); |
|
109 |
} catch (Throwable t) { |
|
110 |
return null; |
|
111 |
} |
|
112 |
} |
|
113 |
}; |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
114 |
propertyValue = AccessController.doPrivileged(action); |
2 | 115 |
} catch( Exception e ) { |
116 |
if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties"); |
|
117 |
propertyValue = System.getProperty(propertyName); |
|
118 |
} |
|
119 |
} else { |
|
120 |
if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties"); |
|
121 |
propertyValue = System.getProperty(propertyName); |
|
122 |
} |
|
123 |
return propertyValue; |
|
124 |
} |
|
125 |
||
126 |
||
127 |
/** Load properties from a file. |
|
128 |
This method tries to load properties from the filename give into |
|
129 |
the passed properties object. |
|
130 |
If the file cannot be found or something else goes wrong, |
|
131 |
the method silently fails. |
|
132 |
@param properties The properties bundle to store the values of the |
|
133 |
properties file. |
|
134 |
@param filename The filename of the properties file to load. This |
|
135 |
filename is interpreted as relative to the subdirectory "lib" in |
|
136 |
the JRE directory. |
|
137 |
*/ |
|
138 |
static void loadProperties(final Properties properties, |
|
139 |
final String filename) { |
|
140 |
if(hasSecurityManager()) { |
|
141 |
try { |
|
142 |
// invoke the privileged action using 1.2 security |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
143 |
PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
144 |
public Void run() { |
2 | 145 |
loadPropertiesImpl(properties, filename); |
146 |
return null; |
|
147 |
} |
|
148 |
}; |
|
149 |
AccessController.doPrivileged(action); |
|
150 |
if(Printer.debug)Printer.debug("Loaded properties with JDK 1.2 security"); |
|
151 |
} catch (Exception e) { |
|
152 |
if(Printer.debug)Printer.debug("Exception loading properties with JDK 1.2 security"); |
|
153 |
// try without using JDK 1.2 security |
|
154 |
loadPropertiesImpl(properties, filename); |
|
155 |
} |
|
156 |
} else { |
|
157 |
// not JDK 1.2 security, assume we already have permission |
|
158 |
loadPropertiesImpl(properties, filename); |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
||
163 |
private static void loadPropertiesImpl(Properties properties, |
|
164 |
String filename) { |
|
165 |
if(Printer.trace)Printer.trace(">> JSSecurityManager: loadPropertiesImpl()"); |
|
166 |
String fname = System.getProperty("java.home"); |
|
167 |
try { |
|
168 |
if (fname == null) { |
|
169 |
throw new Error("Can't find java.home ??"); |
|
170 |
} |
|
171 |
File f = new File(fname, "lib"); |
|
172 |
f = new File(f, filename); |
|
173 |
fname = f.getCanonicalPath(); |
|
174 |
InputStream in = new FileInputStream(fname); |
|
175 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
176 |
try { |
|
177 |
properties.load(bin); |
|
178 |
} finally { |
|
179 |
if (in != null) { |
|
180 |
in.close(); |
|
181 |
} |
|
182 |
} |
|
183 |
} catch (Throwable t) { |
|
184 |
if (Printer.trace) { |
|
185 |
System.err.println("Could not load properties file \"" + fname + "\""); |
|
186 |
t.printStackTrace(); |
|
187 |
} |
|
188 |
} |
|
189 |
if(Printer.trace)Printer.trace("<< JSSecurityManager: loadPropertiesImpl() completed"); |
|
190 |
} |
|
191 |
||
192 |
||
193 |
private static ThreadGroup getTopmostThreadGroup() { |
|
194 |
ThreadGroup topmostThreadGroup; |
|
195 |
if(hasSecurityManager()) { |
|
196 |
try { |
|
197 |
// invoke the privileged action using 1.2 security |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
198 |
PrivilegedAction<ThreadGroup> action = new PrivilegedAction<ThreadGroup>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
199 |
public ThreadGroup run() { |
2 | 200 |
try { |
201 |
return getTopmostThreadGroupImpl(); |
|
202 |
} catch (Throwable t) { |
|
203 |
return null; |
|
204 |
} |
|
205 |
} |
|
206 |
}; |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
207 |
topmostThreadGroup = AccessController.doPrivileged(action); |
2 | 208 |
if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security"); |
209 |
} catch (Exception e) { |
|
210 |
if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security"); |
|
211 |
// try without using JDK 1.2 security |
|
212 |
topmostThreadGroup = getTopmostThreadGroupImpl(); |
|
213 |
} |
|
214 |
} else { |
|
215 |
// not JDK 1.2 security, assume we already have permission |
|
216 |
topmostThreadGroup = getTopmostThreadGroupImpl(); |
|
217 |
} |
|
218 |
return topmostThreadGroup; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
private static ThreadGroup getTopmostThreadGroupImpl() { |
|
223 |
if(Printer.trace)Printer.trace(">> JSSecurityManager: getTopmostThreadGroupImpl()"); |
|
224 |
ThreadGroup g = Thread.currentThread().getThreadGroup(); |
|
225 |
while ((g.getParent() != null) && (g.getParent().getParent() != null)) { |
|
226 |
g = g.getParent(); |
|
227 |
} |
|
228 |
if(Printer.trace)Printer.trace("<< JSSecurityManager: getTopmostThreadGroupImpl() completed"); |
|
229 |
return g; |
|
230 |
} |
|
231 |
||
232 |
||
233 |
/** Create a Thread in the topmost ThreadGroup. |
|
234 |
*/ |
|
235 |
static Thread createThread(final Runnable runnable, |
|
236 |
final String threadName, |
|
237 |
final boolean isDaemon, final int priority, |
|
238 |
final boolean doStart) { |
|
239 |
Thread thread = null; |
|
240 |
if(hasSecurityManager()) { |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
241 |
PrivilegedAction<Thread> action = new PrivilegedAction<Thread>() { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
242 |
public Thread run() { |
2 | 243 |
try { |
244 |
return createThreadImpl(runnable, threadName, |
|
245 |
isDaemon, priority, |
|
246 |
doStart); |
|
247 |
} catch (Throwable t) { |
|
248 |
return null; |
|
249 |
} |
|
250 |
} |
|
251 |
}; |
|
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
252 |
thread = AccessController.doPrivileged(action); |
2 | 253 |
if(Printer.debug) Printer.debug("created thread with JDK 1.2 security"); |
254 |
} else { |
|
255 |
if(Printer.debug)Printer.debug("not using JDK 1.2 security"); |
|
256 |
thread = createThreadImpl(runnable, threadName, isDaemon, priority, |
|
257 |
doStart); |
|
258 |
} |
|
259 |
return thread; |
|
260 |
} |
|
261 |
||
262 |
||
263 |
private static Thread createThreadImpl(Runnable runnable, |
|
264 |
String threadName, |
|
265 |
boolean isDaemon, int priority, |
|
266 |
boolean doStart) { |
|
267 |
ThreadGroup threadGroup = getTopmostThreadGroupImpl(); |
|
268 |
Thread thread = new Thread(threadGroup, runnable); |
|
269 |
if (threadName != null) { |
|
270 |
thread.setName(threadName); |
|
271 |
} |
|
272 |
thread.setDaemon(isDaemon); |
|
273 |
if (priority >= 0) { |
|
274 |
thread.setPriority(priority); |
|
275 |
} |
|
276 |
if (doStart) { |
|
277 |
thread.start(); |
|
278 |
} |
|
279 |
return thread; |
|
280 |
} |
|
281 |
||
282 |
||
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
283 |
static <T> List<T> getProviders(final Class<T> providerClass) { |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
284 |
List<T> p = new ArrayList<>(); |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
285 |
// ServiceLoader creates "lazy" iterator instance, so it doesn't, |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
286 |
// require do be called from privileged section |
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
287 |
final Iterator<T> ps = ServiceLoader.load(providerClass).iterator(); |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
288 |
|
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
289 |
// the iterator's hasNext() method looks through classpath for |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
290 |
// the provider class names, so it requires read permissions |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
291 |
PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
292 |
public Boolean run() { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
293 |
return ps.hasNext(); |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
294 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
295 |
}; |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
296 |
|
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
297 |
while (AccessController.doPrivileged(hasNextAction)) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
298 |
try { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
299 |
// the iterator's next() method creates instances of the |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
300 |
// providers and it should be called in the current security |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
301 |
// context |
11123
399112af8803
7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers
chegar
parents:
5506
diff
changeset
|
302 |
T provider = ps.next(); |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
303 |
if (providerClass.isInstance(provider)) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
304 |
// $$mp 2003-08-22 |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
305 |
// Always adding at the beginning reverses the |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
306 |
// order of the providers. So we no longer have |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
307 |
// to do this in AudioSystem and MidiSystem. |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
308 |
p.add(0, provider); |
2 | 309 |
} |
3453
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
310 |
} catch (Throwable t) { |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
311 |
//$$fb 2002-11-07: do not fail on SPI not found |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
312 |
if (Printer.err) t.printStackTrace(); |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
313 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
314 |
} |
55b3a9c935cd
6777448: JDK13Services.getProviders creates instances with full privileges [hawtin, alexp]
amenkov
parents:
2
diff
changeset
|
315 |
return p; |
2 | 316 |
} |
317 |
} |