View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/community/server/src/java/org/astrogrid/community/server/database/configuration/DatabaseConfigurationFactory.java,v $</cvs:source>
3    * <cvs:author>$Author: dave $</cvs:author>
4    * <cvs:date>$Date: 2004/09/16 23:18:08 $</cvs:date>
5    * <cvs:version>$Revision: 1.5 $</cvs:version>
6    *
7    * <cvs:log>
8    *   $Log: DatabaseConfigurationFactory.java,v $
9    *   Revision 1.5  2004/09/16 23:18:08  dave
10   *   Replaced debug logging in Community.
11   *   Added stream close() to FileStore.
12   *
13   *   Revision 1.4.82.1  2004/09/16 09:58:48  dave
14   *   Replaced debug with commons logging ....
15   *
16   *   Revision 1.4  2004/06/18 13:45:20  dave
17   *   Merged development branch, dave-dev-200406081614, into HEAD
18   *
19   *   Revision 1.3.52.1  2004/06/17 13:38:59  dave
20   *   Tidied up old CVS log entries
21   *
22   * </cvs:log>
23   *
24   */
25  package org.astrogrid.community.server.database.configuration ;
26  
27  import org.apache.commons.logging.Log ;
28  import org.apache.commons.logging.LogFactory ;
29  
30  import java.util.Map ;
31  import java.util.HashMap ;
32  
33  import java.io.IOException ;
34  
35  import org.exolab.castor.jdo.Database ;
36  import org.exolab.castor.jdo.DatabaseNotFoundException ;
37  import org.exolab.castor.jdo.PersistenceException ;
38  
39  import org.exolab.castor.mapping.MappingException ;
40  
41  /***
42   * A class to handle database configurations.
43   * This could be refactored as static methods ... depends on how we want to use it.
44   *
45   */
46  public class DatabaseConfigurationFactory
47      {
48      /***
49       * Our debug logger.
50       *
51       */
52      private static Log log = LogFactory.getLog(DatabaseConfigurationFactory.class);
53  
54      /***
55       * Public constructor.
56       *
57       */
58      public DatabaseConfigurationFactory()
59          {
60          //
61          // Not a lot ...
62          //
63          }
64  
65      /***
66       * Our map of database configurations.
67       *
68       */
69      private static Map map = new HashMap() ;
70  
71      /***
72       * Access to a named database configuration.
73       * This looks for a matching DatabaseConfiguration and returns a new JDO Database connection.
74       * @param name - The name of the database configuration.
75       *
76       */
77      public Database getDatabase(String name)
78          throws DatabaseNotFoundException, PersistenceException
79          {
80          //
81          // Try finding a matching configuration.
82          DatabaseConfiguration config = this.getDatabaseConfiguration(name) ;
83          //
84          // If we found a matching configuration.
85          if (null != config)
86              {
87              //
88              // Return the JDO database.
89              return config.getDatabase() ;
90              }
91          //
92          // If we didn't find a matching configuration.
93          else {
94              throw new DatabaseNotFoundException(
95              	"No JDO configuration for '" + name + "'"
96              	) ;
97              }
98          }
99  
100     /***
101      * Access to a named database configuration.
102      * @param name - The name of the database configuration.
103      *
104      */
105     public DatabaseConfiguration getDatabaseConfiguration(String name)
106         {
107         //
108         // Try finding a matching configuration.
109         return (DatabaseConfiguration) map.get(name) ;
110         }
111 
112     /***
113      * Add a database configuration to our map.
114      * @param config - A reference to the DatabaseConfiguration object.
115      *
116      */
117     protected DatabaseConfiguration addDatabaseConfiguration(DatabaseConfiguration config)
118         {
119         return this.addDatabaseConfiguration(
120         	config.getDatabaseName(),
121         	config
122         	) ;
123         }
124 
125     /***
126      * Add a database configuration to our map.
127      * @param name   - The name to associate the DatabaseConfiguration.
128      * @param config - A reference to the DatabaseConfiguration object.
129      *
130      */
131     protected DatabaseConfiguration addDatabaseConfiguration(String name, DatabaseConfiguration config)
132         {
133         //
134         // Check for a null param.
135         if (null != config)
136             {
137             //
138             // Add the configuration to our map.
139             map.put(name, config) ;
140             }
141         return config ;
142         }
143 
144     /***
145      * Load a new database configuratiion.
146      * If a matching database configuration exists, this will just return the current one.
147      * If there is no matching database configuration, then this will try to create a new one.
148      * This looks for a JDO config file with the same name.
149      * @param name - The database name.
150      *
151      */
152     public DatabaseConfiguration loadDatabaseConfiguration(String name)
153         throws IOException, MappingException
154         {
155         log.debug("") ;
156         log.debug("----\"----") ;
157         log.debug("DatabaseConfigurationFactory.loadDatabaseConfiguration()") ;
158         log.debug("  Name : " + name) ;
159         //
160         // Try to find a matching configuration.
161 // PATCH
162 //        DatabaseConfiguration config = this.getDatabaseConfiguration(name) ;
163         DatabaseConfiguration config = null ;
164         //
165         // If we didn't find one.
166         if (null == config)
167             {
168             //
169             // Try to create a new database configuration.
170             config = new DatabaseConfiguration(name) ;
171             //
172             // If we got a new configuration.
173             if (null != config)
174                 {
175                 //
176                 // Add the new configuration to our map.
177                 this.addDatabaseConfiguration(config) ;
178                 }
179             }
180         return config ;
181         }
182 
183     /***
184      * Load a new database configuratiion.
185      * If a matching database configuration exists, this will just return the current one.
186      * If there is no matching database configuration, then this will try to create a new one.
187      * @param name - The database name.
188      * @param xml  - The Castor JDO config file.
189      * @param sql  - The SQL script to create the tables.
190      *
191      */
192     public DatabaseConfiguration loadDatabaseConfiguration(String name, String xml, String sql)
193         throws IOException, MappingException
194         {
195         log.debug("") ;
196         log.debug("----\"----") ;
197         log.debug("DatabaseConfigurationFactory.loadDatabaseConfiguration()") ;
198         log.debug("  Name : " + name) ;
199         log.debug("  XML  : " + xml) ;
200         log.debug("  SQL  : " + sql) ;
201         //
202         // Try to find a matching configuration.
203 // PATCH
204 //        DatabaseConfiguration config = this.getDatabaseConfiguration(name) ;
205         DatabaseConfiguration config = null ;
206         //
207         // If we didn't find one.
208         if (null == config)
209             {
210             //
211             // Create a new database configuration.
212             config = new DatabaseConfiguration(name, xml, sql) ;
213             //
214             // If we got a new configuration.
215             if (null != config)
216                 {
217                 //
218                 // Add the new configuration to our map.
219                 this.addDatabaseConfiguration(config) ;
220                 }
221             }
222         return config ;
223         }
224     }
225