VersionHelper12.java 8.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 */

package com.sun.naming.internal;

import java.io.InputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URLClassLoader;
import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import java.util.Properties;

import javax.naming.*;

/**
 * VersionHelper was used by JNDI to accommodate differences between
 * JDK 1.1.x and the Java 2 platform. As this is no longer necessary
 * since JNDI's inclusion in the platform, this class currently
 * serves as a set of utilities for performing system-level things,
 * such as class-loading and reading system properties.
 *
 * @author Rosanna Lee
 * @author Scott Seligman
 */

final class VersionHelper12 extends VersionHelper {

X
xuelei 已提交
56 57 58
    // Disallow external from creating one of these.
    VersionHelper12() {
    }
D
duke 已提交
59

60
    public Class<?> loadClass(String className) throws ClassNotFoundException {
X
xuelei 已提交
61
        return loadClass(className, getContextClassLoader());
D
duke 已提交
62 63 64
    }

    /**
X
xuelei 已提交
65 66 67 68 69
     * Package private.
     *
     * This internal method is used with Thread Context Class Loader (TCCL),
     * please don't expose this method as public.
     */
70
    Class<?> loadClass(String className, ClassLoader cl)
D
duke 已提交
71
        throws ClassNotFoundException {
X
xuelei 已提交
72 73
        Class<?> cls = Class.forName(className, true, cl);
        return cls;
D
duke 已提交
74 75 76 77 78 79
    }

    /**
     * @param className A non-null fully qualified class name.
     * @param codebase A non-null, space-separated list of URL strings.
     */
80
    public Class<?> loadClass(String className, String codebase)
X
xuelei 已提交
81
            throws ClassNotFoundException, MalformedURLException {
D
duke 已提交
82 83

        ClassLoader parent = getContextClassLoader();
X
xuelei 已提交
84 85
        ClassLoader cl =
                 URLClassLoader.newInstance(getUrlArray(codebase), parent);
D
duke 已提交
86

X
xuelei 已提交
87 88 89
        return loadClass(className, cl);
    }

D
duke 已提交
90
    String getJndiProperty(final int i) {
91 92 93
        return AccessController.doPrivileged(
            new PrivilegedAction<String>() {
                public String run() {
D
duke 已提交
94 95 96 97 98 99 100 101 102 103 104
                    try {
                        return System.getProperty(PROPS[i]);
                    } catch (SecurityException e) {
                        return null;
                    }
                }
            }
        );
    }

    String[] getJndiProperties() {
105 106 107
        Properties sysProps = AccessController.doPrivileged(
            new PrivilegedAction<Properties>() {
                public Properties run() {
D
duke 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    try {
                        return System.getProperties();
                    } catch (SecurityException e) {
                        return null;
                    }
                }
            }
        );
        if (sysProps == null) {
            return null;
        }
        String[] jProps = new String[PROPS.length];
        for (int i = 0; i < PROPS.length; i++) {
            jProps[i] = sysProps.getProperty(PROPS[i]);
        }
        return jProps;
    }

126 127 128 129
    InputStream getResourceAsStream(final Class<?> c, final String name) {
        return AccessController.doPrivileged(
            new PrivilegedAction<InputStream>() {
                public InputStream run() {
D
duke 已提交
130 131 132 133 134 135 136
                    return c.getResourceAsStream(name);
                }
            }
        );
    }

    InputStream getJavaHomeLibStream(final String filename) {
137 138 139
        return AccessController.doPrivileged(
            new PrivilegedAction<InputStream>() {
                public InputStream run() {
D
duke 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
                    try {
                        String javahome = System.getProperty("java.home");
                        if (javahome == null) {
                            return null;
                        }
                        String pathname = javahome + java.io.File.separator +
                            "lib" + java.io.File.separator + filename;
                        return new java.io.FileInputStream(pathname);
                    } catch (Exception e) {
                        return null;
                    }
                }
            }
        );
    }

156 157 158
    NamingEnumeration<InputStream> getResources(final ClassLoader cl,
            final String name) throws IOException {
        Enumeration<URL> urls;
D
duke 已提交
159
        try {
160 161 162
            urls = AccessController.doPrivileged(
                new PrivilegedExceptionAction<Enumeration<URL>>() {
                    public Enumeration<URL> run() throws IOException {
D
duke 已提交
163 164 165 166 167 168 169 170 171 172 173 174
                        return (cl == null)
                            ? ClassLoader.getSystemResources(name)
                            : cl.getResources(name);
                    }
                }
            );
        } catch (PrivilegedActionException e) {
            throw (IOException)e.getException();
        }
        return new InputStreamEnumeration(urls);
    }

X
xuelei 已提交
175 176 177
    /**
     * Package private.
     *
178 179
     * This internal method returns Thread Context Class Loader (TCCL),
     * if null, returns the system Class Loader.
X
xuelei 已提交
180
     *
181
     * Please don't expose this method as public.
X
xuelei 已提交
182
     */
D
duke 已提交
183
    ClassLoader getContextClassLoader() {
X
xuelei 已提交
184

185 186 187
        return AccessController.doPrivileged(
            new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
188 189 190 191 192 193 194 195
                    ClassLoader loader =
                            Thread.currentThread().getContextClassLoader();
                    if (loader == null) {
                        // Don't use bootstrap class loader directly!
                        loader = ClassLoader.getSystemClassLoader();
                    }

                    return loader;
D
duke 已提交
196 197 198 199 200 201 202 203 204 205 206 207
                }
            }
        );
    }

    /**
     * Given an enumeration of URLs, an instance of this class represents
     * an enumeration of their InputStreams.  Each operation on the URL
     * enumeration is performed within a doPrivileged block.
     * This is used to enumerate the resources under a foreign codebase.
     * This class is not MT-safe.
     */
208
    class InputStreamEnumeration implements NamingEnumeration<InputStream> {
D
duke 已提交
209

210
        private final Enumeration<URL> urls;
D
duke 已提交
211

212
        private InputStream nextElement = null;
D
duke 已提交
213

214
        InputStreamEnumeration(Enumeration<URL> urls) {
D
duke 已提交
215 216 217 218 219 220 221
            this.urls = urls;
        }

        /*
         * Returns the next InputStream, or null if there are no more.
         * An InputStream that cannot be opened is skipped.
         */
222
        private InputStream getNextElement() {
D
duke 已提交
223
            return AccessController.doPrivileged(
224 225
                new PrivilegedAction<InputStream>() {
                    public InputStream run() {
D
duke 已提交
226 227
                        while (urls.hasMoreElements()) {
                            try {
228
                                return urls.nextElement().openStream();
D
duke 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
                            } catch (IOException e) {
                                // skip this URL
                            }
                        }
                        return null;
                    }
                }
            );
        }

        public boolean hasMore() {
            if (nextElement != null) {
                return true;
            }
            nextElement = getNextElement();
            return (nextElement != null);
        }

        public boolean hasMoreElements() {
            return hasMore();
        }

251
        public InputStream next() {
D
duke 已提交
252
            if (hasMore()) {
253
                InputStream res = nextElement;
D
duke 已提交
254 255 256 257 258 259 260
                nextElement = null;
                return res;
            } else {
                throw new NoSuchElementException();
            }
        }

261
        public InputStream nextElement() {
D
duke 已提交
262 263 264 265 266 267 268
            return next();
        }

        public void close() {
        }
    }
}