001    package hirondelle.web4j.database;
002    
003    import java.sql.Connection;
004    import java.util.Set;
005    import javax.servlet.ServletConfig;
006    
007    /**
008     Return a {@link Connection} to a database, using a policy defined by the application. 
009     
010     <P>See {@link hirondelle.web4j.BuildImpl} for important information on how this item is configured. 
011     {@link hirondelle.web4j.BuildImpl#forConnectionSource()} 
012     returns the configured implementation of this interface.
013     
014     <P>Here is an example 
015     <a href="http://www.javapractices.com/apps/fish/javadoc/src-html/hirondelle/web4j/config/ConnectionSrc.html">implementation</a>, 
016     taken from the WEB4J example application.
017     
018     <P>There are many alternatives for creating or accessing database connections.
019     The application programmer can use any technique whatsoever to implement this 
020     interface. For example, an implementation may :  
021     <ul>
022     <li>use a connection pool configured in the server environment (the style  
023     used in the example application)
024     <li>create a {@link Connection} directly, using the database driver. (Some modern 
025     drivers have connection pooling built directly into the driver.)
026     <li>use some other means
027     </ul>
028    
029     <P><span class="highlight">The design of this interface is slightly skewed towards applications 
030     that use more than one database.</span> 
031     If only one database is used in an application, then this interface is simply implemented
032     'as if' there were more than one. In practice, this should be only a minor nuisance. In addition, 
033     if a single-database application is extended to use more than one database, the changes will 
034     be absorbed naturally by the existing implementation. 
035     
036     <P>If no database is used at all, then {@link #getDatabaseNames()} returns an emtpy <tt>Set</tt>.
037    
038     <P>See {@link SqlId} for related information.
039    */
040    public interface ConnectionSource {
041      
042      /**
043       Read in any necessary configuration parameters.
044       
045       <P>This method is by the framework, early in startup processing, to allow the framework to use connections 
046       during its initialization. If your implementation of this interface does not use <tt>web.xml</tt> for 
047       its config, then it can simply ignore the <tt>ServletConfig</tt> parameter. 
048      */
049      void init(ServletConfig aConfig);
050      
051      /**
052       Return the database names accepted by {@link #getConnection(String)}.
053       
054       <P>Return a <tt>Set</tt> with one or more entries. Each entry must have content.
055       If your application does not use a database at all, then you must return an empty <tt>Set</tt>.
056      */
057      Set<String> getDatabaseNames();  
058    
059      /**
060       Return a {@link Connection} to the <em>default</em> database.
061       
062       <P>The default database is selected by the application programmer as representing 
063       the principal or main database in the application, against which the majority of 
064       SQL statements are executed. (For most applications, this choice will be obvious.) 
065       In <tt>.sql</tt> files, SQL statements against the default database 
066       do not need an extra qualifier in their identifiers, while those against a non-default 
067       database do need an extra qualifier.
068       
069       <P>Implementations must translate any exceptions into a {@link DAOException}.
070       Examples of exceptions that may need translation are <tt>SQLException</tt> 
071       and <tt>javax.naming.NamingException</tt>.
072      */
073      Connection getConnection() throws DAOException;
074      
075      /**
076       Return a {@link Connection} for a specified database (default or non-default). 
077       
078       <P>The {@link #getConnection()} method is intended for the "default" or 
079       principal database used in the application, while this method is 
080       intended for all other databases (but may be used for the default as well).
081       
082       <P>See {@link SqlId} for related information.
083       
084       <span class="highlight">Here, <tt>aDatabaseName</tt> is a prefix used 
085       in <tt>.sql</tt> files as a qualifer to a SQL statement identifier.</span> 
086       For example, in an <tt>.sql</tt> file, the SQL statement identifier: 
087       <ul>
088       <li><tt>LIST_MEMBERS</tt> refers to the default database (no qualifier)
089       <li><tt>TRANSLATION.LOCALE_LIST</tt> refers to the '<tt>TRANSLATION</tt>' database. 
090       <em>It is this string </em>- '<tt>TRANSLATION</tt>' - <em>which is passed to this method</em>. 
091       </ul>  
092       
093       <P>The values taken by <tt>aDatabaseName</tt> are those returned by {@link #getDatabaseNames()}.
094       They may be chosen by the application programmer in any desired way, to clarify the SQL 
095       statement identifiers appearing in the <tt>.sql</tt> file(s).
096       Typically, an implementation of this interface will internally map these database 
097       identifiers into specific connection strings, such as 
098       '<tt>java:comp/env/jdbc/blah</tt>' (or whatever is required).
099        
100       <P>Implementations must translate any exceptions into a {@link DAOException}.
101       Examples of exceptions that may need translation are <tt>SQLException</tt> 
102       and <tt>javax.naming.NamingException</tt>.
103      */
104      Connection getConnection(String aDatabaseName) throws DAOException;
105    }