src/java.naming/share/classes/com/sun/jndi/ldap/VersionHelper.java
changeset 47216 71c04702a3d5
parent 32834 e1dca5fe4de3
child 52427 3c6aa484536c
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
       
     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
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    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  *
       
    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.
       
    24  */
       
    25 
       
    26 package com.sun.jndi.ldap;
       
    27 
       
    28 import jdk.internal.misc.SharedSecrets;
       
    29 
       
    30 import java.net.MalformedURLException;
       
    31 import java.net.URL;
       
    32 import java.net.URLClassLoader;
       
    33 import java.security.AccessControlContext;
       
    34 import java.security.AccessController;
       
    35 import java.security.PrivilegedAction;
       
    36 
       
    37 public final class VersionHelper {
       
    38 
       
    39     private static final VersionHelper helper = new VersionHelper();
       
    40 
       
    41     /**
       
    42      * Determines whether classes may be loaded from an arbitrary URL code base.
       
    43      */
       
    44     private static final boolean trustURLCodebase;
       
    45 
       
    46     static {
       
    47         // System property to control whether classes may be loaded from an
       
    48         // arbitrary URL code base
       
    49         PrivilegedAction<String> act =
       
    50                 () -> System.getProperty("com.sun.jndi.ldap.object.trustURLCodebase", "false");
       
    51         String trust = AccessController.doPrivileged(act);
       
    52         trustURLCodebase = "true".equalsIgnoreCase(trust);
       
    53     }
       
    54 
       
    55     private VersionHelper() { }
       
    56 
       
    57     static VersionHelper getVersionHelper() {
       
    58         return helper;
       
    59     }
       
    60 
       
    61     ClassLoader getURLClassLoader(String[] url) throws MalformedURLException {
       
    62         ClassLoader parent = getContextClassLoader();
       
    63         /*
       
    64          * Classes may only be loaded from an arbitrary URL code base when
       
    65          * the system property com.sun.jndi.ldap.object.trustURLCodebase
       
    66          * has been set to "true".
       
    67          */
       
    68         if (url != null && trustURLCodebase) {
       
    69             return URLClassLoader.newInstance(getUrlArray(url), parent);
       
    70         } else {
       
    71             return parent;
       
    72         }
       
    73     }
       
    74 
       
    75     Class<?> loadClass(String className) throws ClassNotFoundException {
       
    76         return Class.forName(className, true, getContextClassLoader());
       
    77     }
       
    78 
       
    79     Thread createThread(Runnable r) {
       
    80         AccessControlContext acc = AccessController.getContext();
       
    81         // 4290486: doPrivileged is needed to create a thread in
       
    82         // an environment that restricts "modifyThreadGroup".
       
    83         PrivilegedAction<Thread> act =
       
    84                 () -> SharedSecrets.getJavaLangAccess().newThreadWithAcc(r, acc);
       
    85         return AccessController.doPrivileged(act);
       
    86     }
       
    87 
       
    88     private ClassLoader getContextClassLoader() {
       
    89         PrivilegedAction<ClassLoader> act =
       
    90                 Thread.currentThread()::getContextClassLoader;
       
    91         return AccessController.doPrivileged(act);
       
    92     }
       
    93 
       
    94     private static URL[] getUrlArray(String[] url) throws MalformedURLException {
       
    95         URL[] urlArray = new URL[url.length];
       
    96         for (int i = 0; i < urlArray.length; i++) {
       
    97             urlArray[i] = new URL(url[i]);
       
    98         }
       
    99         return urlArray;
       
   100     }
       
   101 }