changeset 6659:600107f4664d

8013739: Better LDAP resource management Reviewed-by: ahgross, mchung, xuelei
author weijun
date Thu, 13 Jun 2013 10:21:06 +0800
parents 18479f069dab
children e074eb78c8fd
files src/share/classes/com/sun/jndi/ldap/VersionHelper12.java src/share/classes/java/lang/System.java src/share/classes/java/lang/Thread.java src/share/classes/sun/misc/JavaLangAccess.java
diffstat 4 files changed, 44 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/jndi/ldap/VersionHelper12.java	Wed Jun 12 09:51:44 2013 -0700
+++ b/src/share/classes/com/sun/jndi/ldap/VersionHelper12.java	Thu Jun 13 10:21:06 2013 +0800
@@ -25,11 +25,12 @@
 
 package com.sun.jndi.ldap;
 
-import java.net.URL;
 import java.net.URLClassLoader;
 import java.net.MalformedURLException;
+import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import sun.misc.SharedSecrets;
 
 final class VersionHelper12 extends VersionHelper {
 
@@ -82,12 +83,16 @@
     }
 
     Thread createThread(final Runnable r) {
-        return (Thread) AccessController.doPrivileged(
-            new PrivilegedAction() {
-                public Object run() {
-                    return new Thread(r);
+        final AccessControlContext acc = AccessController.getContext();
+        // 4290486: doPrivileged is needed to create a thread in
+        // an environment that restricts "modifyThreadGroup".
+        return AccessController.doPrivileged(
+                new PrivilegedAction<Thread>() {
+                    public Thread run() {
+                        return SharedSecrets.getJavaLangAccess()
+                                .newThreadWithAcc(r, acc);
+                    }
                 }
-            }
         );
     }
 }
--- a/src/share/classes/java/lang/System.java	Wed Jun 12 09:51:44 2013 -0700
+++ b/src/share/classes/java/lang/System.java	Thu Jun 13 10:21:06 2013 +0800
@@ -25,6 +25,7 @@
 package java.lang;
 
 import java.io.*;
+import java.security.AccessControlContext;
 import java.util.Properties;
 import java.util.PropertyPermission;
 import java.util.StringTokenizer;
@@ -1202,6 +1203,9 @@
             public int getStringHash32(String string) {
                 return string.hash32();
             }
+            public Thread newThreadWithAcc(Runnable target, AccessControlContext acc) {
+                return new Thread(target, acc);
+            }
         });
     }
 }
--- a/src/share/classes/java/lang/Thread.java	Wed Jun 12 09:51:44 2013 -0700
+++ b/src/share/classes/java/lang/Thread.java	Thu Jun 13 10:21:06 2013 +0800
@@ -341,6 +341,15 @@
     }
 
     /**
+     * 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.
      *
      * @param g the Thread group
@@ -348,9 +357,11 @@
      * @param name the name of the new Thread
      * @param stackSize the desired stack size for the new thread, or
      *        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,
-                      long stackSize) {
+                      long stackSize, AccessControlContext acc) {
         if (name == null) {
             throw new NullPointerException("name cannot be null");
         }
@@ -396,7 +407,8 @@
             this.contextClassLoader = parent.getContextClassLoader();
         else
             this.contextClassLoader = parent.contextClassLoader;
-        this.inheritedAccessControlContext = AccessController.getContext();
+        this.inheritedAccessControlContext =
+                acc != null ? acc : AccessController.getContext();
         this.target = target;
         setPriority(priority);
         if (parent.inheritableThreadLocals != null)
@@ -449,6 +461,14 @@
     }
 
     /**
+     * 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
      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
      * {@code (group, target, gname)} ,where {@code gname} is a newly generated
--- a/src/share/classes/sun/misc/JavaLangAccess.java	Wed Jun 12 09:51:44 2013 -0700
+++ b/src/share/classes/sun/misc/JavaLangAccess.java	Thu Jun 13 10:21:06 2013 +0800
@@ -25,6 +25,7 @@
 
 package sun.misc;
 
+import java.security.AccessControlContext;
 import sun.reflect.ConstantPool;
 import sun.reflect.annotation.AnnotationType;
 import sun.nio.ch.Interruptible;
@@ -88,4 +89,10 @@
      * Returns the murmur hash value for the specified String.
      */
     int getStringHash32(String string);
+
+    /**
+     * Returns a new Thread with the given Runnable and an
+     * inherited AccessControlContext.
+     */
+    Thread newThreadWithAcc(Runnable target, AccessControlContext acc);
 }