com.tomgibara.pronto.config.source
Class URLConfigSource

java.lang.Object
  extended by com.tomgibara.pronto.config.source.URLConfigSource
All Implemented Interfaces:
ConfigSource

public class URLConfigSource
extends java.lang.Object
implements ConfigSource

Accesses a (possibly) remote resource that is been specified via a URL and parses it into a collection of property values. Instances of this class are not safe for concurrent use. Wrap instances in a SynchronousConfigSource if thread safety is required.

Author:
Tom Gibara

Constructor Summary
URLConfigSource(java.net.URL url)
          Constructs a source which reads its properties from a URL using a DefaultPropertiesReader.
URLConfigSource(java.net.URL url, PropertiesReader reader)
          Constructs a source which reads its properties from a URL using the supplied properties reader.
 
Method Summary
protected  java.net.URLConnection connectToURL()
          This method is responsible for obtaining a connection to the URL for this source, configuring it, calling the connect() method on it and then returning the connection.
 java.util.Map<java.lang.String,java.lang.String> getProperties()
          A map of the configuration properties supplied by this source.
 PropertiesReader getReader()
          The reader which converts this source's resources into properties.
 java.net.URL getURL()
          The URL from which the properties are loaded.
 long lastModified()
          Returns the time at which the properties were last known to have changed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLConfigSource

public URLConfigSource(java.net.URL url)
Constructs a source which reads its properties from a URL using a DefaultPropertiesReader.

Parameters:
url - the location of the resource which contains the properties to be loaded, not null

URLConfigSource

public URLConfigSource(java.net.URL url,
                       PropertiesReader reader)
Constructs a source which reads its properties from a URL using the supplied properties reader. If a null reader is supplied to this constructor, a DefaultPropertiesReader is used.

Parameters:
url - the location of the resource which contains the properties to be loaded, not null
reader - a properties reader which can convert the resource into a set of properties, may be null
Method Detail

getURL

public java.net.URL getURL()
The URL from which the properties are loaded.

Returns:
the url for this source, never null

getReader

public PropertiesReader getReader()
The reader which converts this source's resources into properties.

Returns:
the properties reader for this source

lastModified

public long lastModified()
                  throws java.lang.RuntimeException
Description copied from interface: ConfigSource
Returns the time at which the properties were last known to have changed. This method must be implemented and MAY NOT return rogue values if the time is unknown. The correct behaviour in such circumstances is to return the current system time. If calculation of the time fails, the implementation is free to throw a runtime exception of its choosing.

Specified by:
lastModified in interface ConfigSource
Returns:
the last time at which the resource identified by this source's URL was declared to have changed
Throws:
java.lang.RuntimeException - if an IOException was raised when accessing the resource

getProperties

public java.util.Map<java.lang.String,java.lang.String> getProperties()
                                                               throws java.lang.RuntimeException
Description copied from interface: ConfigSource
A map of the configuration properties supplied by this source. The map returned by this method will not be modified by the config. The implementation is free to throw an unchecked exception of its choice if the properties cannot be supplied.

Specified by:
getProperties in interface ConfigSource
Returns:
the properties read from the resource identified by this source's URL
Throws:
java.lang.RuntimeException - if an IOException was raised when accessing the resource

connectToURL

protected java.net.URLConnection connectToURL()
                                       throws java.io.IOException
This method is responsible for obtaining a connection to the URL for this source, configuring it, calling the connect() method on it and then returning the connection. This method may be overridden to customize the connection to the URL. The default implementation simply opens the connection, calls connect and then returns.

Returns:
an opened, connected URLConnection
Throws:
java.io.IOException - if the connection raises an IOException


Copyright © 2006-2007 Tom Gibara. All Rights Reserved.