/*
 * Copyright 2007-2017 the original author or authors.
 *
 * 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 net.ymate.platform.persistence.jdbc.impl;

import net.ymate.platform.persistence.jdbc.AbstractDataSourceAdapter;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;

/**
 * 基于JNDI的数据源适配器
 *
 * @author 刘镇 (suninformation@163.com) on 2013年12月19日 下午2:43:51
 * @version 1.0
 */
public class JNDIDataSourceAdapter extends AbstractDataSourceAdapter {

    private DataSource __dataSource;

    @Override
    protected void __doInit() throws Exception {
        Context _initContext = new InitialContext();
        Context _envContext = (Context) _initContext.lookup("java:/comp/env");
        // 从JNDI获取数据库源
        __dataSource = (DataSource) _envContext.lookup(__cfgMeta.getConnectionUrl());
    }

    @Override
    public Connection getConnection() throws Exception {
        return __dataSource.getConnection();
    }
}
