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