1 package org.apache.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.sql.Connection;
20
21 import org.apache.commons.configuration.Configuration;
22
23 import org.apache.torque.adapter.DB;
24 import org.apache.torque.manager.AbstractBaseManager;
25 import org.apache.torque.map.DatabaseMap;
26
27 /***
28 * A static facade wrapper around the Torque implementation (which is in
29 * {@link org.apache.torque.TorqueInstance}).
30 * <br/>
31 * For historical reasons this class also contains a thin object which can
32 * be used to configure Torque. This is deprecated and will be removed in the
33 * future in favour of using Torque as an Avalon Component.
34 *
35 * @todo This class will be made abstract once Stratum is removed.
36 *
37 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
38 * @author <a href="mailto:magnus@handtolvur.is">Magn�s ��r Torfason</a>
39 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
40 * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
41 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
42 * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
43 * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a>
44 * @version $Id: Torque.java,v 1.93 2004/02/22 06:19:56 jmcnally Exp $
45 */
46 public class Torque
47 {
48 /***
49 * Name of property that specifies the default map builder and map.
50 */
51 public static final String DATABASE_DEFAULT = "database.default";
52
53 /***
54 * A prefix for <code>Manager</code> properties in the configuration.
55 */
56 public static final String MANAGER_PREFIX = "managed_class.";
57
58 /***
59 * A <code>Service</code> property determining its implementing
60 * class name .
61 */
62 public static final String MANAGER_SUFFIX = ".manager";
63
64 /***
65 * property to determine whether caching is used.
66 */
67 public static final String CACHE_KEY = "manager.useCache";
68
69 /***
70 * The single instance of {@link TorqueInstance} used by the
71 * static API presented by this class.
72 */
73 private static TorqueInstance torqueSingleton = null;
74
75 /***
76 * This is a member variable of Torque objects created by the Stratum
77 * lifecycle
78 */
79 private Configuration memberConfig = null;
80
81 /***
82 * C'tor for usage with the Stratum Lifecycle.
83 *
84 * @todo Should be made private or protected once Stratum is removed.
85 */
86 public Torque()
87 {
88 }
89
90 /***
91 * Retrieves the single {@link org.apache.torque.TorqueInstance}
92 * used by this class.
93 *
94 * @return Our singleton.
95 */
96 public static TorqueInstance getInstance()
97 {
98 if (torqueSingleton == null)
99 {
100 torqueSingleton = new TorqueInstance();
101 }
102 return torqueSingleton;
103 }
104
105 /***
106 * Initialization of Torque with a properties file.
107 *
108 * @param configFile The absolute path to the configuration file.
109 * @throws TorqueException Any exceptions caught during processing will be
110 * rethrown wrapped into a TorqueException.
111 */
112 public static void init(String configFile)
113 throws TorqueException
114 {
115 getInstance().init(configFile);
116 }
117
118 /***
119 * Initialization of Torque with a properties file.
120 *
121 * @param conf The Torque configuration.
122 * @throws TorqueException Any exceptions caught during processing will be
123 * rethrown wrapped into a TorqueException.
124 */
125 public static void init(Configuration conf)
126 throws TorqueException
127 {
128 getInstance().init(conf);
129 }
130
131 /***
132 * Determine whether Torque has already been initialized.
133 *
134 * @return true if Torque is already initialized
135 */
136 public static boolean isInit()
137 {
138 return getInstance().isInit();
139 }
140
141 /***
142 * Sets the configuration for Torque and all dependencies.
143 *
144 * @param conf the Configuration
145 */
146 public static void setConfiguration(Configuration conf)
147 {
148 getInstance().setConfiguration(conf);
149 }
150
151 /***
152 * Get the configuration for this component.
153 *
154 * @return the Configuration
155 */
156 public static Configuration getConfiguration()
157 {
158 return getInstance().getConfiguration();
159 }
160
161 /***
162 * This method returns a Manager for the given name.
163 *
164 * @param name name of the manager
165 * @return a Manager
166 */
167 public static AbstractBaseManager getManager(String name)
168 {
169 return getInstance().getManager(name);
170 }
171
172 /***
173 * This methods returns either the Manager from the configuration file,
174 * or the default one provided by the generated code.
175 *
176 * @param name name of the manager
177 * @param defaultClassName the class to use if name has not been configured
178 * @return a Manager
179 */
180 public static AbstractBaseManager getManager(String name,
181 String defaultClassName)
182 {
183 return getInstance().getManager(name, defaultClassName);
184 }
185
186 /***
187 * Shuts down the service.
188 *
189 * This method halts the IDBroker's daemon thread in all of
190 * the DatabaseMap's.
191 */
192 public static void shutdown()
193 {
194 getInstance().shutdown();
195 }
196
197 /***
198 * Returns the default database map information.
199 *
200 * @return A DatabaseMap.
201 * @throws TorqueException Any exceptions caught during processing will be
202 * rethrown wrapped into a TorqueException.
203 */
204 public static DatabaseMap getDatabaseMap()
205 throws TorqueException
206 {
207 return getInstance().getDatabaseMap();
208 }
209
210 /***
211 * Returns the database map information. Name relates to the name
212 * of the connection pool to associate with the map.
213 *
214 * @param name The name of the database corresponding to the
215 * <code>DatabaseMap</code> to retrieve.
216 * @return The named <code>DatabaseMap</code>.
217 * @throws TorqueException Any exceptions caught during processing will be
218 * rethrown wrapped into a TorqueException.
219 */
220 public static DatabaseMap getDatabaseMap(String name)
221 throws TorqueException
222 {
223 return getInstance().getDatabaseMap(name);
224 }
225
226 /***
227 * Register a MapBuilder
228 *
229 * @param className the MapBuilder
230 */
231 public static void registerMapBuilder(String className)
232 {
233 getInstance().registerMapBuilder(className);
234 }
235
236 /***
237 * This method returns a Connection from the default pool.
238 *
239 * @return The requested connection.
240 * @throws TorqueException Any exceptions caught during processing will be
241 * rethrown wrapped into a TorqueException.
242 */
243 public static Connection getConnection()
244 throws TorqueException
245 {
246 return getInstance().getConnection();
247 }
248
249 /***
250 * This method returns a Connecton using the given database name.
251 *
252 * @param name The database name.
253 * @return a database connection
254 * @throws TorqueException Any exceptions caught during processing will be
255 * rethrown wrapped into a TorqueException.
256 */
257 public static Connection getConnection(String name)
258 throws TorqueException
259 {
260 return getInstance().getConnection(name);
261 }
262
263 /***
264 * This method returns a Connecton using the given parameters.
265 * You should only use this method if you need user based access to the
266 * database!
267 *
268 * @param name The database name.
269 * @param username The name of the database user.
270 * @param password The password of the database user.
271 * @return A Connection.
272 * @throws TorqueException Any exceptions caught during processing will be
273 * rethrown wrapped into a TorqueException.
274 */
275 public static Connection getConnection(String name, String username,
276 String password)
277 throws TorqueException
278 {
279 return getInstance().getConnection(name, username, password);
280 }
281 /***
282 * Returns database adapter for a specific connection pool.
283 *
284 * @param name A pool name.
285 * @return The corresponding database adapter.
286 * @throws TorqueException Any exceptions caught during processing will be
287 * rethrown wrapped into a TorqueException.
288 */
289 public static DB getDB(String name) throws TorqueException
290 {
291 return getInstance().getDB(name);
292 }
293
294 /***
295 * Returns the name of the default database.
296 *
297 * @return name of the default DB
298 */
299 public static String getDefaultDB()
300 {
301 return getInstance().getDefaultDB();
302 }
303
304 /***
305 * Closes a connection.
306 *
307 * @param con A Connection to close.
308 */
309 public static void closeConnection(Connection con)
310 {
311 getInstance().closeConnection(con);
312 }
313
314
315
316
317
318
319
320
321
322 /***
323 * configure torque
324 *
325 * @param conf Configuration
326 * @throws TorqueException Any exceptions caught during processing will be
327 * rethrown wrapped into a TorqueException.
328 * @deprecated
329 */
330 public void configure(Configuration conf) throws TorqueException
331 {
332 this.memberConfig = conf;
333 }
334
335 /***
336 * initialize Torque
337 *
338 * @throws TorqueException Any exceptions caught during processing will be
339 * rethrown wrapped into a TorqueException.
340 * @deprecated
341 */
342 public void initialize() throws TorqueException
343 {
344 getInstance().init(memberConfig);
345 }
346
347 /***
348 * Shuts down the service, Lifecycle style
349 * @deprecated
350 */
351 public void dispose()
352 {
353 getInstance().shutdown();
354 }
355 }