org.openflamingo.uploader.util.ResourceUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.openflamingo.uploader.util.ResourceUtils.java

Source

/**
 * Flamingo HDFS File Uploader - a tool to upload from datasource to datasource and schedule jobs
 *
 * Copyright (C) 2011-2012 Cloudine.
 *
 * This file is part of Flamingo HDFS File Uploader.
 *
 * Flamingo HDFS File Uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Flamingo HDFS File Uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openflamingo.uploader.util;

import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.ClassUtils;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;

/**
 *    ?. ?  ? ?  ?  URL ? ?    .
 *
 * @author Edward KIM
 * @since 0.1
 */
public class ResourceUtils {

    /**
     * ClASSPATH? ? ? Pseudo URL prefix : "classpath:"
     */
    public static final String CLASSPATH_URL_PREFIX = "classpath:";

    /**
     * ? ? ? ? URL prefix : "file:"
     */
    public static final String FILE_URL_PREFIX = "file:";

    /**
     * ?  ? ??  URL protocol : "file"
     */
    public static final String URL_PROTOCOL_FILE = "file";

    /**
     * Jar ??   URL protocol : "jar"
     */
    public static final String URL_PROTOCOL_JAR = "jar";

    /**
     * Zip ??   URL protocol : "zip"
     */
    public static final String URL_PROTOCOL_ZIP = "zip";

    /**
     * JAR ? ? ?  JAR URL ? seperator
     */
    public static final String JAR_URL_SEPARATOR = "!/";

    /**
     *    ?(Spring Framework)
     */
    public static final ResourceLoader resourceLoader = new DefaultResourceLoader();

    /**
     *  ?  URL?  ?.   "classpath" ?  URL? ?? .
     *
     * @param resourceLocation  
     * @return URL?  <tt>true</tt>
     * @see #CLASSPATH_URL_PREFIX
     * @see java.net.URL
     */
    public static boolean isUrl(String resourceLocation) {
        if (resourceLocation == null) {
            return false;
        }
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            return true;
        }
        try {
            new URL(resourceLocation);
            return true;
        } catch (MalformedURLException e) {
            return false;
        }
    }

    /**
     *  ? URL?  {@link java.net.URL}? .
     *
     * @param resourceLocation ? URL
     * @return ?? {@link java.net.URL} ?
     * @throws java.io.FileNotFoundException ?? ?   
     */
    public static URL getURL(String resourceLocation) throws FileNotFoundException {
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
            URL url = ClassUtils.getDefaultClassLoader().getResource(path);
            if (url == null) {
                String description = "CLASSPATH  [" + path + "]";
                throw new FileNotFoundException(description
                        + "? URL? ?  .  URL? ??  ?.");
            }
            return url;
        }
        try {
            // URL ?
            return new URL(resourceLocation);
        } catch (MalformedURLException e) {
            // no URL -> ?  
            try {
                return new URL(FILE_URL_PREFIX + resourceLocation);
            } catch (MalformedURLException mue) {
                throw new FileNotFoundException("  [" + resourceLocation
                        + "]? URL?  ??? ? ? .");
            }
        }
    }

    /**
     *  ?  ? ? {@link java.io.File} .  ??   .
     *
     * @param resourceLocation ? . ?   ? "classpath:", "file:" ? ? ?  .
     * @return ?? {@link java.io.File} ?
     * @throws java.io.FileNotFoundException ?? ?   
     */
    public static File getFile(String resourceLocation) throws FileNotFoundException {
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
            String description = "CLASSPATH  [" + path + "]";
            URL url = getDefaultClassLoader().getResource(path);
            if (url == null) {
                throw new FileNotFoundException(description
                        + "?  ?  . ? ? ?   .");
            }
            return getFile(url, description);
        }
        try {
            // URL ?
            return getFile(new URL(resourceLocation));
        } catch (MalformedURLException e) {
            // no URL -> ?  
            return new File(resourceLocation);
        }
    }

    /**
     *  ? ? . ?    ? ?  ? ? ? ?  ?  ? ? ? <tt>null</tt>?? ? ? 
     * ? ? .
     *
     * @return ? ?
     */
    private static ClassLoader getDefaultClassLoader() {
        ClassLoader cl = null;
        try {
            cl = Thread.currentThread().getContextClassLoader();
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println(
                    "? ? ? ??   .  ? ? .");
        }
        if (cl == null) {
            //  ? ? ? ? -> ? ?? ? ?  
            cl = ResourceUtils.class.getClassLoader();
        }
        return cl;
    }

    /**
     *  ?  ? ? {@link java.io.File} .
     *
     * @param resourceUrl ? 
     * @return ?? {@link java.io.File} ?
     * @throws java.io.FileNotFoundException ?? ?   
     */
    public static File getFile(URL resourceUrl) throws FileNotFoundException {
        return getFile(resourceUrl, "URL");
    }

    /**
     *  ?  ? ? {@link java.io.File} .
     *
     * @param resourceUrl ? 
     * @param description ?  ?  (; ?  )
     * @return ?? {@link java.io.File} ?
     * @throws java.io.FileNotFoundException ?? ?   
     */
    @SuppressWarnings({ "deprecation" })
    public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {
        if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
            throw new FileNotFoundException(description
                    + "?  ?  . ? ? ?    : "
                    + resourceUrl);
        }
        return new File(URLDecoder.decode(resourceUrl.getFile()));
    }

    /**
     *  URL? Jar ? ?   ? ?. , "jar", "zip"?   ? ?. "zip"? WebLogic?  Jar ? .
     *
     * @param url  URL
     * @return JAR ?? 
     */
    public static boolean isJarURL(URL url) {
        String protocol = url.getProtocol();
        return (URL_PROTOCOL_JAR.equals(protocol) || URL_PROTOCOL_ZIP.equals(protocol));
    }

    /**
     *  URL(Jar ??   ? Jar ?  ?)   Jar ??  URL? (Jar ?  ? ? Jar ? ?   ?).
     *
     * @param jarUrl ?  
     * @return ?? {@link java.net.URL} ?
     * @throws java.net.MalformedURLException URL ?? ? 
     */
    public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {
        String urlFile = jarUrl.getFile();
        int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
        if (separatorIndex != -1) {
            String jarFile = urlFile.substring(0, separatorIndex);
            try {
                return new URL(jarFile);
            } catch (MalformedURLException e) {
                // ? ? Jar URL ? ?   ?(; "jar:C:/mypath/myjar.jar").
                // ? ?  Jar ??   ?
                if (!jarFile.startsWith("/")) {
                    jarFile = "/" + jarFile;
                }
                return new URL(FILE_URL_PREFIX + jarFile);
            }
        } else {
            return jarUrl;
        }
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource} .
     *
     * @param location  
     * @return {@link org.springframework.core.io.Resource}
     */
    public static Resource getResource(String location) {
        return resourceLoader.getResource(location);
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource}? ? ? .
     *
     * @param location  
     * @return {@link org.springframework.core.io.Resource}? ? 
     * @throws java.io.IOException     
     */
    public static String getResourceTextContents(String location) throws IOException {
        Resource resource = resourceLoader.getResource(location);
        InputStream inputStream = resource.getInputStream();
        return getResourceTextContents(inputStream);
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource}? ? ? .
     *
     * @param inputStream   
     * @return {@link org.springframework.core.io.Resource}? ? 
     * @throws java.io.IOException  ?    
     */
    public static String getResourceTextContents(InputStream inputStream) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        int size = bis.available();
        byte[] bytes = new byte[size];
        bis.read(bytes);
        return new String(bytes, "UTF-8");
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource}? ? ?  .
     *
     * @param location  
     * @return {@link org.springframework.core.io.Resource}? ?  
     * @throws java.io.IOException     
     */
    public static byte[] getResourceByteContents(String location) throws IOException {
        Resource resource = resourceLoader.getResource(location);
        return getResourceByteContents(resource);
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource}? ? ? .
     *
     * @param resource 
     * @return {@link org.springframework.core.io.Resource}? ?  
     * @throws java.io.IOException     
     */
    public static String getResourceTextContents(Resource resource) throws IOException {
        byte[] resourceByteContents = getResourceByteContents(resource);
        return new String(resourceByteContents, "UTF-8");
    }

    /**
     *  ?  {@link org.springframework.core.io.Resource}? ? ?  .
     *
     * @param resource 
     * @return {@link org.springframework.core.io.Resource}? ?  
     * @throws java.io.IOException ??    
     */
    public static byte[] getResourceByteContents(Resource resource) throws IOException {
        InputStream inputStream = resource.getInputStream();
        int size = inputStream.available();
        byte[] bytes = new byte[size];
        inputStream.read(bytes);
        return bytes;
    }
}