importorg.apache.commons.lang3.StringUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importjava.io.File;importjava.io.InputStreamReader;importjava.net.MalformedURLException;importjava.net.URI;importjava.net.URISyntaxException;importjava.net.URL;importjava.nio.charset.StandardCharsets;importjava.util.Properties;/**
* Created by luopiao on 17-2-28.
*/publicabstractclassResourceUtils{privatestaticfinalLogger logger =LoggerFactory.getLogger(ResourceUtils.class);publicstaticfinalString CLASSPATH_URL_PREFIX ="classpath:";publicstaticfinalString URL_PROTOCOL_FILE ="file";/**
* URL protocol for an entry from a jar file: "jar"
*/publicstaticfinalString URL_PROTOCOL_JAR ="jar";/**
* URL protocol for an entry from a zip file: "zip"
*/publicstaticfinalString URL_PROTOCOL_ZIP ="zip";/**
* URL protocol for an entry from a WebSphere jar file: "wsjar"
*/publicstaticfinalString URL_PROTOCOL_WSJAR ="wsjar";/**
* URL protocol for an entry from a JBoss jar file: "vfszip"
*/publicstaticfinalString URL_PROTOCOL_VFSZIP ="vfszip";/**
* URL protocol for a JBoss file system resource: "vfsfile"
*/publicstaticfinalString URL_PROTOCOL_VFSFILE ="vfsfile";/**
* URL protocol for a general JBoss VFS resource: "vfs"
*/publicstaticfinalString URL_PROTOCOL_VFS ="vfs";/**
* File extension for a regular jar file: ".jar"
*/publicstaticfinalString JAR_FILE_EXTENSION =".jar";publicstaticbooleanisUrl(String resourceLocation){if(resourceLocation ==null){returnfalse;}if(resourceLocation.startsWith(CLASSPATH_URL_PREFIX)){returntrue;}try{newURL(resourceLocation);returntrue;}catch(MalformedURLException ex){returnfalse;}}publicstaticURLgetURL(String resourceLocation){if(resourceLocation.startsWith(CLASSPATH_URL_PREFIX)){String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());ClassLoader cl =ClassUtils.getDefaultClassLoader();URL url =(cl !=null? cl.getResource(path):ClassLoader.getSystemResource(path));if(url ==null){String description ="class path resource ["+ path +"]";thrownewRuntimeException(description +" cannot be resolved to URL because it does not exist");}return url;}try{// try URLreturnnewURL(resourceLocation);}catch(MalformedURLException ex){
logger.debug("no URL -> treat as file path");try{returnnewFile(resourceLocation).toURI().toURL();}catch(MalformedURLException ex2){thrownewRuntimeException("Resource location ["+ resourceLocation +"] is neither a URL not a well-formed file path");}}}publicstaticFilegetFile(String resourceLocation){if(resourceLocation.startsWith(CLASSPATH_URL_PREFIX)){String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());String description ="class path resource ["+ path +"]";ClassLoader cl =ClassUtils.getDefaultClassLoader();URL url =(cl !=null? cl.getResource(path):ClassLoader.getSystemResource(path));if(url ==null){thrownewRuntimeException(description +" cannot be resolved to absolute file path because it does not exist");}returngetFile(url, description);}try{// try URLreturngetFile(newURL(resourceLocation));}catch(MalformedURLException ex){// no URL -> treat as file pathreturnnewFile(resourceLocation);}}publicstaticFilegetFile(URL resourceUrl){returngetFile(resourceUrl,"URL");}publicstaticFilegetFile(URL resourceUrl,String description){if(!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())){thrownewRuntimeException(description +" cannot be resolved to absolute file path "+"because it does not reside in the file system: "+ resourceUrl);}try{returnnewFile(toURI(resourceUrl).getSchemeSpecificPart());}catch(Exception ex){// Fallback for URLs that are not valid URIs (should hardly ever happen).returnnewFile(resourceUrl.getFile());}}/**
* 判断url 是否是jar 路径
*
* @param url url
* @return 是否是jar 路径
*/publicstaticbooleanisJarURL(URL url){String protocol = url.getProtocol();return(URL_PROTOCOL_JAR.equals(protocol)|| URL_PROTOCOL_ZIP.equals(protocol)||
URL_PROTOCOL_VFSZIP.equals(protocol)|| URL_PROTOCOL_WSJAR.equals(protocol));}publicstaticbooleanisJarFileURL(URL url){return(URL_PROTOCOL_FILE.equals(url.getProtocol())&&
url.getPath().toLowerCase().endsWith(JAR_FILE_EXTENSION));}publicstaticbooleanisFileURL(URL url){String protocol = url.getProtocol();return(URL_PROTOCOL_FILE.equals(protocol)|| URL_PROTOCOL_VFSFILE.equals(protocol)||
URL_PROTOCOL_VFS.equals(protocol));}publicstaticURItoURI(URL url){returntoURI(url.toString());}publicstaticURItoURI(String location){try{returnnewURI(StringUtils.replace(location," ","%20"));}catch(URISyntaxException e){thrownewRuntimeException(e);}}/**
* 读取配置文件
*
* @param path 文件路径
* @return 返回 Properties
*/publicstaticPropertiesreadProperties(String path){Properties config =newProperties();try(InputStreamReader in =newInputStreamReader(getURL(path).openStream(),StandardCharsets.UTF_8.name())){
config.load(in);}catch(Exception e){
logger.warn("缺少 {} 文件", path);}return config;}/**
* 检查文件时候存在
*
* @param resourceLocation 文件路径
* @return 是否存在
*/publicstaticbooleanexistsResource(String resourceLocation){try{getURL(resourceLocation);returntrue;}catch(Exception e){returnfalse;}}}