Class AbstractStreamLocator
- java.lang.Object
-
- net.sf.jguiraffe.locators.AbstractLocator
-
- net.sf.jguiraffe.locators.AbstractStreamLocator
-
- All Implemented Interfaces:
Locator
- Direct Known Subclasses:
ByteArrayLocator
public abstract class AbstractStreamLocator extends AbstractLocator
An abstract base class for
Locator
implementations that mainly operate on streams.The
Locator
interface requires that thegetURL()
method has to be implemented in a meaningful way. However, there are some use cases where data is available only as a stream (e.g. in some binary form), and it is not obvious how a URL can be constructed to point to this data. This class is designed to support such use cases.The basic idea is that this class provides a specialized implementation of the abstract
java.net.URLStreamHandler
class. This implementation can createjava.net.URLConnection
objects whosegetInputStream()
method returns the input stream provided by the concreteLocator
implementation.Concrete subclasses have to implement the
getInputStream()
method to return the stream they point to. They also have to provide an implementation of thecreateURL()
method. This method is passed aURLStreamHandler
object as described above. An implementation ofcreateURL()
can create a URL with an arbitrary protocol and other components as it sees fit. As long as theURLStreamHandler
is used when creating the URL it is guaranteed that calls toopenConnection()
oropenStream()
on the URL object return the stream of theLocator
.- Version:
- $Id: AbstractStreamLocator.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractStreamLocator()
Creates a new instance ofAbstractStreamLocator
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract URL
createURL(URLStreamHandler streamHandler)
Creates the URL managed by this locator.URL
getURL()
Returns theURL
managed by this locator.-
Methods inherited from class net.sf.jguiraffe.locators.AbstractLocator
getFile, getInputStream
-
-
-
-
Method Detail
-
getURL
public URL getURL()
Returns theURL
managed by this locator. On first access this implementation delegates tocreateURL()
for creating the URL. Then the URL is cached and directly returned on subsequent requests. Note that a non-blocking algorithm is used for lazily creating the URL. So on concurrent access it may happen thatcreateURL()
is invoked multiple times. However, it is guaranteed that this method always returns the sameURL
.- Returns:
- the
URL
managed by this locator
-
createURL
protected abstract URL createURL(URLStreamHandler streamHandler) throws MalformedURLException
Creates the URL managed by this locator. This method is called bygetURL()
to initialize the URL. An implementation is free to create whatever URL it likes, but it should use the specifiedURLStreamHandler
. This handler ensures that reading from the URL is delegated to the stream managed by this locator.- Parameters:
streamHandler
- the stream handler to use when creating the URL- Returns:
- the URL to be returned by
getURL()
- Throws:
MalformedURLException
- if the URL cannot be created
-
-