org.springframework.osgi.io
Class OsgiBundleResource

java.lang.Object
  extended by org.springframework.core.io.AbstractResource
      extended by org.springframework.osgi.io.OsgiBundleResource
All Implemented Interfaces:
ContextResource, InputStreamSource, Resource

public class OsgiBundleResource
extends AbstractResource
implements ContextResource

Resource implementation for OSGi environments.

Lazy evaluation of the resource will be used. This implementation allows resource location inside:

For more explanations on resource locations in OSGi, please see the Access to Resources chapter from the OSGi spec.

OSGi framework specific prefixes (such as bundleentry: and bundleresource:under Equinox, bundle: under Knopflefish and Felix, etc..) are supported. Resources outside the OSGi space (file:, http:, etc..) are supported as well as the path is being resolved to an URL.

If no prefix is specified, the bundle space will be used for locating a resource.

Note: When the bundle space (bundle jar and its attached fragments) is being searched, multiple URLs can be found but this implementation will return only the first one. Consider using OsgiBundleResourcePatternResolver to retrieve all entries.

Author:
Costin Leau, Adrian Colyer, Sam Brannen

Field Summary
static String BUNDLE_JAR_URL_PREFIX
          Prefix for searching only the bundle raw jar.
static String BUNDLE_URL_PREFIX
          Prefix for searching inside the owning bundle space.
 
Constructor Summary
OsgiBundleResource(Bundle bundle, String path)
          Constructs a new OsgiBundleResource instance.
 
Method Summary
 Resource createRelative(String relativePath)
          Returns a resource relative to this resource.
 boolean equals(Object obj)
          

This implementation compares the underlying bundle and path locations.

 boolean exists()
          Return whether this resource actually exists in physical form.
 String getDescription()
          

This implementation returns a description that includes the bundle location.

 File getFile()
          Returns a File handle for this resource.
 String getFilename()
          Returns the filename of this resources.
 InputStream getInputStream()
          Returns an InputStream to this resource.
 String getPathWithinContext()
           
 URL getURL()
          Locates the resource in the underlying bundle based on the prefix, if it exists.
 int hashCode()
          

This implementation returns the hash code of the underlying class path location.

 long lastModified()
           
 
Methods inherited from class org.springframework.core.io.AbstractResource
getFileForLastModifiedCheck, getURI, isOpen, isReadable, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.springframework.core.io.Resource
getURI, isOpen, isReadable
 

Field Detail

BUNDLE_URL_PREFIX

public static final String BUNDLE_URL_PREFIX
Prefix for searching inside the owning bundle space. This translates to searching the bundle and its attached fragments. If no prefix is specified, this one will be used.

See Also:
Constant Field Values

BUNDLE_JAR_URL_PREFIX

public static final String BUNDLE_JAR_URL_PREFIX
Prefix for searching only the bundle raw jar. Will ignore attached fragments. Not used at the moment.

See Also:
Constant Field Values
Constructor Detail

OsgiBundleResource

public OsgiBundleResource(Bundle bundle,
                          String path)
Constructs a new OsgiBundleResource instance.

Parameters:
bundle - OSGi bundle used by this resource
path - resource path inside the bundle.
Method Detail

getInputStream

public InputStream getInputStream()
                           throws IOException
Returns an InputStream to this resource. This implementation opens an InputStream for the given URL. It sets the UseCaches flag to false, mainly to avoid jar file locking on Windows.

Specified by:
getInputStream in interface InputStreamSource
Returns:
input stream to the underlying resource
Throws:
IOException - if the stream could not be opened
See Also:
URL.openConnection(), URLConnection.setUseCaches(boolean), URLConnection.getInputStream()

getURL

public URL getURL()
           throws IOException
Locates the resource in the underlying bundle based on the prefix, if it exists. Note that the location happens per call since due to the dynamic nature of OSGi, the classpath of the bundle (among others) can change during a bundle lifecycle (depending on its imports).

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
Returns:
URL to this resource
Throws:
IOException - if the resource cannot be resolved as URL, i.e. if the resource is not available as descriptor
See Also:
Bundle.getEntry(String), Bundle.getResource(String)

createRelative

public Resource createRelative(String relativePath)
Returns a resource relative to this resource. This implementation creates an OsgiBundleResource, applying the given path relative to the path of the underlying resource of this descriptor.

Specified by:
createRelative in interface Resource
Overrides:
createRelative in class AbstractResource
Parameters:
relativePath - the relative path (relative to this resource)
Returns:
the resource handle for the relative resource
Throws:
IOException - if the relative resource cannot be determined
See Also:
StringUtils.applyRelativePath(String, String)

getFilename

public String getFilename()
Returns the filename of this resources. This implementation returns the name of the file that this bundle path resource refers to.

Specified by:
getFilename in interface Resource
Overrides:
getFilename in class AbstractResource
Returns:
resource filename
See Also:
StringUtils.getFilename(String)

getFile

public File getFile()
             throws IOException
Returns a File handle for this resource. This method does a best-effort attempt to locate the bundle resource on the file system. It is strongly recommended to use getInputStream() method instead which works no matter if the bundles are saved (in exploded form or not) on the file system.

Specified by:
getFile in interface Resource
Overrides:
getFile in class AbstractResource
Returns:
File handle to this resource
Throws:
IOException - if the resource cannot be resolved as absolute file path, i.e. if the resource is not available in a file system

getDescription

public String getDescription()

This implementation returns a description that includes the bundle location.

Specified by:
getDescription in interface Resource

equals

public boolean equals(Object obj)

This implementation compares the underlying bundle and path locations.

Overrides:
equals in class AbstractResource

hashCode

public int hashCode()

This implementation returns the hash code of the underlying class path location.

Overrides:
hashCode in class AbstractResource

lastModified

public long lastModified()
                  throws IOException
Specified by:
lastModified in interface Resource
Overrides:
lastModified in class AbstractResource
Throws:
IOException

getPathWithinContext

public String getPathWithinContext()
Specified by:
getPathWithinContext in interface ContextResource

exists

public boolean exists()
Return whether this resource actually exists in physical form.

This method performs a definitive existence check, whereas the existence of a Resource handle only guarantees a valid descriptor handle.

The existence check is done by opening an InputStream to the underlying resource (overriding the default implementation which checks first for the presence of a File).

Specified by:
exists in interface Resource
Overrides:
exists in class AbstractResource


Copyright © 2006-2009 Spring Framework. All Rights Reserved.