src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/c14n/ClassLoaderUtils.java
author jlaskey
Thu, 21 Jun 2018 08:58:59 -0300
changeset 50695 36ca515343e0
parent 50614 3810c9a2efa1
permissions -rw-r--r--
8203637: Fix Sources Reviewed-by: hannesw, sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50614
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     1
/*
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     2
 * reserved comment block
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     4
 */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     5
/**
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     8
 * distributed with this work for additional information
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    11
 * "License"); you may not use this file except in compliance
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    12
 * with the License. You may obtain a copy of the License at
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    13
 *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    15
 *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    17
 * software distributed under the License is distributed on an
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    19
 * KIND, either express or implied. See the License for the
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    20
 * specific language governing permissions and limitations
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    21
 * under the License.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    22
 */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    23
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    24
package com.sun.org.apache.xml.internal.security.c14n;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    25
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    26
// NOTE! This is a duplicate of utils.ClassLoaderUtils with public
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    27
// modifiers changed to package-private. Make sure to integrate any future
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    28
// changes to utils.ClassLoaderUtils to this file.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    29
final class ClassLoaderUtils {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    30
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    31
    private static final com.sun.org.slf4j.internal.Logger LOG =
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    32
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(ClassLoaderUtils.class);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    33
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    34
    private ClassLoaderUtils() {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    35
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    36
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    37
    /**
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    38
     * Load a class with a given name. <p></p> It will try to load the class in the
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    39
     * following order:
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    40
     * <ul>
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    41
     * <li>From Thread.currentThread().getContextClassLoader()
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    42
     * <li>Using the basic Class.forName()
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    43
     * <li>From ClassLoaderUtil.class.getClassLoader()
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    44
     * <li>From the callingClass.getClassLoader()
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    45
     * </ul>
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    46
     *
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    47
     * @param className The name of the class to load
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    48
     * @param callingClass The Class object of the calling object
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    49
     * @throws ClassNotFoundException If the class cannot be found anywhere.
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    50
     */
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    51
    static Class<?> loadClass(String className, Class<?> callingClass)
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    52
        throws ClassNotFoundException {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    53
        try {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    54
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    55
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    56
            if (cl != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    57
                return cl.loadClass(className);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    58
            }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    59
        } catch (ClassNotFoundException e) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    60
            LOG.debug(e.getMessage(), e);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    61
            //ignore
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    62
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    63
        return loadClass2(className, callingClass);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    64
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    65
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    66
    private static Class<?> loadClass2(String className, Class<?> callingClass)
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    67
        throws ClassNotFoundException {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    68
        try {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    69
            return Class.forName(className);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    70
        } catch (ClassNotFoundException ex) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    71
            try {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    72
                if (ClassLoaderUtils.class.getClassLoader() != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    73
                    return ClassLoaderUtils.class.getClassLoader().loadClass(className);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    74
                }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    75
            } catch (ClassNotFoundException exc) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    76
                if (callingClass != null && callingClass.getClassLoader() != null) {
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    77
                    return callingClass.getClassLoader().loadClass(className);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    78
                }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    79
            }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    80
            LOG.debug(ex.getMessage(), ex);
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    81
            throw ex;
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    82
        }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    83
    }
3810c9a2efa1 8177334: Update xmldsig implementation to Apache Santuario 2.1.1
weijun
parents:
diff changeset
    84
}