提交 4ea7c751 编写于 作者: C chegar

7116946: JSSecurityManager should use java.util.ServiceLoader to lookup service providers

Reviewed-by: prr
上级 7adf5e07
...@@ -34,15 +34,13 @@ import java.util.ArrayList; ...@@ -34,15 +34,13 @@ import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.ServiceLoader;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import javax.sound.sampled.AudioPermission; import javax.sound.sampled.AudioPermission;
import sun.misc.Service;
/** Managing security in the Java Sound implementation. /** Managing security in the Java Sound implementation.
* This class contains all code that uses and is used by * This class contains all code that uses and is used by
* SecurityManager.doPrivileged(). * SecurityManager.doPrivileged().
...@@ -80,8 +78,8 @@ class JSSecurityManager { ...@@ -80,8 +78,8 @@ class JSSecurityManager {
try { try {
if (hasSecurityManager()) { if (hasSecurityManager()) {
if(Printer.debug) Printer.debug("using security manager to load library"); if(Printer.debug) Printer.debug("using security manager to load library");
PrivilegedAction action = new PrivilegedAction() { PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
public Object run() { public Void run() {
System.loadLibrary(libName); System.loadLibrary(libName);
return null; return null;
} }
...@@ -104,8 +102,8 @@ class JSSecurityManager { ...@@ -104,8 +102,8 @@ class JSSecurityManager {
if (hasSecurityManager()) { if (hasSecurityManager()) {
if(Printer.debug) Printer.debug("using JDK 1.2 security to get property"); if(Printer.debug) Printer.debug("using JDK 1.2 security to get property");
try{ try{
PrivilegedAction action = new PrivilegedAction() { PrivilegedAction<String> action = new PrivilegedAction<String>() {
public Object run() { public String run() {
try { try {
return System.getProperty(propertyName); return System.getProperty(propertyName);
} catch (Throwable t) { } catch (Throwable t) {
...@@ -113,7 +111,7 @@ class JSSecurityManager { ...@@ -113,7 +111,7 @@ class JSSecurityManager {
} }
} }
}; };
propertyValue = (String) AccessController.doPrivileged(action); propertyValue = AccessController.doPrivileged(action);
} catch( Exception e ) { } catch( Exception e ) {
if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties"); if(Printer.debug) Printer.debug("not using JDK 1.2 security to get properties");
propertyValue = System.getProperty(propertyName); propertyValue = System.getProperty(propertyName);
...@@ -142,8 +140,8 @@ class JSSecurityManager { ...@@ -142,8 +140,8 @@ class JSSecurityManager {
if(hasSecurityManager()) { if(hasSecurityManager()) {
try { try {
// invoke the privileged action using 1.2 security // invoke the privileged action using 1.2 security
PrivilegedAction action = new PrivilegedAction() { PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
public Object run() { public Void run() {
loadPropertiesImpl(properties, filename); loadPropertiesImpl(properties, filename);
return null; return null;
} }
...@@ -197,8 +195,8 @@ class JSSecurityManager { ...@@ -197,8 +195,8 @@ class JSSecurityManager {
if(hasSecurityManager()) { if(hasSecurityManager()) {
try { try {
// invoke the privileged action using 1.2 security // invoke the privileged action using 1.2 security
PrivilegedAction action = new PrivilegedAction() { PrivilegedAction<ThreadGroup> action = new PrivilegedAction<ThreadGroup>() {
public Object run() { public ThreadGroup run() {
try { try {
return getTopmostThreadGroupImpl(); return getTopmostThreadGroupImpl();
} catch (Throwable t) { } catch (Throwable t) {
...@@ -206,7 +204,7 @@ class JSSecurityManager { ...@@ -206,7 +204,7 @@ class JSSecurityManager {
} }
} }
}; };
topmostThreadGroup = (ThreadGroup) AccessController.doPrivileged(action); topmostThreadGroup = AccessController.doPrivileged(action);
if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security"); if(Printer.debug)Printer.debug("Got topmost thread group with JDK 1.2 security");
} catch (Exception e) { } catch (Exception e) {
if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security"); if(Printer.debug)Printer.debug("Exception getting topmost thread group with JDK 1.2 security");
...@@ -240,8 +238,8 @@ class JSSecurityManager { ...@@ -240,8 +238,8 @@ class JSSecurityManager {
final boolean doStart) { final boolean doStart) {
Thread thread = null; Thread thread = null;
if(hasSecurityManager()) { if(hasSecurityManager()) {
PrivilegedAction action = new PrivilegedAction() { PrivilegedAction<Thread> action = new PrivilegedAction<Thread>() {
public Object run() { public Thread run() {
try { try {
return createThreadImpl(runnable, threadName, return createThreadImpl(runnable, threadName,
isDaemon, priority, isDaemon, priority,
...@@ -251,7 +249,7 @@ class JSSecurityManager { ...@@ -251,7 +249,7 @@ class JSSecurityManager {
} }
} }
}; };
thread = (Thread) AccessController.doPrivileged(action); thread = AccessController.doPrivileged(action);
if(Printer.debug) Printer.debug("created thread with JDK 1.2 security"); if(Printer.debug) Printer.debug("created thread with JDK 1.2 security");
} else { } else {
if(Printer.debug)Printer.debug("not using JDK 1.2 security"); if(Printer.debug)Printer.debug("not using JDK 1.2 security");
...@@ -282,11 +280,11 @@ class JSSecurityManager { ...@@ -282,11 +280,11 @@ class JSSecurityManager {
} }
static List getProviders(final Class providerClass) { static <T> List<T> getProviders(final Class<T> providerClass) {
List p = new ArrayList(); List<T> p = new ArrayList<>();
// Service.providers(Class) just creates "lazy" iterator instance, // ServiceLoader creates "lazy" iterator instance, so it doesn't,
// so it doesn't require do be called from privileged section // require do be called from privileged section
final Iterator ps = Service.providers(providerClass); final Iterator<T> ps = ServiceLoader.load(providerClass).iterator();
// the iterator's hasNext() method looks through classpath for // the iterator's hasNext() method looks through classpath for
// the provider class names, so it requires read permissions // the provider class names, so it requires read permissions
...@@ -301,7 +299,7 @@ class JSSecurityManager { ...@@ -301,7 +299,7 @@ class JSSecurityManager {
// the iterator's next() method creates instances of the // the iterator's next() method creates instances of the
// providers and it should be called in the current security // providers and it should be called in the current security
// context // context
Object provider = ps.next(); T provider = ps.next();
if (providerClass.isInstance(provider)) { if (providerClass.isInstance(provider)) {
// $$mp 2003-08-22 // $$mp 2003-08-22
// Always adding at the beginning reverses the // Always adding at the beginning reverses the
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册