View Javadoc

1   package org.apache.torque;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache Turbine" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache Turbine", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  
57  import java.sql.Connection;
58  
59  import org.apache.commons.configuration.Configuration;
60  
61  import org.apache.stratum.lifecycle.Configurable;
62  import org.apache.stratum.lifecycle.Disposable;
63  import org.apache.stratum.lifecycle.Initializable;
64  
65  import org.apache.torque.adapter.DB;
66  import org.apache.torque.manager.AbstractBaseManager;
67  import org.apache.torque.map.DatabaseMap;
68  
69  /***
70   * A static facade wrapper around the Torque implementation (which is in
71   * {@link org.apache.torque.TorqueInstance}).
72   * <br/>
73   * For historical reasons this class also contains a thin object which can
74   * be used to configure Torque with the Stratum Lifecycle. This is deprecated
75   * and will be removed in the future in favour of using Torque as an Avalon
76   * Component.
77   *
78   * @todo This class will be made abstract once Stratum is removed.
79   *
80   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
81   * @author <a href="mailto:magnus@handtolvur.is">Magn?s ??r Torfason</a>
82   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
83   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
84   * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
85   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
86   * @author <a href="mailto:kschrader@karmalab.org">Kurt Schrader</a>
87   * @version $Id: Torque.java,v 1.91 2003/08/05 18:09:35 mpoeschl Exp $
88   */
89  public class Torque
90      implements Initializable,
91                 Configurable,
92                 Disposable
93  {
94      /***
95       * Name of property that specifies the default map builder and map.
96       */
97      public static final String DATABASE_DEFAULT = "database.default";
98  
99      /***
100      * A prefix for <code>Manager</code> properties in the configuration.
101      */
102     public static final String MANAGER_PREFIX = "managed_class.";
103 
104     /***
105      * A <code>Service</code> property determining its implementing
106      * class name .
107      */
108     public static final String MANAGER_SUFFIX = ".manager";
109 
110     /***
111      * property to determine whether caching is used.
112      */
113     public static final String CACHE_KEY = "manager.useCache";
114 
115     /***
116      * The single instance of {@link TorqueInstance} used by the
117      * static API presented by this class.
118      */
119     private static TorqueInstance torqueSingleton = null;
120 
121     /*** 
122      * This is a member variable of Torque objects created by the Stratum
123      * lifecycle
124      */
125     private Configuration memberConfig = null;
126 
127     /***
128      * C'tor for usage with the Stratum Lifecycle.
129      *
130      * @todo Should be made private or protected once Stratum is removed.
131      */
132     public Torque()
133     {
134     }
135 
136     /***
137      * Retrieves the single {@link org.apache.torque.TorqueInstance}
138      * used by this class.
139      *
140      * @return Our singleton.
141      */
142     public static TorqueInstance getInstance()
143     {
144         if (torqueSingleton == null)
145         {
146             torqueSingleton = new TorqueInstance();
147         }
148         return torqueSingleton;
149     }
150 
151     /***
152      * Initialization of Torque with a properties file.
153      *
154      * @param configFile The absolute path to the configuration file.
155      * @throws TorqueException Any exceptions caught during processing will be
156      *         rethrown wrapped into a TorqueException.
157      */
158     public static void init(String configFile)
159         throws TorqueException
160     {
161         getInstance().init(configFile);
162     }
163 
164     /***
165      * Initialization of Torque with a properties file.
166      *
167      * @param conf The Torque configuration.
168      * @throws TorqueException Any exceptions caught during processing will be
169      *         rethrown wrapped into a TorqueException.
170      */
171     public static void init(Configuration conf)
172         throws TorqueException
173     {
174         getInstance().init(conf);
175     }
176 
177     /***
178      * Determine whether Torque has already been initialized.
179      *
180      * @return true if Torque is already initialized
181      */
182     public static boolean isInit()
183     {
184         return getInstance().isInit();
185     }
186 
187     /***
188      * Sets the configuration for Torque and all dependencies.
189      *
190      * @param conf the Configuration
191      */
192     public static void setConfiguration(Configuration conf)
193     {
194         getInstance().setConfiguration(conf);
195     }
196 
197     /***
198      * Get the configuration for this component.
199      *
200      * @return the Configuration
201      */
202     public static Configuration getConfiguration()
203     {
204         return getInstance().getConfiguration();
205     }
206 
207     /***
208      * This method returns a Manager for the given name.
209      *
210      * @param name name of the manager
211      * @return a Manager
212      */
213     public static AbstractBaseManager getManager(String name)
214     {
215         return getInstance().getManager(name);
216     }
217 
218     /***
219      * This methods returns either the Manager from the configuration file,
220      * or the default one provided by the generated code.
221      *
222      * @param name name of the manager
223      * @param defaultClassName the class to use if name has not been configured
224      * @return a Manager
225      */
226     public static AbstractBaseManager getManager(String name,
227             String defaultClassName)
228     {
229         return getInstance().getManager(name, defaultClassName);
230     }
231 
232     /***
233      * Shuts down the service.
234      *
235      * This method halts the IDBroker's daemon thread in all of
236      * the DatabaseMap's.
237      */
238     public static void shutdown()
239     {
240         getInstance().shutdown();
241     }
242 
243     /***
244      * Returns the default database map information.
245      *
246      * @return A DatabaseMap.
247      * @throws TorqueException Any exceptions caught during processing will be
248      *         rethrown wrapped into a TorqueException.
249      */
250     public static DatabaseMap getDatabaseMap()
251         throws TorqueException
252     {
253         return getInstance().getDatabaseMap();
254     }
255 
256     /***
257      * Returns the database map information. Name relates to the name
258      * of the connection pool to associate with the map.
259      *
260      * @param name The name of the database corresponding to the
261      *        <code>DatabaseMap</code> to retrieve.
262      * @return The named <code>DatabaseMap</code>.
263      * @throws TorqueException Any exceptions caught during processing will be
264      *         rethrown wrapped into a TorqueException.
265      */
266     public static DatabaseMap getDatabaseMap(String name)
267         throws TorqueException
268     {
269         return getInstance().getDatabaseMap(name);
270     }
271 
272     /***
273      * Register a MapBuilder
274      *
275      * @param className the MapBuilder
276      */
277     public static void registerMapBuilder(String className)
278     {
279         getInstance().registerMapBuilder(className);
280     }
281 
282     /***
283      * This method returns a Connection from the default pool.
284      *
285      * @return The requested connection.
286      * @throws TorqueException Any exceptions caught during processing will be
287      *         rethrown wrapped into a TorqueException.
288      */
289     public static Connection getConnection()
290         throws TorqueException
291     {
292         return getInstance().getConnection();
293     }
294 
295     /***
296      * This method returns a Connecton using the given database name.
297      *
298      * @param name The database name.
299      * @return a database connection
300      * @throws TorqueException Any exceptions caught during processing will be
301      *         rethrown wrapped into a TorqueException.
302      */
303     public static Connection getConnection(String name)
304         throws TorqueException
305     {
306         return getInstance().getConnection(name);
307     }
308 
309     /***
310      * This method returns a Connecton using the given parameters.
311      * You should only use this method if you need user based access to the
312      * database!
313      *
314      * @param name The database name.
315      * @param username The name of the database user.
316      * @param password The password of the database user.
317      * @return A Connection.
318      * @throws TorqueException Any exceptions caught during processing will be
319      *         rethrown wrapped into a TorqueException.
320      */
321     public static Connection getConnection(String name, String username,
322             String password)
323             throws TorqueException
324     {
325         return getInstance().getConnection(name, username, password);
326     }
327     /***
328      * Returns database adapter for a specific connection pool.
329      *
330      * @param name A pool name.
331      * @return The corresponding database adapter.
332      * @throws TorqueException Any exceptions caught during processing will be
333      *         rethrown wrapped into a TorqueException.
334      */
335     public static DB getDB(String name) throws TorqueException
336     {
337         return getInstance().getDB(name);
338     }
339 
340     /***
341      * Returns the name of the default database.
342      *
343      * @return name of the default DB
344      */
345     public static String getDefaultDB()
346     {
347         return getInstance().getDefaultDB();
348     }
349 
350     /***
351      * Closes a connection.
352      *
353      * @param con A Connection to close.
354      */
355     public static void closeConnection(Connection con)
356     {
357         getInstance().closeConnection(con);
358     }
359 
360     /*
361      * ========================================================================
362      *
363      * Stratum Lifecycle Interface (deprecated)
364      *
365      * ========================================================================
366      */
367 
368     /***
369      * configure torque
370      *
371      * @param conf Configuration
372      * @see org.apache.stratum.lifecycle.Configurable
373      * @throws TorqueException Any exceptions caught during processing will be
374      *         rethrown wrapped into a TorqueException.
375      */
376     public void configure(Configuration conf) throws TorqueException
377     {
378         this.memberConfig = conf;
379     }
380 
381     /***
382      * initialize Torque
383      *
384      * @see org.apache.stratum.lifecycle.Initializable
385      * @throws TorqueException Any exceptions caught during processing will be
386      *         rethrown wrapped into a TorqueException.
387      */
388     public void initialize() throws TorqueException
389     {
390         getInstance().init(memberConfig);
391     }
392 
393     /***
394      * Shuts down the service, Lifecycle style
395      * @see org.apache.stratum.lifecycle.Disposable
396      */
397     public void dispose()
398     {
399         getInstance().shutdown();
400     }
401 }