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:
InputStreamSource, Resource

public class OsgiBundleResource
extends AbstractResource

Resource implementation for OSGi environments.

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

As fallback, the path is transformed to an URL thus supporting the underlying OSGi framework specific prefixes. Note that 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

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)
           
 
Method Summary
 Resource createRelative(String relativePath)
          This implementation creates a OsgiBundleResource, applying the given path relative to the path of the underlying resource of this descriptor.
 boolean equals(Object obj)
          This implementation compares the underlying bundle and path locations.
 Bundle getBundle()
          Return the bundle for this resource.
 String getDescription()
          This implementation returns a description that includes the bundle location.
 File getFile()
           
 String getFilename()
          This implementation returns the name of the file that this bundle path resource refers to.
 InputStream getInputStream()
          This implementation opens an InputStream for the given URL.
 String getPath()
          Return the path for this resource.
protected  URL getResourceFromBundleClasspath(String bundlePath)
          Resolves a resource from the bundle's classpath.
protected  URL getResourceFromBundleJar(String bundlePath)
          Resolves a resource from the *bundle jar* only.
protected  URL getResourceFromBundleSpace(String bundlePath)
          Resolves a resource from *the bundle space* only.
protected  URL getResourceFromFilesystem(String fileName)
          Resolves a resource from the filesystem.
 int getSearchType()
           
 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.
protected  boolean isRelativePath(String locationPath)
          Determine if the given path is relative or absolute.
 
Methods inherited from class org.springframework.core.io.AbstractResource
exists, isOpen, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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)
Method Detail

getPath

public final String getPath()
Return the path for this resource.


getBundle

public final Bundle getBundle()
Return the bundle for this resource.


getInputStream

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

Throws:
IOException
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 the classpath of the bundle for example can change during a bundle lifecycle (depending on its imports).

Specified by:
getURL in interface Resource
Overrides:
getURL in class AbstractResource
Throws:
IOException
See Also:
Bundle.getEntry(String), Bundle.getResource(String)

getResourceFromFilesystem

protected URL getResourceFromFilesystem(String fileName)
Resolves a resource from the filesystem.

Parameters:
fileName -
Returns:
a URL to the returned resource or null if none is found

getResourceFromBundleSpace

protected URL getResourceFromBundleSpace(String bundlePath)
                                  throws IOException
Resolves a resource from *the bundle space* only. Only the bundle and its attached fragments are searched for the given resource. Note that this method returns only the first URL found, discarding the rest. To retrieve the entire set, consider using OsgiBundleResourcePatternResolver.

Parameters:
bundlePath - the path to resolve
Returns:
a URL to the returned resource or null if none is found
Throws:
IOException
See Also:
org.osgi.framework.Bundle#findEntries(String, String, boolean)}

getResourceFromBundleJar

protected URL getResourceFromBundleJar(String bundlePath)
                                throws IOException
Resolves a resource from the *bundle jar* only. Only the bundle jar is searched (its attached fragments are ignored).

Parameters:
bundlePath - the path to resolve
Returns:
URL to the specified path or null if none is found
Throws:
IOException
See Also:
Bundle#getEntry(String)}

getResourceFromBundleClasspath

protected URL getResourceFromBundleClasspath(String bundlePath)
Resolves a resource from the bundle's classpath. This will find resources in this bundle and also in imported packages from other bundles.

Parameters:
bundlePath -
Returns:
a URL to the returned resource or null if none is found
See Also:
Bundle.getResource(String)

isRelativePath

protected boolean isRelativePath(String locationPath)
Determine if the given path is relative or absolute.

Parameters:
locationPath -
Returns:

createRelative

public Resource createRelative(String relativePath)
This implementation creates a 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
See Also:
StringUtils.applyRelativePath(String, String)

getFilename

public String getFilename()
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
See Also:
StringUtils.getFilename(String)

getFile

public File getFile()
             throws IOException
Specified by:
getFile in interface Resource
Overrides:
getFile in class AbstractResource
Throws:
IOException

getDescription

public String getDescription()
This implementation returns a description that includes the bundle location.


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

getSearchType

public int getSearchType()
Returns:
Returns the searchType.


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