org.stringer
Class Resource

java.lang.Object
  |
  +--org.stringer.Resource
All Implemented Interfaces:
java.io.Serializable

public class Resource
extends java.lang.Object
implements java.io.Serializable

A class to locate resources, retrieve their contents, and determine their last modified time. To find the resource the class searches the CLASSPATH first, then Resource.class.getResource("/" + name). If the Resource finds a "file:" URL, the file path will be treated as a file. Otherwise, the path is treated as a URL and has limited last modified info. From the book "Java Enterprise Best Practices" published by O'Reilly

See Also:
Serialized Form

Field Summary
private  java.io.File file
           
private  java.lang.String name
           
private  java.net.URL url
           
 
Constructor Summary
Resource(java.lang.String name)
           
 
Method Summary
 java.lang.String getDirectory()
          Returns the directory containing the resource, or null if the resource isn't directly available on the filesystem.
 java.io.InputStream getInputStream()
          Returns an input stream to read the resource contents
 java.lang.String getName()
          Returns the resource name, as passed to the constructor
 long lastModified()
          Returns when the resource was last modified.
private static java.io.File searchDirectories(java.lang.String[] paths, java.lang.String filename)
           
private static java.lang.String[] split(java.lang.String str, java.lang.String delim)
           
 java.lang.String toString()
           
private  boolean tryClasspath(java.lang.String filename)
           
private  boolean tryLoader(java.lang.String name)
           
private static java.io.File urlToFile(java.net.URL res)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

name

private java.lang.String name

file

private java.io.File file

url

private java.net.URL url
Constructor Detail

Resource

public Resource(java.lang.String name)
         throws java.io.IOException
Method Detail

getName

public java.lang.String getName()
Returns the resource name, as passed to the constructor


getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an input stream to read the resource contents

java.io.IOException

lastModified

public long lastModified()
Returns when the resource was last modified. If the resource was found using a URL, this method will work only if the URL connection supports last modified information. If there's no support, Long.MAX_VALUE is returned. Perhaps this should return -1, but you should return MAX_VALUE on the assumption that if you can't determine the time, it's maximally new.


getDirectory

public java.lang.String getDirectory()
Returns the directory containing the resource, or null if the resource isn't directly available on the filesystem. This value can be used to locate the configuration file on disk, or to write files in the same directory.


tryClasspath

private boolean tryClasspath(java.lang.String filename)

searchDirectories

private static java.io.File searchDirectories(java.lang.String[] paths,
                                              java.lang.String filename)

split

private static java.lang.String[] split(java.lang.String str,
                                        java.lang.String delim)

tryLoader

private boolean tryLoader(java.lang.String name)

urlToFile

private static java.io.File urlToFile(java.net.URL res)

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


Copyright © R. Kevin Cole. All Rights Reserved.