001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.db;
029
030import java.sql.Connection;
031import java.sql.SQLException;
032import java.util.ArrayList;
033import java.util.List;
034
035/**
036 * Superclass for all SQL manager implementations.<p>
037 *
038 * @since 6.0.0
039 */
040public class CmsSqlManager {
041
042    /** the driver manager. */
043    private CmsDriverManager m_driverManager;
044
045    /**
046     * Protected constructor to allow only subclassing.<p>
047     */
048    protected CmsSqlManager() {
049        // hide public constructor
050    }
051
052    /**
053     * Creates a new SQL manager from the provided driver manager.<p>
054     *
055     * @param driverManager the low level database driver manager
056     */
057    protected CmsSqlManager(CmsDriverManager driverManager) {
058
059        m_driverManager = driverManager;
060    }
061
062    /**
063     * Returns the number of active connections managed by a pool.<p>
064     *
065     * @param dbPoolUrl the url of a pool
066     * @return the number of active connections
067     * @throws CmsDbException if something goes wrong
068     */
069    public int getActiveConnections(String dbPoolUrl) throws CmsDbException {
070
071        return m_driverManager.getActiveConnections(dbPoolUrl);
072    }
073
074    /**
075     * Returns a connection to the database using the given pool identified by its name.<p>
076     *
077     * @param dbPoolName the pool name
078     * @return a database connection
079     * @throws SQLException if something goes wrong
080     */
081    public Connection getConnection(String dbPoolName) throws SQLException {
082
083        return getConnectionByUrl(CmsDbPoolV11.OPENCMS_URL_PREFIX + dbPoolName);
084    }
085
086    /**
087     * Returns a connection to the database using the given pool identified by its full url.<p>
088     *
089     * @param dbPoolUrl the pool url
090     * @return a database connection
091     * @throws SQLException if something goes wrong
092     */
093    public Connection getConnectionByUrl(String dbPoolUrl) throws SQLException {
094
095        return CmsDriverManager.m_pools.get(dbPoolUrl).getConnection();
096    }
097
098    /**
099     * Returns a list of available database connection pool names.<p>
100     *
101     * @return a list of database connection pool names
102     */
103    public List<String> getDbPoolUrls() {
104
105        return new ArrayList<String>(CmsDriverManager.m_pools.keySet());
106
107    }
108
109    /**
110     * Returns the name of the default database connection pool.<p>
111     *
112     * @return the name of the default database connection pool
113     */
114    public String getDefaultDbPoolName() {
115
116        return CmsDbPoolV11.getDefaultDbPoolName();
117    }
118
119    /**
120     * Returns the number of idle connections managed by a pool.<p>
121     *
122     * @param dbPoolUrl the url of a pool
123     * @return the number of idle connections
124     * @throws CmsDbException if something goes wrong
125     */
126    public int getIdleConnections(String dbPoolUrl) throws CmsDbException {
127
128        return m_driverManager.getIdleConnections(dbPoolUrl);
129    }
130}