VersionHelper12.java 10.2 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
 */

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.*;
X
xuelei 已提交
42
import sun.reflect.misc.ReflectUtil;
D
duke 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56

/**
 * 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 已提交
57 58 59 60 61 62 63 64 65 66 67
    // workaround to disable additional package access control with
    // Thread Context Class Loader (TCCL).
    private final static boolean noPackageAccessWithTCCL = "true".equals(
        AccessController.doPrivileged(
            new PrivilegedAction<String>() {
                public String run() {
                    return System.getProperty(
                        "com.sun.naming.untieAccessContextWithTCCL");
                }
            }
        ));
D
duke 已提交
68

X
xuelei 已提交
69 70 71
    // Disallow external from creating one of these.
    VersionHelper12() {
    }
D
duke 已提交
72

73
    public Class<?> loadClass(String className) throws ClassNotFoundException {
X
xuelei 已提交
74
        return loadClass(className, getContextClassLoader());
D
duke 已提交
75 76 77
    }

    /**
X
xuelei 已提交
78 79 80 81 82
     * Package private.
     *
     * This internal method is used with Thread Context Class Loader (TCCL),
     * please don't expose this method as public.
     */
83
    Class<?> loadClass(String className, ClassLoader cl)
D
duke 已提交
84
        throws ClassNotFoundException {
X
xuelei 已提交
85 86 87 88 89
        Class<?> cls = Class.forName(className, true, cl);
        if (!noPackageAccessWithTCCL) {
            checkPackageAccess(cls);
        }
        return cls;
D
duke 已提交
90 91 92 93 94 95
    }

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

        ClassLoader parent = getContextClassLoader();
X
xuelei 已提交
100 101
        ClassLoader cl =
                 URLClassLoader.newInstance(getUrlArray(codebase), parent);
D
duke 已提交
102

X
xuelei 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
        return loadClass(className, cl);
    }

    /**
     * check package access of a class that is loaded with Thread Context
     * Class Loader (TCCL).
     *
     * Similar to java.lang.ClassLoader.checkPackageAccess()
     */
    static void checkPackageAccess(Class<?> cls) {
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            if (ReflectUtil.isNonPublicProxyClass(cls)) {
                for (Class<?> intf: cls.getInterfaces()) {
                    checkPackageAccess(intf);
                }
                return;
            }

            final String name = cls.getName();
            final int i = name.lastIndexOf('.');
            if (i != -1) {
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    public Void run() {
                        sm.checkPackageAccess(name.substring(0, i));
                        return null;
                    }
                }, AccessController.getContext());
            }
        }
D
duke 已提交
133 134 135
    }

    String getJndiProperty(final int i) {
136 137 138
        return AccessController.doPrivileged(
            new PrivilegedAction<String>() {
                public String run() {
D
duke 已提交
139 140 141 142 143 144 145 146 147 148 149
                    try {
                        return System.getProperty(PROPS[i]);
                    } catch (SecurityException e) {
                        return null;
                    }
                }
            }
        );
    }

    String[] getJndiProperties() {
150 151 152
        Properties sysProps = AccessController.doPrivileged(
            new PrivilegedAction<Properties>() {
                public Properties run() {
D
duke 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                    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;
    }

171 172 173 174
    InputStream getResourceAsStream(final Class<?> c, final String name) {
        return AccessController.doPrivileged(
            new PrivilegedAction<InputStream>() {
                public InputStream run() {
D
duke 已提交
175 176 177 178 179 180 181
                    return c.getResourceAsStream(name);
                }
            }
        );
    }

    InputStream getJavaHomeLibStream(final String filename) {
182 183 184
        return AccessController.doPrivileged(
            new PrivilegedAction<InputStream>() {
                public InputStream run() {
D
duke 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                    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;
                    }
                }
            }
        );
    }

201 202 203
    NamingEnumeration<InputStream> getResources(final ClassLoader cl,
            final String name) throws IOException {
        Enumeration<URL> urls;
D
duke 已提交
204
        try {
205 206 207
            urls = AccessController.doPrivileged(
                new PrivilegedExceptionAction<Enumeration<URL>>() {
                    public Enumeration<URL> run() throws IOException {
D
duke 已提交
208 209 210 211 212 213 214 215 216 217 218 219
                        return (cl == null)
                            ? ClassLoader.getSystemResources(name)
                            : cl.getResources(name);
                    }
                }
            );
        } catch (PrivilegedActionException e) {
            throw (IOException)e.getException();
        }
        return new InputStreamEnumeration(urls);
    }

X
xuelei 已提交
220 221 222 223 224 225 226 227 228
    /**
     * Package private.
     *
     * This internal method makes use of Thread Context Class Loader (TCCL),
     * please don't expose this method as public.
     *
     * Please take care of package access control on the current context
     * whenever using TCCL.
     */
D
duke 已提交
229
    ClassLoader getContextClassLoader() {
X
xuelei 已提交
230

231 232 233
        return AccessController.doPrivileged(
            new PrivilegedAction<ClassLoader>() {
                public ClassLoader run() {
D
duke 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246
                    return Thread.currentThread().getContextClassLoader();
                }
            }
        );
    }

    /**
     * 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.
     */
247
    class InputStreamEnumeration implements NamingEnumeration<InputStream> {
D
duke 已提交
248

249
        private final Enumeration<URL> urls;
D
duke 已提交
250

251
        private InputStream nextElement = null;
D
duke 已提交
252

253
        InputStreamEnumeration(Enumeration<URL> urls) {
D
duke 已提交
254 255 256 257 258 259 260
            this.urls = urls;
        }

        /*
         * Returns the next InputStream, or null if there are no more.
         * An InputStream that cannot be opened is skipped.
         */
261
        private InputStream getNextElement() {
D
duke 已提交
262
            return AccessController.doPrivileged(
263 264
                new PrivilegedAction<InputStream>() {
                    public InputStream run() {
D
duke 已提交
265 266
                        while (urls.hasMoreElements()) {
                            try {
267
                                return urls.nextElement().openStream();
D
duke 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
                            } 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();
        }

290
        public InputStream next() {
D
duke 已提交
291
            if (hasMore()) {
292
                InputStream res = nextElement;
D
duke 已提交
293 294 295 296 297 298 299
                nextElement = null;
                return res;
            } else {
                throw new NoSuchElementException();
            }
        }

300
        public InputStream nextElement() {
D
duke 已提交
301 302 303 304 305 306 307
            return next();
        }

        public void close() {
        }
    }
}