001    package com.sptci.rwt;
002    
003    import java.sql.Connection;
004    
005    /**
006     * A manager used to globally maintain a single means of connecting to a
007     * database.  Connections are obtained through {@link javax.sql.DataSource}
008     * or {@link java.sql.DriverManager}.
009     *
010     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
011     * @author Rakesh Vidyadharan 2007-09-26
012     * @version $Id: ConnectionManager.java 4123 2008-05-25 21:49:01Z rakesh $
013     */
014    public class ConnectionManager
015    {
016      /**
017       * The name of the data source to use to obtain connections.
018       */
019      private String dataSource;
020    
021      /**
022       * The connection parameters to use to obtain connections.
023       */
024      private ConnectionParameters parameters;
025    
026      /**
027       * Default constructor.  Cannot be instantiated.
028       */
029      private ConnectionManager() {}
030    
031      /**
032       * Create a new instance of the class configured to use the specifiedj
033       * {@link #dataSource}.
034       *
035       * @param dataSource The {@link #dataSource} value to use.
036       */
037      public ConnectionManager( final String dataSource )
038      {
039        setDataSource( dataSource );
040      }
041    
042      /**
043       * Create a new instance of the class configured to use the specified
044       * {@link #parameters}.
045       *
046       * @param parameters The {@link #parameters} to use.
047       */
048      public ConnectionManager( final ConnectionParameters parameters )
049      {
050        setParameters( parameters );
051      }
052    
053      /**
054       * Open a connection to the database using the currently active {@link
055       * #dataSource} or {@link #parameters}.
056       *
057       * @see ConnectionFactory#open( String )
058       * @see ConnectionFactory#open( ConnectionParameters )
059       * @return The connection to the database.
060       * @throws ConnectionException If errors are encountered while fetching 
061       *   the connection to the database.  Can also be thrown if this instance
062       *   has not been initialised through {@link #setDataSource} or {@link
063       *   #setParameters}.
064       */
065      public Connection open() throws ConnectionException
066      {
067        return ( ( dataSource == null ) ? 
068            ConnectionFactory.open( parameters ) :
069            ConnectionFactory.open( dataSource ) );
070      }
071    
072      /**
073       * Close the specified connection.
074       *
075       * @see ConnectionFactory#close
076       */
077      public void close( final Connection connection )
078      {
079        ConnectionFactory.close( connection );
080      }
081      
082      /**
083       * Returns {@link #dataSource}.
084       *
085       * @return The value/reference of/to dataSource.
086       */
087      public String getDataSource()
088      {
089        return dataSource;
090      }
091      
092      /**
093       * Set {@link #dataSource}.  If a valid string was specified, sets {@link
094       * #parameters} to <code>null</code>.
095       *
096       * @param dataSource The value to set.
097       */
098      public void setDataSource( final String dataSource )
099      {
100        this.dataSource = dataSource;
101        if ( dataSource != null ) parameters = null;
102      }
103      
104      /**
105       * Returns {@link #parameters}.
106       *
107       * @return The value/reference of/to parameters.
108       */
109      public ConnectionParameters getParameters()
110      {
111        return parameters;
112      }
113      
114      /**
115       * Set {@link #parameters}.  If valid parameters were specified, sets
116       * {@link #dataSource} to <code>null</code>.
117       *
118       * @param parameters The value to set.
119       */
120      public void setParameters( final ConnectionParameters parameters )
121      {
122        this.parameters = parameters;
123        if ( parameters != null ) dataSource = null;
124      }
125      
126      /**
127       * Return a name indicating the current connection source.
128       * 
129       * @return The name inidicating the connection source in use.
130       */
131      public String getTitle()
132      {
133        String result = "";
134        if ( dataSource != null )
135        {
136          result = dataSource;
137        }
138        else
139        {
140          result = parameters.databaseType + "(" + parameters.database + ":" +
141              parameters.userName + ")";
142        }
143        
144        return result;
145      }
146    }