32 |
32 |
33 import java.util.ArrayList; |
33 import java.util.ArrayList; |
34 import java.util.Iterator; |
34 import java.util.Iterator; |
35 import java.util.List; |
35 import java.util.List; |
36 import java.util.Properties; |
36 import java.util.Properties; |
|
37 import java.util.ServiceLoader; |
37 |
38 |
38 import java.security.AccessController; |
39 import java.security.AccessController; |
39 import java.security.PrivilegedAction; |
40 import java.security.PrivilegedAction; |
40 |
41 |
41 import javax.sound.sampled.AudioPermission; |
42 import javax.sound.sampled.AudioPermission; |
42 |
|
43 import sun.misc.Service; |
|
44 |
|
45 |
43 |
46 /** Managing security in the Java Sound implementation. |
44 /** Managing security in the Java Sound implementation. |
47 * This class contains all code that uses and is used by |
45 * This class contains all code that uses and is used by |
48 * SecurityManager.doPrivileged(). |
46 * SecurityManager.doPrivileged(). |
49 * |
47 * |
78 |
76 |
79 static void loadLibrary(final String libName) { |
77 static void loadLibrary(final String libName) { |
80 try { |
78 try { |
81 if (hasSecurityManager()) { |
79 if (hasSecurityManager()) { |
82 if(Printer.debug) Printer.debug("using security manager to load library"); |
80 if(Printer.debug) Printer.debug("using security manager to load library"); |
83 PrivilegedAction action = new PrivilegedAction() { |
81 PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
84 public Object run() { |
82 public Void run() { |
85 System.loadLibrary(libName); |
83 System.loadLibrary(libName); |
86 return null; |
84 return null; |
87 } |
85 } |
88 }; |
86 }; |
89 AccessController.doPrivileged(action); |
87 AccessController.doPrivileged(action); |
102 static String getProperty(final String propertyName) { |
100 static String getProperty(final String propertyName) { |
103 String propertyValue; |
101 String propertyValue; |
104 if (hasSecurityManager()) { |
102 if (hasSecurityManager()) { |
105 if(Printer.debug) Printer.debug("using JDK 1.2 security to get property"); |
103 if(Printer.debug) Printer.debug("using JDK 1.2 security to get property"); |
106 try{ |
104 try{ |
107 PrivilegedAction action = new PrivilegedAction() { |
105 PrivilegedAction<String> action = new PrivilegedAction<String>() { |
108 public Object run() { |
106 public String run() { |
109 try { |
107 try { |
110 return System.getProperty(propertyName); |
108 return System.getProperty(propertyName); |
111 } catch (Throwable t) { |
109 } catch (Throwable t) { |
112 return null; |
110 return null; |
113 } |
111 } |
114 } |
112 } |
115 }; |
113 }; |
116 propertyValue = (String) AccessController.doPrivileged(action); |
114 propertyValue = AccessController.doPrivileged(action); |
117 } catch( Exception e ) { |
115 } catch( Exception e ) { |
118 if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties"); |
116 if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties"); |
119 propertyValue = System.getProperty(propertyName); |
117 propertyValue = System.getProperty(propertyName); |
120 } |
118 } |
121 } else { |
119 } else { |
140 static void loadProperties(final Properties properties, |
138 static void loadProperties(final Properties properties, |
141 final String filename) { |
139 final String filename) { |
142 if(hasSecurityManager()) { |
140 if(hasSecurityManager()) { |
143 try { |
141 try { |
144 // invoke the privileged action using 1.2 security |
142 // invoke the privileged action using 1.2 security |
145 PrivilegedAction action = new PrivilegedAction() { |
143 PrivilegedAction<Void> action = new PrivilegedAction<Void>() { |
146 public Object run() { |
144 public Void run() { |
147 loadPropertiesImpl(properties, filename); |
145 loadPropertiesImpl(properties, filename); |
148 return null; |
146 return null; |
149 } |
147 } |
150 }; |
148 }; |
151 AccessController.doPrivileged(action); |
149 AccessController.doPrivileged(action); |
195 private static ThreadGroup getTopmostThreadGroup() { |
193 private static ThreadGroup getTopmostThreadGroup() { |
196 ThreadGroup topmostThreadGroup; |
194 ThreadGroup topmostThreadGroup; |
197 if(hasSecurityManager()) { |
195 if(hasSecurityManager()) { |
198 try { |
196 try { |
199 // invoke the privileged action using 1.2 security |
197 // invoke the privileged action using 1.2 security |
200 PrivilegedAction action = new PrivilegedAction() { |
198 PrivilegedAction<ThreadGroup> action = new PrivilegedAction<ThreadGroup>() { |
201 public Object run() { |
199 public ThreadGroup run() { |
202 try { |
200 try { |
203 return getTopmostThreadGroupImpl(); |
201 return getTopmostThreadGroupImpl(); |
204 } catch (Throwable t) { |
202 } catch (Throwable t) { |
205 return null; |
203 return null; |
206 } |
204 } |
207 } |
205 } |
208 }; |
206 }; |
209 topmostThreadGroup = (ThreadGroup) AccessController.doPrivileged(action); |
207 topmostThreadGroup = AccessController.doPrivileged(action); |
210 if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security"); |
208 if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security"); |
211 } catch (Exception e) { |
209 } catch (Exception e) { |
212 if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security"); |
210 if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security"); |
213 // try without using JDK 1.2 security |
211 // try without using JDK 1.2 security |
214 topmostThreadGroup = getTopmostThreadGroupImpl(); |
212 topmostThreadGroup = getTopmostThreadGroupImpl(); |
238 final String threadName, |
236 final String threadName, |
239 final boolean isDaemon, final int priority, |
237 final boolean isDaemon, final int priority, |
240 final boolean doStart) { |
238 final boolean doStart) { |
241 Thread thread = null; |
239 Thread thread = null; |
242 if(hasSecurityManager()) { |
240 if(hasSecurityManager()) { |
243 PrivilegedAction action = new PrivilegedAction() { |
241 PrivilegedAction<Thread> action = new PrivilegedAction<Thread>() { |
244 public Object run() { |
242 public Thread run() { |
245 try { |
243 try { |
246 return createThreadImpl(runnable, threadName, |
244 return createThreadImpl(runnable, threadName, |
247 isDaemon, priority, |
245 isDaemon, priority, |
248 doStart); |
246 doStart); |
249 } catch (Throwable t) { |
247 } catch (Throwable t) { |
250 return null; |
248 return null; |
251 } |
249 } |
252 } |
250 } |
253 }; |
251 }; |
254 thread = (Thread) AccessController.doPrivileged(action); |
252 thread = AccessController.doPrivileged(action); |
255 if(Printer.debug) Printer.debug("created thread with JDK 1.2 security"); |
253 if(Printer.debug) Printer.debug("created thread with JDK 1.2 security"); |
256 } else { |
254 } else { |
257 if(Printer.debug)Printer.debug("not using JDK 1.2 security"); |
255 if(Printer.debug)Printer.debug("not using JDK 1.2 security"); |
258 thread = createThreadImpl(runnable, threadName, isDaemon, priority, |
256 thread = createThreadImpl(runnable, threadName, isDaemon, priority, |
259 doStart); |
257 doStart); |
280 } |
278 } |
281 return thread; |
279 return thread; |
282 } |
280 } |
283 |
281 |
284 |
282 |
285 static List getProviders(final Class providerClass) { |
283 static <T> List<T> getProviders(final Class<T> providerClass) { |
286 List p = new ArrayList(); |
284 List<T> p = new ArrayList<>(); |
287 // Service.providers(Class) just creates "lazy" iterator instance, |
285 // ServiceLoader creates "lazy" iterator instance, so it doesn't, |
288 // so it doesn't require do be called from privileged section |
286 // require do be called from privileged section |
289 final Iterator ps = Service.providers(providerClass); |
287 final Iterator<T> ps = ServiceLoader.load(providerClass).iterator(); |
290 |
288 |
291 // the iterator's hasNext() method looks through classpath for |
289 // the iterator's hasNext() method looks through classpath for |
292 // the provider class names, so it requires read permissions |
290 // the provider class names, so it requires read permissions |
293 PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() { |
291 PrivilegedAction<Boolean> hasNextAction = new PrivilegedAction<Boolean>() { |
294 public Boolean run() { |
292 public Boolean run() { |
299 while (AccessController.doPrivileged(hasNextAction)) { |
297 while (AccessController.doPrivileged(hasNextAction)) { |
300 try { |
298 try { |
301 // the iterator's next() method creates instances of the |
299 // the iterator's next() method creates instances of the |
302 // providers and it should be called in the current security |
300 // providers and it should be called in the current security |
303 // context |
301 // context |
304 Object provider = ps.next(); |
302 T provider = ps.next(); |
305 if (providerClass.isInstance(provider)) { |
303 if (providerClass.isInstance(provider)) { |
306 // $$mp 2003-08-22 |
304 // $$mp 2003-08-22 |
307 // Always adding at the beginning reverses the |
305 // Always adding at the beginning reverses the |
308 // order of the providers. So we no longer have |
306 // order of the providers. So we no longer have |
309 // to do this in AudioSystem and MidiSystem. |
307 // to do this in AudioSystem and MidiSystem. |