0N/A/*
2362N/A * Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.naming.internal;
0N/A
0N/Aimport java.io.InputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URLClassLoader;
0N/Aimport java.net.URL;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.PrivilegedActionException;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport java.util.Enumeration;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport java.util.Properties;
0N/A
0N/Aimport javax.naming.*;
0N/A
0N/A/**
0N/A * VersionHelper was used by JNDI to accommodate differences between
0N/A * JDK 1.1.x and the Java 2 platform. As this is no longer necessary
0N/A * since JNDI's inclusion in the platform, this class currently
0N/A * serves as a set of utilities for performing system-level things,
0N/A * such as class-loading and reading system properties.
0N/A *
0N/A * @author Rosanna Lee
0N/A * @author Scott Seligman
0N/A */
0N/A
0N/Afinal class VersionHelper12 extends VersionHelper {
0N/A
0N/A private boolean getSystemPropsFailed = false;
0N/A
0N/A VersionHelper12() {} // Disallow external from creating one of these.
0N/A
0N/A public Class loadClass(String className) throws ClassNotFoundException {
0N/A ClassLoader cl = getContextClassLoader();
0N/A return Class.forName(className, true, cl);
0N/A }
0N/A
0N/A /**
0N/A * Package private.
0N/A */
0N/A Class loadClass(String className, ClassLoader cl)
0N/A throws ClassNotFoundException {
0N/A return Class.forName(className, true, cl);
0N/A }
0N/A
0N/A /**
0N/A * @param className A non-null fully qualified class name.
0N/A * @param codebase A non-null, space-separated list of URL strings.
0N/A */
0N/A public Class loadClass(String className, String codebase)
0N/A throws ClassNotFoundException, MalformedURLException {
0N/A ClassLoader cl;
0N/A
0N/A ClassLoader parent = getContextClassLoader();
0N/A cl = URLClassLoader.newInstance(getUrlArray(codebase), parent);
0N/A
0N/A return Class.forName(className, true, cl);
0N/A }
0N/A
0N/A String getJndiProperty(final int i) {
0N/A return (String) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A try {
0N/A return System.getProperty(PROPS[i]);
0N/A } catch (SecurityException e) {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A String[] getJndiProperties() {
0N/A if (getSystemPropsFailed) {
0N/A return null; // after one failure, don't bother trying again
0N/A }
0N/A Properties sysProps = (Properties) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A try {
0N/A return System.getProperties();
0N/A } catch (SecurityException e) {
0N/A getSystemPropsFailed = true;
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A );
0N/A if (sysProps == null) {
0N/A return null;
0N/A }
0N/A String[] jProps = new String[PROPS.length];
0N/A for (int i = 0; i < PROPS.length; i++) {
0N/A jProps[i] = sysProps.getProperty(PROPS[i]);
0N/A }
0N/A return jProps;
0N/A }
0N/A
0N/A InputStream getResourceAsStream(final Class c, final String name) {
0N/A return (InputStream) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A return c.getResourceAsStream(name);
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A InputStream getJavaHomeLibStream(final String filename) {
0N/A return (InputStream) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A try {
0N/A String javahome = System.getProperty("java.home");
0N/A if (javahome == null) {
0N/A return null;
0N/A }
0N/A String pathname = javahome + java.io.File.separator +
0N/A "lib" + java.io.File.separator + filename;
0N/A return new java.io.FileInputStream(pathname);
0N/A } catch (Exception e) {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A NamingEnumeration getResources(final ClassLoader cl, final String name)
0N/A throws IOException
0N/A {
0N/A Enumeration urls;
0N/A try {
0N/A urls = (Enumeration) AccessController.doPrivileged(
0N/A new PrivilegedExceptionAction() {
0N/A public Object run() throws IOException {
0N/A return (cl == null)
0N/A ? ClassLoader.getSystemResources(name)
0N/A : cl.getResources(name);
0N/A }
0N/A }
0N/A );
0N/A } catch (PrivilegedActionException e) {
0N/A throw (IOException)e.getException();
0N/A }
0N/A return new InputStreamEnumeration(urls);
0N/A }
0N/A
0N/A ClassLoader getContextClassLoader() {
0N/A return (ClassLoader) AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A return Thread.currentThread().getContextClassLoader();
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Given an enumeration of URLs, an instance of this class represents
0N/A * an enumeration of their InputStreams. Each operation on the URL
0N/A * enumeration is performed within a doPrivileged block.
0N/A * This is used to enumerate the resources under a foreign codebase.
0N/A * This class is not MT-safe.
0N/A */
0N/A class InputStreamEnumeration implements NamingEnumeration {
0N/A
0N/A private final Enumeration urls;
0N/A
0N/A private Object nextElement = null;
0N/A
0N/A InputStreamEnumeration(Enumeration urls) {
0N/A this.urls = urls;
0N/A }
0N/A
0N/A /*
0N/A * Returns the next InputStream, or null if there are no more.
0N/A * An InputStream that cannot be opened is skipped.
0N/A */
0N/A private Object getNextElement() {
0N/A return AccessController.doPrivileged(
0N/A new PrivilegedAction() {
0N/A public Object run() {
0N/A while (urls.hasMoreElements()) {
0N/A try {
0N/A return ((URL)urls.nextElement()).openStream();
0N/A } catch (IOException e) {
0N/A // skip this URL
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A );
0N/A }
0N/A
0N/A public boolean hasMore() {
0N/A if (nextElement != null) {
0N/A return true;
0N/A }
0N/A nextElement = getNextElement();
0N/A return (nextElement != null);
0N/A }
0N/A
0N/A public boolean hasMoreElements() {
0N/A return hasMore();
0N/A }
0N/A
0N/A public Object next() {
0N/A if (hasMore()) {
0N/A Object res = nextElement;
0N/A nextElement = null;
0N/A return res;
0N/A } else {
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A
0N/A public Object nextElement() {
0N/A return next();
0N/A }
0N/A
0N/A public void close() {
0N/A }
0N/A }
0N/A}