com.cyclopsgroup.levistone.datasource.DbcpDataSource.java Source code

Java tutorial

Introduction

Here is the source code for com.cyclopsgroup.levistone.datasource.DbcpDataSource.java

Source

/* ==========================================================================
 * Copyright 2002-2004 Cyclops Group Community
 * 
 * Licensed under the Open Software License, Version 2.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://opensource.org/licenses/osl-2.1.php
 *
 *  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 com.cyclopsgroup.levistone.datasource;

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

import javax.sql.DataSource;

import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbcp.BasicDataSource;

/**
 * Dbcp provided data source
 * 
 * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo </a>
 */
public class DbcpDataSource extends DelegateDataSourceProxy implements Configurable, Initializable {

    private Properties dbcpProperties = new Properties();

    private DataSource nestedDataSource;

    /**
     * Override or implement method of parent class or interface
     *
     * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
     */
    public void configure(Configuration conf) throws ConfigurationException {
        Configuration[] props = conf.getChildren("property");
        for (int i = 0; i < props.length; i++) {
            Configuration prop = props[i];
            String name = prop.getAttribute("name");
            String value = prop.getValue();
            dbcpProperties.setProperty(name, value);
        }
    }

    /**
     * Override or implement method of parent class or interface
     *
     * @see com.cyclopsgroup.levistone.datasource.DelegateDataSourceProxy#getNestedDataSource()
     */
    protected DataSource getNestedDataSource() throws SQLException {
        return nestedDataSource;
    }

    /**
     * Override or implement method of parent class or interface
     *
     * @see org.apache.avalon.framework.activity.Initializable#initialize()
     */
    public void initialize() throws Exception {
        BasicDataSource bds = new BasicDataSource();
        BeanUtils.populate(bds, dbcpProperties);
        nestedDataSource = bds;
    }
}