com.bfd.harpc.common.configure.ResourceUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.bfd.harpc.common.configure.ResourceUtils.java

Source

/**
 * Copyright (C) 2015 Baifendian Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bfd.harpc.common.configure;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;

/**
 * ?<br />
 * ??{@link ResourceConstants}????<br />
 * ResourceUtils.loadResource("classpath*:log/log4j.xml");
 * <p>
 * 
 * @author dsfan
 * @date 2015-8-11
 */
public class ResourceUtils {
    /** ? */
    private static final Object SYS_OBJECT = new Object();

    /** {@link ClassLoader} */
    private static volatile ClassLoader classLoader;

    /**
     * jar?<br />
     * loadResources()??
     * <p>
     * 
     * @param locationPattern
     * <br/>
     *            0. ????{@link ResourceConstants}<br />
     *            1. fileclasspathclasspath*?<br />
     *            2. classpath?<br />
     *            3. classpath*??jar<br />
     *            4. file?<br />
     *            5. classpath 6.
     *            classpath*:log/log4j.xml;file:/home/ydhl/
     *            abc.sh;classpath:log/log4j.xml
     * @return URL
     * @throws IOException
     * @throws URISyntaxException
     */
    public static URL loadResource(String locationPattern) throws IOException, URISyntaxException {
        URL[] urlArray = loadResources(locationPattern);

        return ArrayUtils.isEmpty(urlArray) ? null : urlArray[0];
    }

    /**
     * jar?<br />
     * <p>
     * 
     * @param locationPattern
     * <br/>
     *            0. ????{@link ResourceConstants}<br />
     *            1. fileclasspathclasspath*?<br />
     *            2. classpath?<br />
     *            3. classpath*??jar<br />
     *            4. file?<br />
     *            5. classpath 6.
     *            classpath*:log/log4j.xmlfile:/home/ydhl/
     *            abc.shclasspath:log/log4j.xml
     * @return URL
     * @throws IOException
     * @throws URISyntaxException
     *             ?
     */
    public static URL[] loadResources(String locationPattern) throws IOException, URISyntaxException {
        if (locationPattern.startsWith(ResourceConstants.CLASSPATH_ALL_URL_PREFIX.getValue())) {
            return load1(locationPattern);
        } else if (locationPattern.startsWith(ResourceConstants.CLASSPATH_URL_PREFIX.getValue())) {
            return load2(locationPattern);
        } else if (locationPattern.startsWith(ResourceConstants.FILE_URL_PREFIX.getValue())) {
            return load3(locationPattern);
        } else {
            // 
            locationPattern = "file:" + locationPattern;
            return ResourceUtils.loadResources(locationPattern);
        }
    }

    private static URL[] load1(String locationPattern) throws IOException, URISyntaxException {
        String location = locationPattern.substring(ResourceConstants.CLASSPATH_ALL_URL_PREFIX.getValue().length());
        if (location.startsWith(ResourceConstants.FOLDER_SEPARATOR.getValue())) {
            location = location.substring(1);
        }

        Enumeration<URL> resourceUrls = getDefaultClassLoader().getResources(location);
        Set<URL> result = new LinkedHashSet<URL>(16);
        while (resourceUrls.hasMoreElements()) {
            URL url = resourceUrls.nextElement();
            result.add(PathUtils.cleanPath(url));
        }
        return result.toArray(new URL[result.size()]);
    }

    private static URL[] load2(String locationPattern) throws URISyntaxException, IOException {
        String location = locationPattern.substring(ResourceConstants.CLASSPATH_URL_PREFIX.getValue().length());
        if (location.startsWith(ResourceConstants.FOLDER_SEPARATOR.getValue())) {
            location = location.substring(1);
        }

        String cleanPath = PathUtils.cleanPath(location);

        // a single resource with the given name
        URL url = getDefaultClassLoader().getResource(cleanPath);
        // if (url == null) {
        // throw new UnsupportedOperationException(cleanPath);
        // }
        return url == null ? null : new URL[] { PathUtils.cleanPath(url) };
    }

    private static URL[] load3(String locationPattern) throws MalformedURLException {

        // a single resource with the given name
        URL url = new URL(locationPattern);
        return new File(url.getFile()).exists() ? new URL[] { url } : null;
    }

    /**
     * ??classloaderclassloaderclassloader
     * <p>
     * 
     * @return {@link ClassLoader}
     */
    public static ClassLoader getDefaultClassLoader() {
        if (classLoader != null) {
            return classLoader;
        }

        synchronized (SYS_OBJECT) {
            if (classLoader != null) {
                return classLoader;
            }

            ClassLoader tempClassLoader = null;
            try {
                tempClassLoader = Thread.currentThread().getContextClassLoader();
            } catch (Exception ex) {
                // Cannot access thread context ClassLoader - falling back
                // to system class loader...
            }

            if (tempClassLoader == null) {
                // No thread context class loader -> use class loader of
                // this class.
                tempClassLoader = ResourceUtils.class.getClassLoader();
            }

            classLoader = tempClassLoader;

        }

        return classLoader;
    }
}