jp.terasoluna.fw.batch.unit.util.ClassLoaderUtils.java Source code

Java tutorial

Introduction

Here is the source code for jp.terasoluna.fw.batch.unit.util.ClassLoaderUtils.java

Source

package jp.terasoluna.fw.batch.unit.util;

/*
 * Copyright (c) 2012 NTT DATA 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.
 */

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.util.List;

import jp.terasoluna.fw.batch.unit.exception.UTRuntimeException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

public class ClassLoaderUtils {
    protected volatile static ClassLoader previousClassLoader = null;
    protected static final Log LOG = LogFactory.getLog(ClassLoaderUtils.class);

    private static Method getAddURLMethod() {
        try {
            Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class<?>[] { URL.class });
            method.setAccessible(true);
            return method;
        } catch (Exception e) {
            throw new UTRuntimeException(e);
        }
    }

    /**
     * ???????
     * 
     * @return 
     */
    public static ClassLoader getClassLoader() {
        return Thread.currentThread().getContextClassLoader();
    }

    /**
     * ???????
     * 
     * @param cl 
     */
    public static void setClassLoader(ClassLoader cl) {
        Thread.currentThread().setContextClassLoader(cl);
    }

    /**
     * <pre>
     * ??????url???????
     * ???????
     * 
     * ????
     * ????????????
     * {@link #resetClassLoader()}???????
     * </pre>
     * 
     * @param url ?
     */
    public static void addClassPath(URL url) {
        Assert.notNull(url);
        ClassLoader cl = getClassLoader();

        URLClassLoader newCl = null;
        if (cl instanceof URLClassLoader) {
            @SuppressWarnings("resource")
            URLClassLoader ucl = (URLClassLoader) cl;
            newCl = new URLClassLoader(ucl.getURLs(), cl);
        } else {
            newCl = new URLClassLoader(null, cl);
        }

        Method method = getAddURLMethod();

        try {
            method.invoke(newCl, url);
        } catch (Exception e) {
            throw new UTRuntimeException(e);
        }

        previousClassLoader = cl;
        setClassLoader(newCl);
    }

    /**
     * ????addClassPath???????????
     */
    public static void resetClassLoader() {
        if (previousClassLoader != null) {
            setClassLoader(previousClassLoader);
        }
    }

    /**
     * <pre>
     * ?????????????
     * ???????
     * 
     * ????
     * ????????????
     * {@link #resetClassLoader()}???????
     * 
     * ?????????warn???
     * </pre>
     * 
     * @param file ?
     */
    public static void addClassPath(File file) {
        Assert.notNull(file);
        try {
            if (file.exists()) {
                addClassPath(file.toURI().toURL());
            } else {
                LOG.warn(file + " is not found.");
            }
        } catch (MalformedURLException e) {
            // ?????????
            LOG.warn(file + " is illegal.", e);
        }
    }

    /**
     * <pre>
     * ?????????????
     * ???????
     * 
     * ????
     * ????????????
     * {@link #resetClassLoader()}???????
     * 
     * ??????{@link UTRuntimeException}???
     * </pre>
     * 
     * @param path ?
     * @throws UTRuntimeException ?????
     */
    public static void addClassPath(String path) {
        Assert.notNull(path);
        addClassPath(new File(path));
    }

    /**
     * <pre>
     * srcPaths??????????
     * ??????
     * ????destPaths????
     * </pre>
     * 
     * @param destPaths
     * @param srcPaths
     */
    public static void addPathIfExists(List<String> destPaths, List<String> srcPaths) {
        Assert.notNull(destPaths);
        Assert.notNull(srcPaths);

        ClassLoader cl = getClassLoader();
        for (String path : srcPaths) {
            if (path != null) {
                URL r = cl.getResource(path);
                if (r != null) {
                    try {
                        String protocol = r.getProtocol();
                        if (protocol.equals("file")) {
                            URI uri = r.toURI();
                            File f = new File(uri);
                            if (f.isFile()) {
                                // ?????
                                destPaths.add(path);
                            }
                        } else {
                            // jar
                            URLConnection con = null;
                            try {
                                con = r.openConnection();
                                if (con.getContentLength() > 0) {
                                    // ?????
                                    destPaths.add(path);
                                }
                            } catch (IOException e) {
                                // ?????????
                                LOG.warn(con + " is illegal.", e);
                            }
                        }
                    } catch (URISyntaxException e) {
                        // r != null???????????????
                        LOG.warn(path + " is illegal.", e);
                    }
                } else {
                    LOG.debug(path + " is not found.");
                }
            }
        }
    }
}