jp.co.golorp.emarf.sql.DataSources.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.golorp.emarf.sql.DataSources.java

Source

/*
 * Apache License, Version 2.0???????
 * ?????????????
 *
 * ??http://www.apache.org/licenses/LICENSE-2.0?????
 *
 * ???????????????
 * ????????
 * ??????????????????????
 *
 * ????????????????????????
 */
package jp.co.golorp.emarf.sql;

import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.golorp.emarf.exception.SystemError;

/**
 * ??
 *
 * @author oukuf@golorp
 */
public final class DataSources {

    /*
     * ************************************************************ 
     */

    /** Logger */
    private static final Logger LOG = LoggerFactory.getLogger(DataSources.class);

    /** DataSource.properties */
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(DataSources.class.getSimpleName());

    /** dataSourceName */
    private static final String DATA_SOURCE_NAME = "dataSourceName";

    // /***/
    // private static boolean isMySQL = false;

    // /***/
    // private static boolean isOracle = false;

    /*
     * ************************************************************ 
     */

    /**  */
    private static DataSource ds = null;

    /*
     * ************************************************************ 
     */

    /**
     * 
     */
    private DataSources() {
    }

    /*
     * ************************************************************ 
     */

    /**
     * ?
     *
     * @return DataSource
     */
    public static DataSource get() {

        if (ds != null) {
            return ds;
        }

        /*
         * JNDI??
         */

        String name = BUNDLE.getString(DATA_SOURCE_NAME);
        try {
            Context context = new InitialContext();
            ds = (DataSource) context.lookup(name);
            return ds;
        } catch (NamingException e) {
            LOG.warn(e.getMessage());
        }

        /*
         * DBCP??
         */

        Properties properties = new Properties();
        Enumeration<String> keys = BUNDLE.getKeys();
        while (keys.hasMoreElements()) {
            String key = keys.nextElement();
            String value = BUNDLE.getString(key);
            properties.put(key, value);
            // if (value.contains("mysql")) {
            // DataSources.isMySQL = true;
            // } else if (value.contains("oracle")) {
            // DataSources.isOracle = true;
            // }
        }

        try {
            ds = BasicDataSourceFactory.createDataSource(properties);
            return ds;
        } catch (Exception e) {
            throw new SystemError(e);
        }
    }

    // /**
    // * @return boolean
    // */
    // public static boolean isMySQL() {
    // return DataSources.isMySQL;
    // }

    // /**
    // * @return boolean
    // */
    // public static boolean isOracle() {
    // return DataSources.isOracle;
    // }

}