001/* 
002 * JKNIV, SQLegance keeping queries maintainable.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.sqlegance;
021
022import java.io.InputStream;
023import java.net.URL;
024
025public class DefaultClassLoader
026{
027    /**
028     * Return the default ClassLoader to use: typically the thread context
029     * ClassLoader, if available; the ClassLoader that loaded the ClassUtils
030     * class will be used as fallback.
031     * <p>Call this method if you intend to use the thread context ClassLoader
032     * in a scenario where you absolutely need a non-null ClassLoader reference:
033     * for example, for class path resource loading (but not necessarily for
034     * {@code Class.forName}, which accepts a {@code null} ClassLoader
035     * reference as well).
036     * @return the default ClassLoader (never {@code null})
037     * @see Thread#getContextClassLoader()
038     */
039    public static ClassLoader getClassLoader() {
040        ClassLoader cl = null;
041        try {
042            cl = Thread.currentThread().getContextClassLoader();
043        }
044        catch (Throwable ex) {
045            // Cannot access thread context ClassLoader - falling back to system class loader...
046        }
047        if (cl == null) {
048            // No thread context class loader -> use class loader of this class.
049            cl = DefaultClassLoader.class.getClassLoader();
050        }
051        return cl;
052    }
053
054    public static InputStream getResourceAsStream(String resource)
055    {
056        InputStream is = null;
057        
058        is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
059        if (is == null)
060            is = ClassLoader.getSystemResourceAsStream(resource);
061        if (is == null)
062            is = DefaultClassLoader.class.getResourceAsStream(resource);
063        return is;
064    }
065    
066    public static URL getResource(String resource)
067    {
068        URL url = null;
069        
070        url = Thread.currentThread().getContextClassLoader().getResource(resource);
071        if (url == null)
072            url = ClassLoader.getSystemResource(resource);
073        if (url == null)
074            url = DefaultClassLoader.class.getResource(resource);
075        return url;
076    }
077
078}