首页

关于spark源码包中的ResourceUtils资源工具类对常见资源文件进行操作

标签:ResourceUtils,spark,资源工具类     发布时间:2018-02-27   

一、前言

关于spark包(spark-master)的spark.utils.ResourceUtils资源文件操作工具类对资源配置文件加载、获取、转换等进行操作,详情见源码说明。

二、源码说明

package spark.utils;@b@@b@import java.io.File;@b@import java.io.FileNotFoundException;@b@import java.net.MalformedURLException;@b@import java.net.URI;@b@import java.net.URISyntaxException;@b@import java.net.URL;@b@import java.net.URLConnection;@b@@b@ @b@public abstract class ResourceUtils {@b@@b@    /**@b@     * Pseudo URL prefix for loading from the class path: "classpath:"@b@     */@b@    public static final String CLASSPATH_URL_PREFIX = "classpath:";@b@@b@    /**@b@     * URL prefix for loading from the file system: "file:"@b@     */@b@    public static final String FILE_URL_PREFIX = "file:";@b@@b@    /**@b@     * URL protocol for a file in the file system: "file"@b@     */@b@    public static final String URL_PROTOCOL_FILE = "file";@b@@b@    /**@b@     * URL protocol for an entry from a jar file: "jar"@b@     */@b@    public static final String URL_PROTOCOL_JAR = "jar";@b@@b@    /**@b@     * URL protocol for an entry from a zip file: "zip"@b@     */@b@    public static final String URL_PROTOCOL_ZIP = "zip";@b@@b@    /**@b@     * URL protocol for an entry from a JBoss jar file: "vfszip"@b@     */@b@    public static final String URL_PROTOCOL_VFSZIP = "vfszip";@b@@b@    /**@b@     * URL protocol for an entry from a WebSphere jar file: "wsjar"@b@     */@b@    public static final String URL_PROTOCOL_WSJAR = "wsjar";@b@@b@    /**@b@     * Separator between JAR URL and file path within the JAR@b@     */@b@    public static final String JAR_URL_SEPARATOR = "!/";@b@@b@ @b@    public static boolean isUrl(String resourceLocation) {@b@        if (resourceLocation == null) {@b@            return false;@b@        }@b@        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {@b@            return true;@b@        }@b@        try {@b@            new URL(resourceLocation);@b@            return true;@b@        } catch (MalformedURLException ex) {@b@            return false;@b@        }@b@    }@b@@b@     @b@    public static URL getURL(String resourceLocation) throws FileNotFoundException {@b@        Assert.notNull(resourceLocation, "Resource location must not be null");@b@        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {@b@            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());@b@            URL url = ClassUtils.getDefaultClassLoader().getResource(path);@b@            if (url == null) {@b@                String description = "class path resource [" + path + "]";@b@                throw new FileNotFoundException(@b@                        description + " cannot be resolved to URL because it does not exist");@b@            }@b@            return url;@b@        }@b@        try {@b@            // try URL@b@            return new URL(resourceLocation);@b@        } catch (MalformedURLException ex) {@b@            // no URL -> treat as file path@b@            try {@b@                return new File(resourceLocation).toURI().toURL();@b@            } catch (MalformedURLException ex2) {@b@                throw new FileNotFoundException("Resource location [" + resourceLocation +@b@                                                        "] is neither a URL not a well-formed file path");@b@            }@b@        }@b@    }@b@@b@    @b@    public static File getFile(String resourceLocation) throws FileNotFoundException {@b@        Assert.notNull(resourceLocation, "Resource location must not be null");@b@        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {@b@            String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());@b@            String description = "class path resource [" + path + "]";@b@            URL url = ClassUtils.getDefaultClassLoader().getResource(path);@b@            if (url == null) {@b@                throw new FileNotFoundException(@b@                        description + " cannot be resolved to absolute file path " +@b@                                "because it does not reside in the file system"@b@                );@b@            }@b@            return getFile(url, description);@b@        }@b@        try {@b@            // try URL@b@            return getFile(new URL(resourceLocation));@b@        } catch (MalformedURLException ex) {@b@            // no URL -> treat as file path@b@            return new File(resourceLocation);@b@        }@b@    }@b@@b@     @b@    public static File getFile(URL resourceUrl) throws FileNotFoundException {@b@        return getFile(resourceUrl, "URL");@b@    }@b@@b@     @b@    public static File getFile(URL resourceUrl, String description) throws FileNotFoundException {@b@        Assert.notNull(resourceUrl, "Resource URL must not be null");@b@        if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {@b@            throw new FileNotFoundException(@b@                    description + " cannot be resolved to absolute file path " +@b@                            "because it does not reside in the file system: " + resourceUrl@b@            );@b@        }@b@        try {@b@            return new File(toURI(resourceUrl).getSchemeSpecificPart());@b@        } catch (URISyntaxException ex) {@b@            // Fallback for URLs that are not valid URIs (should hardly ever happen).@b@            return new File(resourceUrl.getFile());@b@        }@b@    }@b@@b@    @b@    public static File getFile(URI resourceUri) throws FileNotFoundException {@b@        return getFile(resourceUri, "URI");@b@    }@b@@b@    @b@    public static File getFile(URI resourceUri, String description) throws FileNotFoundException {@b@        Assert.notNull(resourceUri, "Resource URI must not be null");@b@        if (!URL_PROTOCOL_FILE.equals(resourceUri.getScheme())) {@b@            throw new FileNotFoundException(@b@                    description + " cannot be resolved to absolute file path " +@b@                            "because it does not reside in the file system: " + resourceUri@b@            );@b@        }@b@        return new File(resourceUri.getSchemeSpecificPart());@b@    }@b@@b@    @b@    public static boolean isFileURL(URL url) {@b@        String protocol = url.getProtocol();@b@        return (URL_PROTOCOL_FILE.equals(protocol));@b@    }@b@@b@     @b@    public static boolean isJarURL(URL url) {@b@        String up = url.getProtocol();@b@        return (URL_PROTOCOL_JAR.equals(up) || URL_PROTOCOL_ZIP.equals(up) || URL_PROTOCOL_WSJAR.equals(up));@b@    }@b@@b@    @b@    public static URL extractJarFileURL(URL jarUrl) throws MalformedURLException {@b@        String urlFile = jarUrl.getFile();@b@        int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);@b@        if (separatorIndex != -1) {@b@            String jarFile = urlFile.substring(0, separatorIndex);@b@            try {@b@                return new URL(jarFile);@b@            } catch (MalformedURLException ex) {@b@                // Probably no protocol in original jar URL, like "jar:C:/mypath/myjar.jar".@b@                // This usually indicates that the jar file resides in the file system.@b@                if (!jarFile.startsWith("/")) {@b@                    jarFile = "/" + jarFile;@b@                }@b@                return new URL(FILE_URL_PREFIX + jarFile);@b@            }@b@        } else {@b@            return jarUrl;@b@        }@b@    }@b@@b@     @b@    public static URI toURI(URL url) throws URISyntaxException {@b@        return toURI(url.toString());@b@    }@b@@b@     @b@    public static URI toURI(String location) throws URISyntaxException {@b@        return new URI(StringUtils.replace(location, " ", "%20"));@b@    }@b@@b@     @b@    public static void useCachesIfNecessary(URLConnection con) {@b@        con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));@b@    }@b@@b@}