View Javadoc

1   package pl.aislib.fm;
2   
3   import java.util.Map;
4   import java.util.HashMap;
5   import java.util.Iterator;
6   
7   import java.sql.Connection;
8   
9   import org.apache.commons.logging.Log;
10  
11  import pl.aislib.fm.jdbc.Manager;
12  
13  /***
14   * Container for set of <code>Database</code> instances.
15   *
16   * @author <a href="mailto:pikus@ais.pl">Tomasz Pik</a>, AIS.PL
17   * @version $Revision: 1.2 $
18   * @since AISLIB 0.2
19   */
20  public class KeyedDatabase extends Database {
21  
22    /***
23     * Database instances.
24     */
25    protected Map databases;
26    
27    
28    // Constructors
29    
30    /***
31     *
32     */
33    public KeyedDatabase() {
34      super(new NopManager());
35      databases = new HashMap();
36    }
37  
38  
39    // Public methods
40    
41    /***
42     * Puts database to map.
43     * 
44     * @param databaseName name of database.
45     * @param database <code>Database</code> implementation.
46     */
47    public void addDatabase(String databaseName, Database database) {
48      databases.put(databaseName, database);
49    }
50  
51    /***
52     * @param databaseName name of database.
53     * @return <code>Database</code> implementation from map.
54     */
55    public Database getDatabase(String databaseName) {
56      return (Database) databases.get(databaseName);
57    }
58  
59  
60    // Package methods
61    
62    /***
63     * @see pl.aislib.fm.Database#setLog(org.apache.commons.logging.Log)
64     */
65    void setLog(Log _log) {
66      super.setLog(_log);
67      Iterator iterator = databases.values().iterator();
68      while (iterator.hasNext()) {
69        Database database = (Database) iterator.next();
70        database.setLog(_log);
71      }
72    }
73  
74  
75    // Private classes
76    
77    /***
78     * Not supported manager implementation.
79     * 
80     * @author <a href="mailto:pikus@ais.pl">Tomasz Pik</a>, AIS.PL
81     */
82    private static class NopManager extends Manager {
83      
84      // Public methods
85      
86      /***
87       * @see pl.aislib.fm.jdbc.Manager#getConnection()
88       */
89      public Connection getConnection() {
90        throw new UnsupportedOperationException("This manager doesn't operate on database");
91      }
92  
93      /***
94       * @see pl.aislib.fm.jdbc.Manager#releaseConnection(java.sql.Connection)
95       */
96      public void releaseConnection(Connection con) {
97        throw new UnsupportedOperationException("This manager doesn't operate on database");
98      }
99  
100   } // NopManager class
101 
102 } // KeyedDatabase class