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