KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > persistence > DatabaseService


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: DatabaseService.java,v 1.1 2004/11/26 01:50:44 tanderson Exp $
44  */

45 package org.exolab.jms.persistence;
46
47 import java.sql.Connection JavaDoc;
48
49 import javax.transaction.TransactionManager JavaDoc;
50
51 import org.apache.commons.logging.Log;
52 import org.apache.commons.logging.LogFactory;
53
54 import org.exolab.jms.config.Configuration;
55 import org.exolab.jms.config.ConfigurationManager;
56 import org.exolab.jms.config.DatabaseConfiguration;
57 import org.exolab.jms.config.RdbmsDatabaseConfiguration;
58 import org.exolab.jms.service.Service;
59 import org.exolab.jms.service.ServiceException;
60
61
62 /**
63  * The DatabaseService is used for managing the persistence aspect
64  * of this project.
65  *
66  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:50:44 $
67  * @author <a HREF="mailto:jima@intalio.com">Jim Alateras</a>
68  */

69 public class DatabaseService extends Service {
70
71     /**
72      * The name of the service
73      */

74     private final static String JavaDoc DB_SERVICE_NAME = "DatabaseService";
75
76     /**
77      * Maintains a singleton instance of the database service
78      */

79     private static DatabaseService _instance = null;
80
81     /**
82      * Used to synchronize the creation of the database service
83      */

84     private final static Object JavaDoc _creator = new Object JavaDoc();
85
86     /**
87      * Return a reference to the PersistenceAdaptor, that has been created
88      * by this service
89      */

90     private PersistenceAdapter _adapter = null;
91
92     /**
93      * The logger
94      */

95     private static final Log _log = LogFactory.getLog(DatabaseService.class);
96
97
98     /**
99      * Return the singleton instance of the DatabaseService
100      *
101      * @return DatabaseService
102      * @throws PersistenceException
103      */

104     public static DatabaseService instance()
105         throws PersistenceException {
106         if (_instance == null) {
107             synchronized (_creator) {
108                 // we need to check again if multiple threads
109
// have blocked on the creation of the singleton
110
if (_instance == null) {
111                     _instance = new DatabaseService();
112                 }
113             }
114         }
115
116         return _instance;
117     }
118
119     /**
120      * Return the {@link PersistenceAdapter} created by this service. This will
121      * always be non-null.
122      *
123      * @return PersistenceAdapter - the created adapter
124      */

125     public static PersistenceAdapter getAdapter() {
126         return _instance._adapter;
127     }
128
129     /**
130      * Convenience function to return a connection to the PersistenceAdapter
131      *
132      * @return Connection - the connection
133      * @exception PersistenceException - if no connection could be retrieved
134      * or another error occured.
135      */

136     public static Connection JavaDoc getConnection()
137         throws PersistenceException {
138         return getAdapter().getConnection();
139     }
140
141     /**
142      * Create an instance of a database service. It uses the configuration
143      * manager to extract the service parameters.
144      * <p>
145      * It will throw a PersistenceException, if it cannot construct the service
146      *
147      * @throws PersistenceException
148      */

149     DatabaseService()
150         throws PersistenceException {
151         super(DB_SERVICE_NAME);
152
153         Configuration config = ConfigurationManager.getConfig();
154         DatabaseConfiguration dbconfig = config.getDatabaseConfiguration();
155
156         _adapter = createRdbmsAdapter(
157             dbconfig.getRdbmsDatabaseConfiguration());
158     }
159
160     // override Service.start
161
public void start() throws ServiceException {
162         super.start();
163
164         // remove the expired messages
165
Connection JavaDoc connection = null;
166         try {
167             connection = getConnection();
168
169             getAdapter().removeExpiredMessages(connection);
170             _log.info("Removed expired messages.");
171             connection.commit();
172
173         } catch (PersistenceException exception) {
174             SQLHelper.rollback(connection);
175             throw exception;
176         } catch (Exception JavaDoc exception) {
177             // rethrow as an appropriate exception
178
throw new ServiceException("Failed to start the DatabaseService",
179                                        exception);
180         } finally {
181             SQLHelper.close(connection);
182         }
183     }
184
185     // override Service.stop
186
public void stop()
187         throws ServiceException {
188
189         if (_adapter != null) {
190             _adapter.close();
191         }
192
193         synchronized (_creator) {
194             _instance = null;
195         }
196
197         super.stop();
198     }
199
200     /**
201      * Create an instance of an RDBMS persistence adapter using the specified
202      * database configuration. If it cannot create the adapter then throw
203      * {@lnik PersistenceException} exception
204      *
205      * @param config database configuration
206      * @return the created adapter
207      * @throws PersistenceException
208      */

209     private PersistenceAdapter createRdbmsAdapter(
210         RdbmsDatabaseConfiguration config) throws PersistenceException {
211
212         PersistenceAdapter adapter = null;
213
214         if (config.hasBatch() && config.getBatch()) {
215             _log.info("Creating EXPERIMENTAL BatchingRdbmsAdapter for "
216                 + config.getDriver());
217             adapter = new BatchingRdbmsAdapter(
218                 config.getDriver(),
219                 config.getUrl(),
220                 config.getUser(),
221                 config.getPassword(),
222                 config.getBatchSize());
223         } else {
224             _log.info("Creating RdbmsAdapter for " + config.getDriver());
225             adapter = new RDBMSAdapter(
226                 config.getDriver(),
227                 config.getUrl(),
228                 config.getUser(),
229                 config.getPassword());
230         }
231
232         return adapter;
233     }
234
235 }
236
237
238
Popular Tags