001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025:
026: package org.nemesis.forum.util.jdbc;
027:
028: import java.sql.Connection;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.nemesis.forum.config.ConfigLoader;
033:
034: /**
035: * Central manager of database connections. All methods are static so that they
036: * can be easily accessed throughout the classes in the database package.
037: */
038: public class DbConnectionManager {
039:
040: static protected Log log = LogFactory
041: .getLog(DbConnectionManager.class);
042:
043: private static DbConnectionProvider connectionProvider;
044: private static Object providerLock = new Object();
045:
046: /**
047: * Returns a database connection from the currently active connection
048: * provider.
049: */
050: public static Connection getConnection() {
051: if (connectionProvider == null) {
052: synchronized (providerLock) {
053: if (connectionProvider == null) {
054: //Attempt to load the class.
055: try {
056:
057: connectionProvider = (DbConnectionProvider) Class
058: .forName(
059: ConfigLoader
060: .getInstance()
061: .getConfig()
062: .getJDBCConnectionProviderClass())
063: .newInstance();
064: connectionProvider.start();
065: } catch (Exception e) {
066: log
067: .fatal(
068: "check db.connectionProvider.className in nemesis-forum-config.properties:"
069: + ConfigLoader
070: .getInstance()
071: .getConfig()
072: .getJDBCConnectionProviderClass(),
073: e);
074: }
075: }
076:
077: }
078:
079: }
080: return connectionProvider.getConnection();
081: }
082:
083: /**
084: * Returns the current connection provider. The only case in which this
085: * method should be called is if more information about the current
086: * connection provider is needed. Database connections should always be
087: * obtained by calling the getConnection method of this class.
088: */
089: public static DbConnectionProvider getDbConnectionProvider() {
090: return connectionProvider;
091: }
092:
093: /**
094: * Sets the connection provider. The old provider (if it exists) is shut
095: * down before the new one is started. A connection provider <b>should
096: * not</b> be started before being passed to the connection manager
097: * because the manager will call the start() method automatically.
098: */
099: public static void setDbConnectionProvider(
100: DbConnectionProvider provider) {
101: synchronized (providerLock) {
102: if (connectionProvider != null) {
103: connectionProvider.destroy();
104: connectionProvider = null;
105: }
106: connectionProvider = provider;
107: provider.start();
108: }
109: }
110:
111: }
|