public class ClasspathResource extends AbstractDataResource
DataResource
interface for a resource that comes from the
classpath
.
MyClass.class.getResourceAsStream
("config.xml")
This will NOT allow to override resources in other classpath entries and especially NOT work in situations where
there are specific classloaders, what is a typical situation in environments of applications servers or IoC
frameworks. context-class-loader
to get resources what is done
by this implementation. A proper version of the example above is:
DataResource
resource =ClasspathResource
((MyClass.class.getPackage(), "config.xml")); if (!resource.isAvailable()
) { // possible fallback resource = getFallbackResource(); } InputStream inStream = resource.openStream()
; ...
Modifier and Type | Field and Description |
---|---|
private String |
path |
static String |
SCHEME_PREFIX
The
scheme-prefix for this type of DataResource . |
private URL |
url |
Constructor and Description |
---|
ClasspathResource(Class<?> someClass,
String nameOrSuffix,
boolean append)
The constructor for a classpath-resource identified by
someClass and the given nameOrSuffix . |
ClasspathResource(Package somePackage,
String filename)
The constructor.
|
ClasspathResource(String absoluteClasspath)
The constructor.
|
Modifier and Type | Method and Description |
---|---|
private static String |
getAbsolutePath(Class<?> someClass,
String nameOrSuffix,
boolean append) |
Date |
getLastModificationDate()
This method gets the last modification date of the
DataResource if available and
supported. |
String |
getPath()
This method gets the path of this resource.
|
String |
getSchemePrefix()
This method gets the scheme-prefix of absolute
URIs for this type of
DataResource . |
String |
getUri()
This method gets a string identifying this
DataResource . |
URL |
getUrl()
This method gets this resource as
URL . |
boolean |
isData()
This method determines if this resource has potentially data
available . |
DataResource |
navigate(String resourcePath)
This method retrieves a
DataResource pointing to the given resourcePath based on this resource.E.g. |
equals, getName, getSize, hashCode, isAvailable, isModifiedSince, openOutputStream, openStream, toString
public static final String SCHEME_PREFIX
scheme-prefix
for this type of DataResource
.private final String path
private final URL url
public ClasspathResource(String absoluteClasspath)
absoluteClasspath
- is the absolute path to the resource. E.g.
"net/sf/mmm/util/resource/ClasspathResource.txt".public ClasspathResource(Class<?> someClass, String nameOrSuffix, boolean append)
someClass
and the given nameOrSuffix
. newThis is the same as:ClasspathResource
(ClasspathResource
.class, ".xml", true)
newThis is the same as:ClasspathResource
(ClasspathResource
.class, "ClasspathResource.xml", false)
new ClasspathResource
("net/sf/mmm/util/resource/ClasspathResource.xml")
someClass
- is the class identifying the path where the resource is located and the prefix of its filename.nameOrSuffix
- is the filename of the resource or a suffix (e.g. ".properties" or "-test.xml") for it
depending on append
.append
- - if true
the nameOrSuffix
is appended to the simple
classname
of someClass
or false
if the simple name is replaced by nameOrSuffix
.ClasspathResource(Package, String)
public ClasspathResource(Package somePackage, String filename)
somePackage
and the given filename
. newThis is the same as:ClasspathResource
(ClasspathResource
.class.getPackage
(), "relection.properties")
newClasspathResource
(ClasspathResource
.class, "relection.properties", false)
somePackage
- is the package identifying the path where the resource is located.filename
- is the name of the resource.ClasspathResource(Class, String, boolean)
public String getSchemePrefix()
AbstractDataResource
URIs
for this type of
DataResource
. The scheme-prefix has the following form: <scheme>
:<suffix>
where <suffix>
is the empty string or something like //
.getSchemePrefix
in class AbstractDataResource
private static String getAbsolutePath(Class<?> someClass, String nameOrSuffix, boolean append)
someClass
- is the class identifying the path where the resource is located and the prefix of its filename.nameOrSuffix
- is the filename of the resource or a suffix (e.g. ".properties" or "-test.xml") for it
depending on append
.append
- - if true
the nameOrSuffix
is appended to the simple
classname
of someClass
or false
if the simple name is replaced by nameOrSuffix
.ClasspathResource(Class, String, boolean)
public boolean isData()
DataResource
available
. Unlike
DataResource.isAvailable()
this method will not invoke expensive operations like connecting to remote URLs. If this
method will return false
, then DataResource.isAvailable()
would also have returned false
. However in
case of true
only DataResource.isAvailable()
can guarantee if a resource really exists and contains data. E.g.
if the resource points to a File
then this method can check if it is a data-file
. So in case it points to a directory or does not exist at all in the filesystem, this method will return
false
. Please also note that this may invoke expensive operations if the according directory path points to
something like a network share. You should also be aware that the state of DataResource.isData()
and
DataResource.isAvailable()
can change at any time so you never have a full guarantee if some data exists or NOT.
However in most cases it is very improbable that this status changes when you read
the
resource immediately after the check.true
if this resource points to potential data, false
otherwise.public String getPath()
DataResource
name
of
the resource. getPath
in interface DataResource
getPath
in class AbstractDataResource
public URL getUrl() throws ResourceNotAvailableException
DataResource
URL
.ResourceNotAvailableException
- if an URL can NOT be created because the represented resource does not exist.public Date getLastModificationDate()
DataResource
DataResource
if available
and
supported.Date
or null
if not available or supported.public String getUri()
DataResource
DataResource
. In most cases this will be the same as
string-representation
of the URL
. However this method will not throw an
exception.getUri
in interface DataResource
getUri
in class AbstractDataResource
public DataResource navigate(String resourcePath)
DataResource
DataResource
pointing to the given resourcePath
based on this resource.resourcePath
would be "../apt/sources.list"
the resulting resource would point to "/etc/apt/sources.list". folder
of this resource. This will also be the case if this resource itself is
a folder
. Due to this generic API this is the only consistent way as there are
implementations that can not easily know if they represent a folder
or a
file
or maybe even both at the same time. However, please be aware of this and
do not get confused as you might expect this to be a cd
command if invoked on
a folder
what is not exactly not the case (instead it is a
cd
on the parent folder). On a BrowsableResource
you can use
BrowsableResource.cd(String)
instead to avoid confusion.resourcePath
- the absolute or relative path pointing to a new resource. If it is a relative path, it is
interpreted relative to the parent URI (directory) of this resource.BrowsableResource.cd(String)
Copyright © 2001–2016 mmm-Team. All rights reserved.