org.azkfw.persistence.database.DatabaseSource.java Source code

Java tutorial

Introduction

Here is the source code for org.azkfw.persistence.database.DatabaseSource.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.azkfw.persistence.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.StackObjectPool;
import org.azkfw.persistence.database.entity.DatabaseConnectionEntity;

/**
 * ???????
 * 
 * @since 1.0.0
 * @version 1.0.0 12/06/09
 * @author Kawakicchi
 * 
 */
public final class DatabaseSource {

    public class SimpleConnectionFactory extends BasePoolableObjectFactory<Connection> {
        /** URI */
        private String url;
        /**  */
        private String user;
        /**  */
        private String password;

        /**
         * ???????
         * 
         * @param aUrl ????URL
         * @param aUser ??????
         * @param aPassword ????
         */
        public SimpleConnectionFactory(final String aUrl, final String aUser, final String aPassword) {
            url = aUrl;
            user = aUser;
            password = aPassword;
        }

        /**
         * java.sql.Connection????
         * 
         * @return ???
         */
        public Connection makeObject() throws Exception {
            return DriverManager.getConnection(url, user, password);
        }
    }

    /**
     * Connection entity
     */
    private DatabaseConnectionEntity entity;

    private ObjectPool<Connection> pool;

    /**
     * 
     */
    public DatabaseSource() {
        ;
    }

    /**
     * ???
     * 
     * @param driver ???
     * @param uri 
     * @param user ??
     * @param password 
     * @throws ClassNotFoundException ???????
     */
    public void load(final String driver, final String uri, final String user, final String password)
            throws ClassNotFoundException {
        DatabaseConnectionEntity e = new DatabaseConnectionEntity();
        e.setDriver(driver);
        e.setUri(uri);
        e.setUser(user);
        e.setPassword(password);
        load(e);
    }

    /**
     * ???
     * 
     * @param p 
     * @throws ClassNotFoundException ???????
     */
    public void load(final Properties p) throws ClassNotFoundException {
        DatabaseConnectionEntity e = new DatabaseConnectionEntity();
        e.setDriver(p.getProperty("database.dirver"));
        e.setUri(p.getProperty("database.uri"));
        e.setUser(p.getProperty("database.user"));
        e.setPassword(p.getProperty("database.password"));
        load(e);
    }

    /**
     * ???
     * 
     * @param aEntity 
     * @throws ClassNotFoundException ???????
     */
    private void load(final DatabaseConnectionEntity aEntity) throws ClassNotFoundException {
        entity = aEntity;
        pooling();
    }

    /**
     * ???
     * 
     * @throws ClassNotFoundException ???????
     */
    private void pooling() throws ClassNotFoundException {
        Class.forName(entity.getDriver());

        PoolableObjectFactory<Connection> factory = new SimpleConnectionFactory(entity.getUri(), entity.getUser(),
                entity.getPassword());
        pool = new StackObjectPool<Connection>(factory);
    }

    /**
     * ?????
     * 
     * @return ?
     * @throws SQLException SQL????
     */
    public DatabaseConnection getConnection() throws SQLException {
        return getConnection(true);
    }

    /**
     * ?????
     * 
     * @param aPool 
     * @return ?
     * @throws SQLException SQL????
     */
    public DatabaseConnection getConnection(final boolean aPool) throws SQLException {
        Connection con = null;
        try {
            if (aPool) {
                con = pool.borrowObject();
            } else {
                con = DriverManager.getConnection(entity.getUri(), entity.getUser(), entity.getPassword());
            }
        } catch (Exception ex) {
            throw new SQLException(ex);
        }
        return new DatabaseConnection(con);
    }

    /**
     * ?????
     * 
     * @param connection ?
     * @throws SQLException SQL????
     */
    public void returnConnection(final DatabaseConnection connection) throws SQLException {
        returnConnection(connection, true);
    }

    /**
     * ?????
     * 
     * @param connection ?
     * @param pool 
     * @throws SQLException SQL????
     */
    public void returnConnection(final DatabaseConnection connection, final boolean aPool) throws SQLException {
        Connection con = connection.getConnection();
        try {
            if (null != con) {
                if (aPool) {
                    pool.returnObject(con);
                } else {
                    con.close();
                }
            }
        } catch (Exception ex) {
            throw new SQLException(ex);
        }
    }
}