⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 databaseservice.java

📁 实现了Jms的服务器源码,支持多种适配器,DB,FTP,支持多种数据库
💻 JAVA
字号:
/**
 * Redistribution and use of this software and associated documentation
 * ("Software"), with or without modification, are permitted provided
 * that the following conditions are met:
 *
 * 1. Redistributions of source code must retain copyright
 *    statements and notices.  Redistributions must also contain a
 *    copy of this document.
 *
 * 2. Redistributions in binary form must reproduce the
 *    above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. The name "Exolab" must not be used to endorse or promote
 *    products derived from this Software without prior written
 *    permission of Exoffice Technologies.  For written permission,
 *    please contact info@exolab.org.
 *
 * 4. Products derived from this Software may not be called "Exolab"
 *    nor may "Exolab" appear in their names without prior written
 *    permission of Exoffice Technologies. Exolab is a registered
 *    trademark of Exoffice Technologies.
 *
 * 5. Due credit should be given to the Exolab Project
 *    (http://www.exolab.org/).
 *
 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
 *
 * $Id: DatabaseService.java,v 1.13 2004/01/08 05:55:07 tanderson Exp $
 *
 * Date         Author  Changes
 * 08/29/2001   jima    Created
 */
package org.exolab.jms.persistence;

import java.sql.Connection;

import javax.transaction.TransactionManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.exolab.core.service.Service;
import org.exolab.core.service.ServiceException;
import org.exolab.jms.config.Configuration;
import org.exolab.jms.config.ConfigurationManager;
import org.exolab.jms.config.DatabaseConfiguration;
import org.exolab.jms.config.JdbmDatabaseConfiguration;
import org.exolab.jms.config.RdbmsDatabaseConfiguration;


/**
 * The DatabaseService is used for managing the persistence aspect
 * of this project.
 *
 * @version     $Revision: 1.13 $ $Date: 2004/01/08 05:55:07 $
 * @author      <a href="mailto:jima@intalio.com">Jim Alateras</a>
 */
public class DatabaseService extends Service {

    /**
     * The name of the service
     */
    private final static String DB_SERVICE_NAME = "DatabaseService";

    /**
     * Maintains a singleton instance of the database service
     */
    private static DatabaseService _instance = null;

    /**
     * Used to synchronize the creation of the database service
     */
    private final static Object _creator = new Object();

    /**
     * Return a reference to the PersistenceAdaptor, that has been created
     * by this service
     */
    private PersistenceAdapter _adapter = null;

    /**
     * The logger
     */
    private static final Log _log = LogFactory.getLog(DatabaseService.class);


    /**
     * Return the singleton instance of the DatabaseService
     *
     * @return DatabaseService
     * @throws PersistenceException
     */
    public static DatabaseService instance()
        throws PersistenceException {
        if (_instance == null) {
            synchronized (_creator) {
                // we need to check again if multiple threads
                // have blocked on the creation of the singleton
                if (_instance == null) {
                    _instance = new DatabaseService();
                }
            }
        }

        return _instance;
    }

    /**
     * Return the {@link PersistenceAdapter} created by this service. This will
     * always be non-null.
     *
     * @return PersistenceAdapter - the created adapter
     */
    public static PersistenceAdapter getAdapter() {
        return _instance._adapter;
    }

    /**
     * Convenience function to return a connection to the PersistenceAdapter
     *
     * @return Connection - the connection
     * @exception PersistenceException - if no connection could be retrieved
     *                                   or another error occured.
     */
    public static Connection getConnection()
        throws PersistenceException {
        return getAdapter().getConnection();
    }

    /**
     * Create an instance of a database service. It uses the configuration
     * manager to extract the service parameters.
     * <p>
     * It will throw a PersistenceException, if it cannot construct the service
     *
     * @throws PersistenceException
     */
    DatabaseService()
        throws PersistenceException {
        super(DB_SERVICE_NAME);

        Configuration config = ConfigurationManager.getConfig();
        DatabaseConfiguration dbconfig = config.getDatabaseConfiguration();

        // we have the database configuration information, now we need to
        // create the adapter
        if (dbconfig.getRdbmsDatabaseConfiguration() != null) {
            _adapter = createRdbmsAdapter(
                dbconfig.getRdbmsDatabaseConfiguration());
        } else {
            _adapter = createJdbmAdapter(
                dbconfig.getJdbmDatabaseConfiguration());
        }
    }

    // override Service.start
    public void start() throws ServiceException {
        super.start();

        // remove the expired messages
        Connection connection = null;
        try {
            connection = getConnection();

            getAdapter().removeExpiredMessages(connection);
            _log.info("Removed expired messages.");
            connection.commit();

        } catch (PersistenceException exception) {
            SQLHelper.rollback(connection);
            throw exception;
        } catch (Exception exception) {
            // rethrow as an appropriate exception
            throw new ServiceException("Failed to start the DatabaseService",
                                       exception);
        } finally {
            SQLHelper.close(connection);
        }
    }

    // override Service.stop
    public void stop()
        throws ServiceException {

        if (_adapter != null) {
            _adapter.close();
        }

        synchronized (_creator) {
            _instance = null;
        }

        super.stop();
    }

    /**
     * Create an instance of an RDBMS persistence adapter using the specified
     * database configuration. If it cannot create the adapter then throw
     * {@lnik PersistenceException} exception
     *
     * @param config database configuration
     * @return the created adapter
     * @throws PersistenceException
     */
    private PersistenceAdapter createRdbmsAdapter(
        RdbmsDatabaseConfiguration config) throws PersistenceException {

        PersistenceAdapter adapter = null;

        if (config.hasBatch() && config.getBatch()) {
            _log.info("Creating EXPERIMENTAL BatchingRdbmsAdapter for "
                + config.getDriver());
            adapter = new BatchingRdbmsAdapter(
                config.getDriver(),
                config.getUrl(),
                config.getUser(),
                config.getPassword(),
                config.getBatchSize());
        } else {
            _log.info("Creating RdbmsAdapter for " + config.getDriver());
            adapter = new RDBMSAdapter(
                config.getDriver(),
                config.getUrl(),
                config.getUser(),
                config.getPassword());
        }

        return adapter;
    }

    /**
     * Create an instance of an JDBM persistence adapter using the specified
     * database configuration. If it cannot create the adapter then throw
     * {@lnik PersistenceException} exception
     *
     * @param config database configuration
     * @return the created adapter
     * @throws PersistenceException
     */
    private PersistenceAdapter createJdbmAdapter(
        JdbmDatabaseConfiguration config) throws PersistenceException {

        PersistenceAdapter adapter = new ObjectAdapter(config.getName(),
            10000,
            40000,
            config.getCacheSize());
        return adapter;
    }

} //-- DatabaseService


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -