提交 c7514743 编写于 作者: W weijun

8013739: Better LDAP resource management

Reviewed-by: ahgross, mchung, xuelei
上级 61f17936
...@@ -25,11 +25,12 @@ ...@@ -25,11 +25,12 @@
package com.sun.jndi.ldap; package com.sun.jndi.ldap;
import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.security.AccessControlContext;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import sun.misc.SharedSecrets;
final class VersionHelper12 extends VersionHelper { final class VersionHelper12 extends VersionHelper {
...@@ -82,12 +83,16 @@ final class VersionHelper12 extends VersionHelper { ...@@ -82,12 +83,16 @@ final class VersionHelper12 extends VersionHelper {
} }
Thread createThread(final Runnable r) { Thread createThread(final Runnable r) {
final AccessControlContext acc = AccessController.getContext();
// 4290486: doPrivileged is needed to create a thread in
// an environment that restricts "modifyThreadGroup".
return AccessController.doPrivileged( return AccessController.doPrivileged(
new PrivilegedAction<Thread>() { new PrivilegedAction<Thread>() {
public Thread run() { public Thread run() {
return new Thread(r); return SharedSecrets.getJavaLangAccess()
.newThreadWithAcc(r, acc);
}
} }
}
); );
} }
} }
...@@ -26,6 +26,7 @@ package java.lang; ...@@ -26,6 +26,7 @@ package java.lang;
import java.io.*; import java.io.*;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.security.AccessControlContext;
import java.util.Properties; import java.util.Properties;
import java.util.PropertyPermission; import java.util.PropertyPermission;
import java.util.StringTokenizer; import java.util.StringTokenizer;
...@@ -1251,6 +1252,9 @@ public final class System { ...@@ -1251,6 +1252,9 @@ public final class System {
public String newStringUnsafe(char[] chars) { public String newStringUnsafe(char[] chars) {
return new String(chars, true); return new String(chars, true);
} }
public Thread newThreadWithAcc(Runnable target, AccessControlContext acc) {
return new Thread(target, acc);
}
}); });
} }
} }
...@@ -340,6 +340,15 @@ class Thread implements Runnable { ...@@ -340,6 +340,15 @@ class Thread implements Runnable {
sleep(millis); sleep(millis);
} }
/**
* Initializes a Thread with the current AccessControlContext.
* @see #init(ThreadGroup,Runnable,String,long,AccessControlContext)
*/
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null);
}
/** /**
* Initializes a Thread. * Initializes a Thread.
* *
...@@ -348,9 +357,11 @@ class Thread implements Runnable { ...@@ -348,9 +357,11 @@ class Thread implements Runnable {
* @param name the name of the new Thread * @param name the name of the new Thread
* @param stackSize the desired stack size for the new thread, or * @param stackSize the desired stack size for the new thread, or
* zero to indicate that this parameter is to be ignored. * zero to indicate that this parameter is to be ignored.
* @param acc the AccessControlContext to inherit, or
* AccessController.getContext() if null
*/ */
private void init(ThreadGroup g, Runnable target, String name, private void init(ThreadGroup g, Runnable target, String name,
long stackSize) { long stackSize, AccessControlContext acc) {
if (name == null) { if (name == null) {
throw new NullPointerException("name cannot be null"); throw new NullPointerException("name cannot be null");
} }
...@@ -396,7 +407,8 @@ class Thread implements Runnable { ...@@ -396,7 +407,8 @@ class Thread implements Runnable {
this.contextClassLoader = parent.getContextClassLoader(); this.contextClassLoader = parent.getContextClassLoader();
else else
this.contextClassLoader = parent.contextClassLoader; this.contextClassLoader = parent.contextClassLoader;
this.inheritedAccessControlContext = AccessController.getContext(); this.inheritedAccessControlContext =
acc != null ? acc : AccessController.getContext();
this.target = target; this.target = target;
setPriority(priority); setPriority(priority);
if (parent.inheritableThreadLocals != null) if (parent.inheritableThreadLocals != null)
...@@ -448,6 +460,14 @@ class Thread implements Runnable { ...@@ -448,6 +460,14 @@ class Thread implements Runnable {
init(null, target, "Thread-" + nextThreadNum(), 0); init(null, target, "Thread-" + nextThreadNum(), 0);
} }
/**
* Creates a new Thread that inherits the given AccessControlContext.
* This is not a public constructor.
*/
Thread(Runnable target, AccessControlContext acc) {
init(null, target, "Thread-" + nextThreadNum(), 0, acc);
}
/** /**
* Allocates a new {@code Thread} object. This constructor has the same * Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread} * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
......
...@@ -27,6 +27,8 @@ package sun.misc; ...@@ -27,6 +27,8 @@ package sun.misc;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.reflect.Executable; import java.lang.reflect.Executable;
import java.security.AccessControlContext;
import sun.reflect.ConstantPool; import sun.reflect.ConstantPool;
import sun.reflect.annotation.AnnotationType; import sun.reflect.annotation.AnnotationType;
import sun.nio.ch.Interruptible; import sun.nio.ch.Interruptible;
...@@ -107,4 +109,10 @@ public interface JavaLangAccess { ...@@ -107,4 +109,10 @@ public interface JavaLangAccess {
* @return a newly created string whose content is the character array * @return a newly created string whose content is the character array
*/ */
String newStringUnsafe(char[] chars); String newStringUnsafe(char[] chars);
/**
* Returns a new Thread with the given Runnable and an
* inherited AccessControlContext.
*/
Thread newThreadWithAcc(Runnable target, AccessControlContext acc);
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册