1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
82 DatabaseConfiguration config = this.getDatabaseConfiguration(name) ;
83
84
85 if (null != config)
86 {
87
88
89 return config.getDatabase() ;
90 }
91
92
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
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
135 if (null != config)
136 {
137
138
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
161
162
163 DatabaseConfiguration config = null ;
164
165
166 if (null == config)
167 {
168
169
170 config = new DatabaseConfiguration(name) ;
171
172
173 if (null != config)
174 {
175
176
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
203
204
205 DatabaseConfiguration config = null ;
206
207
208 if (null == config)
209 {
210
211
212 config = new DatabaseConfiguration(name, xml, sql) ;
213
214
215 if (null != config)
216 {
217
218
219 this.addDatabaseConfiguration(config) ;
220 }
221 }
222 return config ;
223 }
224 }
225