1 /*
   2  * Copyright (c) 1999, 2013, 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 java.net.URLClassLoader;
  29 import java.net.MalformedURLException;
  30 import java.security.AccessControlContext;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import sun.misc.SharedSecrets;
  34 
  35 final class VersionHelper12 extends VersionHelper {
  36 
  37     // System property to control whether classes may be loaded from an
  38     // arbitrary URL code base.
  39     private static final String TRUST_URL_CODEBASE_PROPERTY =
  40         "com.sun.jndi.ldap.object.trustURLCodebase";
  41 
  42     // Determine whether classes may be loaded from an arbitrary URL code base.
  43     private static final String trustURLCodebase =
  44         AccessController.doPrivileged(
  45             new PrivilegedAction<String>() {
  46                 public String run() {
  47                     return System.getProperty(TRUST_URL_CODEBASE_PROPERTY,
  48                             "false");
  49                 }
  50             }
  51         );
  52 
  53     VersionHelper12() {} // Disallow external from creating one of these.
  54 
  55     ClassLoader getURLClassLoader(String[] url)
  56         throws MalformedURLException {
  57             ClassLoader parent = getContextClassLoader();
  58             /*
  59              * Classes may only be loaded from an arbitrary URL code base when
  60              * the system property com.sun.jndi.ldap.object.trustURLCodebase
  61              * has been set to "true".
  62              */
  63             if (url != null && "true".equalsIgnoreCase(trustURLCodebase)) {
  64                 return URLClassLoader.newInstance(getUrlArray(url), parent);
  65             } else {
  66                 return parent;
  67             }
  68     }
  69 
  70     Class<?> loadClass(String className) throws ClassNotFoundException {
  71         ClassLoader cl = getContextClassLoader();
  72         return Class.forName(className, true, cl);
  73     }
  74 
  75     private ClassLoader getContextClassLoader() {
  76         return AccessController.doPrivileged(
  77             new PrivilegedAction<ClassLoader>() {
  78                 public ClassLoader run() {
  79                     return Thread.currentThread().getContextClassLoader();
  80                 }
  81             }
  82         );
  83     }
  84 
  85     Thread createThread(final Runnable r) {
  86         final AccessControlContext acc = AccessController.getContext();
  87         // 4290486: doPrivileged is needed to create a thread in
  88         // an environment that restricts "modifyThreadGroup".
  89         return AccessController.doPrivileged(
  90                 new PrivilegedAction<Thread>() {
  91                     public Thread run() {
  92                         return SharedSecrets.getJavaLangAccess()
  93                                 .newThreadWithAcc(r, acc);
  94                     }
  95                 }
  96         );
  97     }
  98 }