net.sf.jguiraffe.locators
Class AbstractStreamLocator

java.lang.Object
  extended by net.sf.jguiraffe.locators.AbstractLocator
      extended by 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 the getURL() 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 create java.net.URLConnection objects whose getInputStream() method returns the input stream provided by the concrete Locator implementation.

Concrete subclasses have to implement the getInputStream() method to return the stream they point to. They also have to provide an implementation of the createURL() method. This method is passed a URLStreamHandler object as described above. An implementation of createURL() can create a URL with an arbitrary protocol and other components as it sees fit. As long as the URLStreamHandler is used when creating the URL it is guaranteed that calls to openConnection() or openStream() on the URL object return the stream of the Locator.

Version:
$Id: AbstractStreamLocator.java 156 2009-03-03 21:04:47Z oheger $
Author:
Oliver Heger

Constructor Summary
protected AbstractStreamLocator()
          Creates a new instance of AbstractStreamLocator.
 
Method Summary
protected abstract  java.net.URL createURL(java.net.URLStreamHandler streamHandler)
          Creates the URL managed by this locator.
 java.net.URL getURL()
          Returns the URL managed by this locator.
 
Methods inherited from class net.sf.jguiraffe.locators.AbstractLocator
getFile, getInputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractStreamLocator

protected AbstractStreamLocator()
Creates a new instance of AbstractStreamLocator.

Method Detail

getURL

public java.net.URL getURL()
Returns the URL managed by this locator. On first access this implementation delegates to createURL() 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 that createURL() is invoked multiple times. However, it is guaranteed that this method always returns the same URL.

Returns:
the URL managed by this locator

createURL

protected abstract java.net.URL createURL(java.net.URLStreamHandler streamHandler)
                                   throws java.net.MalformedURLException
Creates the URL managed by this locator. This method is called by getURL() to initialize the URL. An implementation is free to create whatever URL it likes, but it should use the specified URLStreamHandler. 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:
java.net.MalformedURLException - if the URL cannot be created


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.