// Path: octopus-meta/src/main/java/kr/co/bitnine/octopus/meta/MetaContext.java
// public interface MetaContext {
//     /*
//      * User
//      */
// 
//     boolean userExists(String name) throws MetaException;
// 
//     MetaUser getUser(String name) throws MetaException;
// 
//     MetaUser createUser(String name, String password) throws MetaException;
// 
//     void alterUser(String name, String newPassword) throws MetaException;
// 
//     void dropUser(String name) throws MetaException;
// 
//     void commentOnUser(String comment, String name) throws MetaException;
// 
//     Collection<MetaUser> getUsers() throws MetaException;
// 
//     /*
//      * DataSource
//      */
// 
//     boolean dataSourceExists(String name) throws MetaException;
// 
//     MetaDataSource getDataSource(String name) throws MetaException;
// 
//     MetaDataSource addJdbcDataSource(String driverName, String connectionString, String name) throws MetaException;
// 
//     MetaDataSource addMetaModelDataSource(String driverName, String connectionString, String name) throws MetaException;
// 
//     void dropJdbcDataSource(String name) throws MetaException;
// 
//     MetaDataSource updateJdbcDataSource(String dataSourceName, final String schemaRegex, final String tableRegex) throws MetaException;
// 
//     void commentOnDataSource(String comment, String name) throws MetaException;
// 
//     Collection<MetaDataSource> getDataSources() throws MetaException;
// 
//     Collection<ResultOfGetColumns> getColumns(String datasourceName, final String schemaRegex, final String tableRegex, final String columnRegex) throws MetaException;
//     /*
//      * Schema
//      */
// 
//     void commentOnSchema(String comment, String dataSourceName, String schemaName) throws MetaException;
// 
//     /*
//      * Table
//      */
// 
//     void commentOnTable(String comment, String dataSourceName, String schemaName, String tableName) throws MetaException;
// 
//     /*
//      * Column
//      */
// 
//     void commentOnColumn(String comment, String dataSourceName, String schemaName, String tableName, String columnName) throws MetaException;
// 
//     void setDataCategoryOn(String category, String dataSource, String schema, String table, String column) throws MetaException;
// 
//     /*
//      * Role
//      */
// 
//     MetaRole createRole(String name) throws MetaException;
// 
//     void dropRoleByName(String name) throws MetaException;
// 
//     /*
//      * Privilege
//      */
// 
//     void addSystemPrivileges(List<SystemPrivilege> sysPrivs, List<String> userNames) throws MetaException;
// 
//     void removeSystemPrivileges(List<SystemPrivilege> sysPrivs, List<String> userNames) throws MetaException;
// 
//     MetaSchemaPrivilege getSchemaPrivilege(String[] schemaName, String userName) throws MetaException;
// 
//     Collection<MetaSchemaPrivilege> getSchemaPrivilegesByUser(String userName) throws MetaException;
// 
//     void addObjectPrivileges(List<ObjectPrivilege> objPrivs, String[] schemaName, List<String> userNames) throws MetaException;
// 
//     void removeObjectPrivileges(List<ObjectPrivilege> objPrivs, String[] schemaName, List<String> userNames) throws MetaException;
// 
//     void close();
// }
// 
// Path: octopus-meta/src/main/java/kr/co/bitnine/octopus/meta/model/MetaDataSource.java
// public interface MetaDataSource {
//     enum DataSourceType {
//         JDBC,
//         CATALOG,
//         METAMODEL
//     }
// 
//     String getName();
// 
//     String getDriverName();
// 
//     String getConnectionString();
// 
//     DataSourceType getDataSourceType();
// 
//     String getComment();
// 
//     Collection<MetaSchema> getSchemas();
// }


import kr.co.bitnine.octopus.conf.OctopusConfiguration;
import kr.co.bitnine.octopus.meta.MetaContext;
import kr.co.bitnine.octopus.meta.MetaStore;
import kr.co.bitnine.octopus.meta.model.MetaDataSource;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDriver;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.service.AbstractService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/*
 * Licensed 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 kr.co.bitnine.octopus.frame;



public final class ConnectionManager extends AbstractService {
    private static final Log LOG = LogFactory.getLog(ConnectionManager.class);
    private static final String DRIVER_PREFIX = "jdbc:apache:commons:dbcp:";

    private final MetaStore metaStore;

    private PoolingDriver poolingDriver;

    public ConnectionManager(MetaStore metaStore) {
        super(ConnectionManager.class.getName());

        this.metaStore = metaStore;
    }

    @Override
    protected void serviceInit(Configuration conf) throws Exception {
        LOG.info("initialize service - " + getName());

        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        Class.forName("kr.co.bitnine.octopus.engine.calcite.Driver"); // FIXME:
        poolingDriver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX);

        super.serviceInit(conf);
    }

    @Override
    protected void serviceStart() throws Exception {
        LOG.info("start service - " + getName());

        MetaContext mc = metaStore.getMetaContext();
// Path: octopus-meta/src/main/java/kr/co/bitnine/octopus/meta/MetaContext.java
// public interface MetaContext {
//     /*
//      * User
//      */
// 
//     boolean userExists(String name) throws MetaException;
// 
//     MetaUser getUser(String name) throws MetaException;
// 
//     MetaUser createUser(String name, String password) throws MetaException;
// 
//     void alterUser(String name, String newPassword) throws MetaException;
// 
//     void dropUser(String name) throws MetaException;
// 
//     void commentOnUser(String comment, String name) throws MetaException;
// 
//     Collection<MetaUser> getUsers() throws MetaException;
// 
//     /*
//      * DataSource
//      */
// 
//     boolean dataSourceExists(String name) throws MetaException;
// 
//     MetaDataSource getDataSource(String name) throws MetaException;
// 
//     MetaDataSource addJdbcDataSource(String driverName, String connectionString, String name) throws MetaException;
// 
//     MetaDataSource addMetaModelDataSource(String driverName, String connectionString, String name) throws MetaException;
// 
//     void dropJdbcDataSource(String name) throws MetaException;
// 
//     MetaDataSource updateJdbcDataSource(String dataSourceName, final String schemaRegex, final String tableRegex) throws MetaException;
// 
//     void commentOnDataSource(String comment, String name) throws MetaException;
// 
//     Collection<MetaDataSource> getDataSources() throws MetaException;
// 
//     Collection<ResultOfGetColumns> getColumns(String datasourceName, final String schemaRegex, final String tableRegex, final String columnRegex) throws MetaException;
//     /*
//      * Schema
//      */
// 
//     void commentOnSchema(String comment, String dataSourceName, String schemaName) throws MetaException;
// 
//     /*
//      * Table
//      */
// 
//     void commentOnTable(String comment, String dataSourceName, String schemaName, String tableName) throws MetaException;
// 
//     /*
//      * Column
//      */
// 
//     void commentOnColumn(String comment, String dataSourceName, String schemaName, String tableName, String columnName) throws MetaException;
// 
//     void setDataCategoryOn(String category, String dataSource, String schema, String table, String column) throws MetaException;
// 
//     /*
//      * Role
//      */
// 
//     MetaRole createRole(String name) throws MetaException;
// 
//     void dropRoleByName(String name) throws MetaException;
// 
//     /*
//      * Privilege
//      */
// 
//     void addSystemPrivileges(List<SystemPrivilege> sysPrivs, List<String> userNames) throws MetaException;
// 
//     void removeSystemPrivileges(List<SystemPrivilege> sysPrivs, List<String> userNames) throws MetaException;
// 
//     MetaSchemaPrivilege getSchemaPrivilege(String[] schemaName, String userName) throws MetaException;
// 
//     Collection<MetaSchemaPrivilege> getSchemaPrivilegesByUser(String userName) throws MetaException;
// 
//     void addObjectPrivileges(List<ObjectPrivilege> objPrivs, String[] schemaName, List<String> userNames) throws MetaException;
// 
//     void removeObjectPrivileges(List<ObjectPrivilege> objPrivs, String[] schemaName, List<String> userNames) throws MetaException;
// 
//     void close();
// }
// 
// Path: octopus-meta/src/main/java/kr/co/bitnine/octopus/meta/model/MetaDataSource.java
// public interface MetaDataSource {
//     enum DataSourceType {
//         JDBC,
//         CATALOG,
//         METAMODEL
//     }
// 
//     String getName();
// 
//     String getDriverName();
// 
//     String getConnectionString();
// 
//     DataSourceType getDataSourceType();
// 
//     String getComment();
// 
//     Collection<MetaSchema> getSchemas();
// }


// Path: octopus-core/src/main/java/kr/co/bitnine/octopus/frame/ConnectionManager.java
import kr.co.bitnine.octopus.conf.OctopusConfiguration;
import kr.co.bitnine.octopus.meta.MetaContext;
import kr.co.bitnine.octopus.meta.MetaStore;
import kr.co.bitnine.octopus.meta.model.MetaDataSource;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDriver;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.service.AbstractService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/*
 * Licensed 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 kr.co.bitnine.octopus.frame;



public final class ConnectionManager extends AbstractService {
    private static final Log LOG = LogFactory.getLog(ConnectionManager.class);
    private static final String DRIVER_PREFIX = "jdbc:apache:commons:dbcp:";

    private final MetaStore metaStore;

    private PoolingDriver poolingDriver;

    public ConnectionManager(MetaStore metaStore) {
        super(ConnectionManager.class.getName());

        this.metaStore = metaStore;
    }

    @Override
    protected void serviceInit(Configuration conf) throws Exception {
        LOG.info("initialize service - " + getName());

        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        Class.forName("kr.co.bitnine.octopus.engine.calcite.Driver"); // FIXME:
        poolingDriver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX);

        super.serviceInit(conf);
    }

    @Override
    protected void serviceStart() throws Exception {
        LOG.info("start service - " + getName());

        MetaContext mc = metaStore.getMetaContext();
        for (MetaDataSource ds : mc.getDataSources()) {