Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/naming/internal/VersionHelper12.java
38923 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.naming.internal;2627import java.io.InputStream;28import java.io.IOException;29import java.net.MalformedURLException;30import java.net.URLClassLoader;31import java.net.URL;32import java.security.AccessController;33import java.security.PrivilegedAction;34import java.security.PrivilegedActionException;35import java.security.PrivilegedExceptionAction;36import java.util.Enumeration;37import java.util.NoSuchElementException;38import java.util.Properties;3940import javax.naming.*;4142/**43* VersionHelper was used by JNDI to accommodate differences between44* JDK 1.1.x and the Java 2 platform. As this is no longer necessary45* since JNDI's inclusion in the platform, this class currently46* serves as a set of utilities for performing system-level things,47* such as class-loading and reading system properties.48*49* @author Rosanna Lee50* @author Scott Seligman51*/5253final class VersionHelper12 extends VersionHelper {5455// Disallow external from creating one of these.56VersionHelper12() {57}5859public Class<?> loadClass(String className) throws ClassNotFoundException {60return loadClass(className, getContextClassLoader());61}6263public Class<?> loadClassWithoutInit(String className) throws ClassNotFoundException {64return loadClass(className, false, getContextClassLoader());65}6667/**68* Determines whether classes may be loaded from an arbitrary URL code base.69*/70private static final String TRUST_URL_CODEBASE_PROPERTY =71"com.sun.jndi.ldap.object.trustURLCodebase";72private static final String trustURLCodebase =73AccessController.doPrivileged(74new PrivilegedAction<String>() {75public String run() {76try {77return System.getProperty(TRUST_URL_CODEBASE_PROPERTY,78"false");79} catch (SecurityException e) {80return "false";81}82}83}84);8586/**87* Package private.88*89* This internal method is used with Thread Context Class Loader (TCCL),90* please don't expose this method as public.91*/92Class<?> loadClass(String className, boolean initialize, ClassLoader cl)93throws ClassNotFoundException {94Class<?> cls = Class.forName(className, initialize, cl);95return cls;96}9798Class<?> loadClass(String className, ClassLoader cl)99throws ClassNotFoundException {100return loadClass(className, true, cl);101}102103/**104* @param className A non-null fully qualified class name.105* @param codebase A non-null, space-separated list of URL strings.106*/107public Class<?> loadClass(String className, String codebase)108throws ClassNotFoundException, MalformedURLException {109if ("true".equalsIgnoreCase(trustURLCodebase)) {110ClassLoader parent = getContextClassLoader();111ClassLoader cl =112URLClassLoader.newInstance(getUrlArray(codebase), parent);113114return loadClass(className, cl);115} else {116return null;117}118}119120String getJndiProperty(final int i) {121return AccessController.doPrivileged(122new PrivilegedAction<String>() {123public String run() {124try {125return System.getProperty(PROPS[i]);126} catch (SecurityException e) {127return null;128}129}130}131);132}133134String[] getJndiProperties() {135Properties sysProps = AccessController.doPrivileged(136new PrivilegedAction<Properties>() {137public Properties run() {138try {139return System.getProperties();140} catch (SecurityException e) {141return null;142}143}144}145);146if (sysProps == null) {147return null;148}149String[] jProps = new String[PROPS.length];150for (int i = 0; i < PROPS.length; i++) {151jProps[i] = sysProps.getProperty(PROPS[i]);152}153return jProps;154}155156InputStream getResourceAsStream(final Class<?> c, final String name) {157return AccessController.doPrivileged(158new PrivilegedAction<InputStream>() {159public InputStream run() {160return c.getResourceAsStream(name);161}162}163);164}165166InputStream getJavaHomeLibStream(final String filename) {167return AccessController.doPrivileged(168new PrivilegedAction<InputStream>() {169public InputStream run() {170try {171String javahome = System.getProperty("java.home");172if (javahome == null) {173return null;174}175String pathname = javahome + java.io.File.separator +176"lib" + java.io.File.separator + filename;177return new java.io.FileInputStream(pathname);178} catch (Exception e) {179return null;180}181}182}183);184}185186NamingEnumeration<InputStream> getResources(final ClassLoader cl,187final String name) throws IOException {188Enumeration<URL> urls;189try {190urls = AccessController.doPrivileged(191new PrivilegedExceptionAction<Enumeration<URL>>() {192public Enumeration<URL> run() throws IOException {193return (cl == null)194? ClassLoader.getSystemResources(name)195: cl.getResources(name);196}197}198);199} catch (PrivilegedActionException e) {200throw (IOException)e.getException();201}202return new InputStreamEnumeration(urls);203}204205/**206* Package private.207*208* This internal method returns Thread Context Class Loader (TCCL),209* if null, returns the system Class Loader.210*211* Please don't expose this method as public.212*/213ClassLoader getContextClassLoader() {214215return AccessController.doPrivileged(216new PrivilegedAction<ClassLoader>() {217public ClassLoader run() {218ClassLoader loader =219Thread.currentThread().getContextClassLoader();220if (loader == null) {221// Don't use bootstrap class loader directly!222loader = ClassLoader.getSystemClassLoader();223}224225return loader;226}227}228);229}230231/**232* Given an enumeration of URLs, an instance of this class represents233* an enumeration of their InputStreams. Each operation on the URL234* enumeration is performed within a doPrivileged block.235* This is used to enumerate the resources under a foreign codebase.236* This class is not MT-safe.237*/238class InputStreamEnumeration implements NamingEnumeration<InputStream> {239240private final Enumeration<URL> urls;241242private InputStream nextElement = null;243244InputStreamEnumeration(Enumeration<URL> urls) {245this.urls = urls;246}247248/*249* Returns the next InputStream, or null if there are no more.250* An InputStream that cannot be opened is skipped.251*/252private InputStream getNextElement() {253return AccessController.doPrivileged(254new PrivilegedAction<InputStream>() {255public InputStream run() {256while (urls.hasMoreElements()) {257try {258return urls.nextElement().openStream();259} catch (IOException e) {260// skip this URL261}262}263return null;264}265}266);267}268269public boolean hasMore() {270if (nextElement != null) {271return true;272}273nextElement = getNextElement();274return (nextElement != null);275}276277public boolean hasMoreElements() {278return hasMore();279}280281public InputStream next() {282if (hasMore()) {283InputStream res = nextElement;284nextElement = null;285return res;286} else {287throw new NoSuchElementException();288}289}290291public InputStream nextElement() {292return next();293}294295public void close() {296}297}298}299300301