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.13 2004/01/08 05:55:07 tanderson Exp $
44 *
45 * Date Author Changes
46 * 08/29/2001 jima Created
47 */
48 package org.exolab.jms.persistence;
49
50 import java.sql.Connection;
51
52 import javax.transaction.TransactionManager;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56
57 import org.exolab.core.service.Service;
58 import org.exolab.core.service.ServiceException;
59 import org.exolab.jms.config.Configuration;
60 import org.exolab.jms.config.ConfigurationManager;
61 import org.exolab.jms.config.DatabaseConfiguration;
62 import org.exolab.jms.config.JdbmDatabaseConfiguration;
63 import org.exolab.jms.config.RdbmsDatabaseConfiguration;
64
65
66 /***
67 * The DatabaseService is used for managing the persistence aspect
68 * of this project.
69 *
70 * @version $Revision: 1.13 $ $Date: 2004/01/08 05:55:07 $
71 * @author <a href="mailto:jima@intalio.com">Jim Alateras</a>
72 */
73 public class DatabaseService extends Service {
74
75 /***
76 * The name of the service
77 */
78 private final static String DB_SERVICE_NAME = "DatabaseService";
79
80 /***
81 * Maintains a singleton instance of the database service
82 */
83 private static DatabaseService _instance = null;
84
85 /***
86 * Used to synchronize the creation of the database service
87 */
88 private final static Object _creator = new Object();
89
90 /***
91 * Return a reference to the PersistenceAdaptor, that has been created
92 * by this service
93 */
94 private PersistenceAdapter _adapter = null;
95
96 /***
97 * The logger
98 */
99 private static final Log _log = LogFactory.getLog(DatabaseService.class);
100
101
102 /***
103 * Return the singleton instance of the DatabaseService
104 *
105 * @return DatabaseService
106 * @throws PersistenceException
107 */
108 public static DatabaseService instance()
109 throws PersistenceException {
110 if (_instance == null) {
111 synchronized (_creator) {
112 // we need to check again if multiple threads
113 // have blocked on the creation of the singleton
114 if (_instance == null) {
115 _instance = new DatabaseService();
116 }
117 }
118 }
119
120 return _instance;
121 }
122
123 /***
124 * Return the {@link PersistenceAdapter} created by this service. This will
125 * always be non-null.
126 *
127 * @return PersistenceAdapter - the created adapter
128 */
129 public static PersistenceAdapter getAdapter() {
130 return _instance._adapter;
131 }
132
133 /***
134 * Convenience function to return a connection to the PersistenceAdapter
135 *
136 * @return Connection - the connection
137 * @exception PersistenceException - if no connection could be retrieved
138 * or another error occured.
139 */
140 public static Connection getConnection()
141 throws PersistenceException {
142 return getAdapter().getConnection();
143 }
144
145 /***
146 * Create an instance of a database service. It uses the configuration
147 * manager to extract the service parameters.
148 * <p>
149 * It will throw a PersistenceException, if it cannot construct the service
150 *
151 * @throws PersistenceException
152 */
153 DatabaseService()
154 throws PersistenceException {
155 super(DB_SERVICE_NAME);
156
157 Configuration config = ConfigurationManager.getConfig();
158 DatabaseConfiguration dbconfig = config.getDatabaseConfiguration();
159
160 // we have the database configuration information, now we need to
161 // create the adapter
162 if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
163 _adapter = createRdbmsAdapter(
164 dbconfig.getRdbmsDatabaseConfiguration());
165 } else {
166 _adapter = createJdbmAdapter(
167 dbconfig.getJdbmDatabaseConfiguration());
168 }
169 }
170
171 // override Service.start
172 public void start() throws ServiceException {
173 super.start();
174
175 // remove the expired messages
176 Connection connection = null;
177 try {
178 connection = getConnection();
179
180 getAdapter().removeExpiredMessages(connection);
181 _log.info("Removed expired messages.");
182 connection.commit();
183
184 } catch (PersistenceException exception) {
185 SQLHelper.rollback(connection);
186 throw exception;
187 } catch (Exception exception) {
188 // rethrow as an appropriate exception
189 throw new ServiceException("Failed to start the DatabaseService",
190 exception);
191 } finally {
192 SQLHelper.close(connection);
193 }
194 }
195
196 // override Service.stop
197 public void stop()
198 throws ServiceException {
199
200 if (_adapter != null) {
201 _adapter.close();
202 }
203
204 synchronized (_creator) {
205 _instance = null;
206 }
207
208 super.stop();
209 }
210
211 /***
212 * Create an instance of an RDBMS persistence adapter using the specified
213 * database configuration. If it cannot create the adapter then throw
214 * {@lnik PersistenceException} exception
215 *
216 * @param config database configuration
217 * @return the created adapter
218 * @throws PersistenceException
219 */
220 private PersistenceAdapter createRdbmsAdapter(
221 RdbmsDatabaseConfiguration config) throws PersistenceException {
222
223 PersistenceAdapter adapter = null;
224
225 if (config.hasBatch() && config.getBatch()) {
226 _log.info("Creating EXPERIMENTAL BatchingRdbmsAdapter for "
227 + config.getDriver());
228 adapter = new BatchingRdbmsAdapter(
229 config.getDriver(),
230 config.getUrl(),
231 config.getUser(),
232 config.getPassword(),
233 config.getBatchSize());
234 } else {
235 _log.info("Creating RdbmsAdapter for " + config.getDriver());
236 adapter = new RDBMSAdapter(
237 config.getDriver(),
238 config.getUrl(),
239 config.getUser(),
240 config.getPassword());
241 }
242
243 return adapter;
244 }
245
246 /***
247 * Create an instance of an JDBM persistence adapter using the specified
248 * database configuration. If it cannot create the adapter then throw
249 * {@lnik PersistenceException} exception
250 *
251 * @param config database configuration
252 * @return the created adapter
253 * @throws PersistenceException
254 */
255 private PersistenceAdapter createJdbmAdapter(
256 JdbmDatabaseConfiguration config) throws PersistenceException {
257
258 PersistenceAdapter adapter = new ObjectAdapter(config.getName(),
259 10000,
260 40000,
261 config.getCacheSize());
262 return adapter;
263 }
264
265 } //-- DatabaseService
266
267
This page was automatically generated by Maven