上善若水
In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
posts - 146,comments - 147,trackbacks - 0

Java中,将不同来源的资源抽象成URL,通过注册不同的handlerURLStreamHandler)来处理不同来源的资源的读取逻辑,一般handler的类型使用不同前缀(协议,protocol)来识别,如“file:”、“http:”、“jar:”等,然而URL没有默认定义相对classpathServletContext等资源的handler,虽然可以注册自己的URLStreamHandler来解析特定的URL前缀(协议),比如“classpath:”,然而这需要了解URL的实现机制,而且URL也没有提供一些基本的方法,如检查当前资源是否存在、检查当前资源是否可读等方法。因而Spring对其内部使用到的资源实现了自己的抽象结构:Resource接口来封装底层资源:

public interface InputStreamSource {

    InputStream getInputStream() throws IOException;

}

public interface Resource extends InputStreamSource {

    boolean exists();

    boolean isReadable();

    boolean isOpen();

    URL getURL() throws IOException;

    URI getURI() throws IOException;

    File getFile() throws IOException;

    long lastModified() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    String getFilename();

    String getDescription();

}

InputStreamSource封装任何能返回InputStream的类,比如Fileclasspath下的资源、Byte Array等。它只有一个方法定义:getInputStream(),该方法返回一个新的InputStream对象。

Resource接口抽象了所有Spring内部使用到的底层资源:FileURLclasspath等。首先,它定义了三个判断当前资源状态的方法:存在性(exists)、可读性(isReadable)、是否处于打开状态(isOpen)。在C语言中,当我们拿到一个文件句柄时,我们要调用open方法打开文件才可以真正读取该文件,但是在Java中并没有显示的定义open方法,一般当我们创建一个InputStreamReader时,该资源(文件)就已经处于打开状态了,因而这里的isOpen方法并不是判断当前资源是否已经处于打开的可操作状态,这里是表示Resource接口所抽象的底层资源是否可以多次调用getInputStream()方法,如果该方法返回true,则不可以多次调用getInputStream()方法。在Spring 2.5.6的实现中,只有InputStreamResource类的isOpen()方法返回true,其余都返回false

另外,Resource接口还提供了不同资源到URLURIFile类型的转换,以及获取lastModified属性、文件名(不带路径信息的文件名,getFilename())的方法。为了便于操作,Resource还提供了基于当前资源创建一个相对资源的方法:createRelative();在错误处理中需要详细的打印出错的资源文件,因而Resource还提供了getDescription()方法用于在错误处理中的打印信息。

Spring 2.5.6中,所有实现Resource的接口类继承关系图如下:

即对不同来源的资源文件都有相应的Resource实现:文件(FileSystemResource)、classpath资源(ClassPathResource)、URL资源(UrlResource)、InputStream资源(InputStreamResource)、Byte数组(ByteArrayResource)等。

AbstractResource

AbstractResource是对Resource的基本实现,所有Resource实现类都继承了该类,所有继承该类的Resource一般只需要实现以下方法即可:

public File getFile() throws IOException

public URL getURL() throws IOException

public String getDescription()

public InputStream getInputStream() throws IOException

该类默认实现中,将toStringequalshashCode都代理给Description属性;isReadable总是返回true,而isOpen总是返回falseexists方法实现中,先调用getFile返回的File对象的exists方法,如果失败,查看是否可以获得InputStream,如果可以,返回true,否则,返回falsegetURLgetFilecreateRelative方法抛出FileNotFoundException,而getURI则代理给getURL方法。

ByteArrayResource

ByteArrayResource是一个简单的Resource实现,它是对二进制数组的封装,每次调用getInputStream时都会以这个二进制数组作为源创建一个ByteArrayInputStream。它的exists方法总是返回true,而且重写了equalshashCode的方法,以判断二进制数组的内容;它的description属性可以是用户自定义,也可以使用默认值:resource loaded from byte array

public final byte[] getByteArray() {

    return this.byteArray;

}

public boolean exists() {

    return true;

}

public InputStream getInputStream() throws IOException {

    return new ByteArrayInputStream(this.byteArray);

}

FileSystemResource

FileSystemResource是对File的封装,在构建FileSystemResource时可以传入File对象或路径字符串(这里的路径可以是相对路径,相对路径是相对于System.getProperty(“user.dir”)的值所在的路径,也可以是绝对路径,也可以是“file:”开头的路径值),在内部会创建相应的File对象,并且计算其path值,这里的path是计算完“.”和“..”影响的值(规格化)。

getInputStream方法中,使用该File对象创建FileInputStream;而path值作为description属性、equalshashCode等方法的实现;所有其他方法(existsisReadablegetURL等)都代理给File对象;createRelative方法中使用path计算相对路径,其算法是:找到最后一个路径分隔符(/),将相对路径添加到该分隔符之后,传入的相对路径可以是以路径分割符(/)开头,也可以不以分隔符(/)开头,他们的效果是一样的,对相对路径存在的“.”和“..”会在创建FileSystemResource类时处理。最后,当使用将一个目录的File对象构建FileSystemResource时,调用createRelative方法,其相对路径的父目录和当前FileSystemResource的父目录相同,比如使用”/home/Levin/dir1”目录创建FileSystemResource对象,该Resource对象调用createRelative,并传入”file”,那么出现的结果为”/home/Levin/file”,如果要得到”/home/Levin/dir1/file”,那么构建FileSystemResource时,应该传入”/home/Levin/dir1/”字符串。

public boolean isReadable() {

    return (this.file.canRead() && !this.file.isDirectory());

}

public InputStream getInputStream() throws IOException {

    return new FileInputStream(this.file);

}

public Resource createRelative(String relativePath) {

    String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);

    return new FileSystemResource(pathToUse);

}

public String getDescription() {

    return "file [" + this.file.getAbsolutePath() + "]";

}

UrlResource

UrlResource是对URLURI的封装。在构建UrlResource时可以传入URLURIPath字符串(带协议字符串,如”file:”)。在UrlResource内部还会创建一个cleanedUrl,它是规格化(计算“.”和“..”后的值),该URL将会用于equalshashCode方法的实现。

getInputStream方法实现中,它使用URL.openConnection()方法获取URLConnection,后调用该URLConnectiongetInputStream方法。对getFile()方法,只支持文件系统的资源,即URL字符串的协议部分为”file:”UrlResource还支持从jarzipvfszipwsjar等内部文件,以jar为例,这些文件的字符串表达为:jar:file:/<jarpath>/jarfile.jar!/<filepath>/filename,如jar:file:/E:/Program%20Files/eclipse-juno/plugins/org.junit_4.10.0.v4_10_0_v20120426-0900/junit.jar!/org/junit/Test.class,然而这些内部文件本身并没有lastModified的属性,因而对这些内部文件,UrlResourcejarzip等文件的lastModified视为这些内部文件的lastModified属性。对createRelative方法,直接使用URL提供的构造函数,忽略传入的relativePath中的路径分隔符“/”。

public InputStream getInputStream() throws IOException {

    URLConnection con = this.url.openConnection();

    con.setUseCaches(false);

    return con.getInputStream();

}

protected File getFileForLastModifiedCheck() throws IOException {

    if (ResourceUtils.isJarURL(this.url)) {

        URL actualUrl = ResourceUtils.extractJarFileURL(this.url);

        return ResourceUtils.getFile(actualUrl);

    }

    else {

        return getFile();

    }

}

public Resource createRelative(String relativePath) throws MalformedURLException {

    if (relativePath.startsWith("/")) {

        relativePath = relativePath.substring(1);

    }

    return new UrlResource(new URL(this.url, relativePath));

}

ClassPathResource

classpath下资源的封装,或者说是对ClassLoader.getResource()方法或Class.getResource()方法的封装。它支持在当前classpath中读取资源文件。可以传入相对classpath的文件全路径名和ClassLoader构建ClassPathResource,或忽略ClassLoader采用默认ClassLoader(即Thread Context ClassLoader),此时在getInputStream()方法实现时时会使用ClassLoader.getResourceAsStream()方法,由于使用ClassLoader获取资源时默认相对于classpath的根目录,因而构造函数会忽略开头的“/”字符。ClassPathResource还可以使用文件路径和Class作为参数构建,此时若文件路径以“/”开头,表示该文件为相对于classpath的绝对路径,否则为相对Class实例的相对路径,在getInputStream()方法实现时使用Class.getResourceAsStream()方法。

getFile()方法只支持存在于文件系统中的资源;对lastModified的属性,若是jarzip等文件中的资源,则采用jarzip文件本身的lastModified属性;equals会同时判断pathclassloaderclazz字段,而hashCode则只使用path

public InputStream getInputStream() throws IOException {

    InputStream is = null;

    if (this.clazz != null) {

        is = this.clazz.getResourceAsStream(this.path);

    }

    else {

        is = this.classLoader.getResourceAsStream(this.path);

    }

    if (is == null) {

        throw new FileNotFoundException(

                getDescription() + " cannot be opened because it does not exist");

    }

    return is;

}

public URL getURL() throws IOException {

    URL url = null;

    if (this.clazz != null) {

        url = this.clazz.getResource(this.path);

    }

    else {

        url = this.classLoader.getResource(this.path);

    }

    if (url == null) {

        throw new FileNotFoundException(

                getDescription() + " cannot be resolved to URL because it does not exist");

    }

    return url;

}

public File getFile() throws IOException {

    return ResourceUtils.getFile(getURL(), getDescription());

}

protected File getFileForLastModifiedCheck() throws IOException {

    URL url = getURL();

    if (ResourceUtils.isJarURL(url)) {

        URL actualUrl = ResourceUtils.extractJarFileURL(url);

        return ResourceUtils.getFile(actualUrl);

    }

    else {

        return ResourceUtils.getFile(url, getDescription());

    }

}

public Resource createRelative(String relativePath) {

    String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);

    return new ClassPathResource(pathToUse, this.classLoader, this.clazz);

}

InputStreamResource

InputStreamResource是对InputStream的封装,它接收InputStream作为构造函数参数,它的isOpen总是返回true,并且只能被读取一次(即getInputStream方法只能被调用一次),existsisReadable方法也总是返回true。由于它不能被多次读取,只有当不用多次读取的时候才使用该类,并且只有当没有其他可用Resource类时才使用该类。在Spring内部貌似没有使用它。它只实现了getInputStream方法:

public InputStream getInputStream() throws IOException, IllegalStateException {

    if (this.read) {

        throw new IllegalStateException("InputStream has already been read - " +

                "do not use InputStreamResource if a stream needs to be read multiple times");

    }

    this.read = true;

    return this.inputStream;

}

DescriptiveResource

DescriptiveResource是对非物理资源的Description的封装。它实现了getDescription()方法。ResourceDescription属性主要用于错误处理时能更加准确的打印出错位置的信息,DescriptiveResource提供对那些需要提供Resource接口中的Description属性作为错误打印信息的方法自定义的描述信息。比如在BeanDefinitionReader中,在仅仅使用InputSource作为源加载BeanDefinition时,就可以使用DescriptiveResource定义自己的Description,从而在出错信息中可以方便的知道问题源在哪里。

BeanDefinitionResource

SpringResource可以用于非物理资源的抽,BeanDefinitionResource是对BeanDefinition的封装。BeanDefinitionResource类似DescriptiveResource,它也只实现了getDescription()方法,用于在解析某个BeanDefinition出错时显示错误源信息:

public String getDescription() {

    return "BeanDefinition defined in " + this.beanDefinition.getResourceDescription();

}

ContextResource接口

Spring中还定义了ContextResource接口,继承自Resource接口,只包含一个方法:

public interface ContextResource extends Resource {

    String getPathWithinContext();

}

getPathWithContext()方法相对于Context的路径,如ServletContextPortletContextclasspathFileSystem等,在Spring core中它有两个实现类FileSystemContextResourceClassPathContextResource,他们分别是FileSystemResourceLoaderDefaultResourceLoader中的内部类,他们对getPathWithContext()方法的实现只是简单的返回path值。

另外,在Spring Web模块中,有一个ServletContextResource实现类,它使用ServletContextpath作为参数构造,getInputStreamgetURLgetURIgetFile等方法中将实现代理给ServletContext,其中getPathWithContext方法依然返回path字符串:

public boolean exists() {

    try {

        URL url = this.servletContext.getResource(this.path);

        return (url != null);

    }

    catch (MalformedURLException ex) {

        return false;

    }

}

public InputStream getInputStream() throws IOException {

    InputStream is = this.servletContext.getResourceAsStream(this.path);

    if (is == null) {

        throw new FileNotFoundException("Could not open " + getDescription());

    }

    return is;

}

public URL getURL() throws IOException {

    URL url = this.servletContext.getResource(this.path);

    if (url == null) {

        throw new FileNotFoundException(

                getDescription() + " cannot be resolved to URL because it does not exist");

    }

    return url;

}

public File getFile() throws IOException {

    String realPath = WebUtils.getRealPath(this.servletContext, this.path);

    return new File(realPath);

}

public Resource createRelative(String relativePath) {

    String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);

    return new ServletContextResource(this.servletContext, pathToUse);

}

posted on 2012-12-01 12:51 DLevin 阅读(6718) 评论(1)  编辑  收藏 所属分类: Spring

FeedBack:
# re: 深入Spring IOC源码之Resource
2014-04-14 18:14 | luchy
你好,我也有阅读源码的习惯,觉得你写的不错  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: