001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.logging.log4j.core.util;
019    
020    import java.io.IOException;
021    import java.net.URL;
022    import java.util.Enumeration;
023    
024    /**
025     * ClassLoader-based ResourceLoader.
026     */
027    public final class ClassLoaderResourceLoader implements ResourceLoader {
028    
029        private final ClassLoader loader;
030    
031        public ClassLoaderResourceLoader(final ClassLoader loader) {
032            this.loader = loader != null ? loader : this.getClass().getClassLoader();
033        }
034    
035        @Override
036        public Class<?> loadClass(final String name) throws ClassNotFoundException {
037            return loader.loadClass(name);
038        }
039    
040        @Override
041        public URL getResource(final String name) {
042            return loader.getResource(name);
043        }
044    
045        @Override
046        public Enumeration<URL> getResources(final String name) throws IOException {
047            return loader.getResources(name);
048        }
049    
050        @Override
051        public String toString() {
052            return this.getClass().getCanonicalName() + '(' + loader.toString() + ')';
053        }
054    }