net.sourceforge.java.feednread.backend.configuration
Class ConnectionManager

java.lang.Object
  extended bynet.sourceforge.java.feednread.backend.configuration.ConnectionManager

public class ConnectionManager
extends Object

Manages and allocates online connections available to the application. All components that need to establish an online connection need to request a free connection slot from the ConnectionManager via its getConnectionSlot(URL) method. It is mandatory to notify the ConnectionManager as soon as the connection is freed up again using the connectionClosed() method to get other demanding objects the chance to allocate a free connection slot.

Author:
Sebastian Machhausen

Field Summary
private  int allocatedConnectionSlots
           The number of allocated connection slots
private  ProxyServerSettings proxy
           The optional proxy server to use for all connections
private static ConnectionManager SINGLETON_INSTANCE
           The single unique ConnectionManager instance
 
Constructor Summary
private ConnectionManager()
           Creates a new ConnectionManager instance.
 
Method Summary
 void connectionClosed()
           Notifies this ConnectionManager that a previously allocated connection slot has been freed up as the connection was closed.
 int getAllocatedConnectionSlots()
           Gets the number of currently alloocated connection slots in this ConnectionManager.
 URLConnection getConnectionSlot(URL url)
           Tries to allocate a free connection slot.
 int getFreeConnectionSlots()
           Gets the number of free connection slots in this ConnectionManager at the moment.
static ConnectionManager getInstance()
           Gets the single unique ConnectionManager instance
 ProxyServerSettings getProxySettings()
           Gets the ProxyServerSettings used for all connections established by this ConnectionManager.
 boolean isFreeConnectionSlot()
           Returns true if a free connection slot is available at the moment.
 void setProxySettings(ProxyServerSettings settings)
           Sets the ProxyServerSettings used for all connections established by this ConnectionManager.
 void setupConnectionSettings()
           Setups the connection settings used for all HttpUrlConnection instances.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

allocatedConnectionSlots

private int allocatedConnectionSlots

The number of allocated connection slots


proxy

private ProxyServerSettings proxy

The optional proxy server to use for all connections


SINGLETON_INSTANCE

private static ConnectionManager SINGLETON_INSTANCE

The single unique ConnectionManager instance

Constructor Detail

ConnectionManager

private ConnectionManager()

Creates a new ConnectionManager instance.

Method Detail

getInstance

public static ConnectionManager getInstance()

Gets the single unique ConnectionManager instance

Returns:
the single unique ConnectionManager instance

isFreeConnectionSlot

public boolean isFreeConnectionSlot()

Returns true if a free connection slot is available at the moment.

Returns:
true if a free connection slot is available at the moment; false otherwise

getConnectionSlot

public URLConnection getConnectionSlot(URL url)
                                throws IOException

Tries to allocate a free connection slot. If the allocation succeeded this method returns an ULRConnection to the specified url.

Note that a connection slot requested by this method has always to be freed up correctly by calling the connectionClosed() method. For the ConnectionManager to work properly it is therefore mandatory to call the connectionClosed() method instantly after a requested URLConnection returned by this method has been closed. Otherwise an allocated connection slot never gets freed up again and stays blocked for other requests made to this ConnectionManager.

Parameters:
url - the URL to connect
Returns:
an ULRConnection to the specified url
Throws:
IOException - if a connection to the specified url could not be established

connectionClosed

public void connectionClosed()

Notifies this ConnectionManager that a previously allocated connection slot has been freed up as the connection was closed. The allocated connection slot gets available again.

Note that a connection slot requested by the getConnectionSlot(URL) method has always to be freed up correctly by calling this method. For the ConnectionManager to work properly it is therefore mandatory to call this method instantly after a requested URLConnection has been closed. Otherwise an allocated connection slot never gets freed up again and stays blocked for other requests made to this ConnectionManager.


getFreeConnectionSlots

public int getFreeConnectionSlots()

Gets the number of free connection slots in this ConnectionManager at the moment.

Returns:
the number of free connection slots in this ConnectionManager at the moment

getAllocatedConnectionSlots

public int getAllocatedConnectionSlots()

Gets the number of currently alloocated connection slots in this ConnectionManager.

Returns:
the number of currently alloocated connection slots in this ConnectionManager

getProxySettings

public ProxyServerSettings getProxySettings()

Gets the ProxyServerSettings used for all connections established by this ConnectionManager.

Returns:
the ProxyServerSettings

setProxySettings

public void setProxySettings(ProxyServerSettings settings)

Sets the ProxyServerSettings used for all connections established by this ConnectionManager.

Parameters:
settings - the ProxyServerSettings to use
Throws:
IllegalArgumentException - if a null value is specified for the settings argument. To switch off a proxy server use the ProxyServerSettings.setEnabled(boolean) method.

setupConnectionSettings

public void setupConnectionSettings()

Setups the connection settings used for all HttpUrlConnection instances.

See Also:
Networking Properties