⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datasourceproviderimpl.java

📁 OpenReports是一个完整的基于Web的报表方案
💻 JAVA
字号:
/*
 * Copyright (C) 2002 Erik Swenson - eswenson@opensourcesoft.net
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

package org.efs.openreports.providers.impl;

import java.sql.Connection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

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

import org.apache.log4j.Logger;
import org.efs.openreports.objects.ReportDataSource;
import org.efs.openreports.providers.DataSourceProvider;
import org.efs.openreports.providers.ProviderException;
import org.efs.openreports.providers.persistence.DataSourcePersistenceProvider;

public class DataSourceProviderImpl implements DataSourceProvider
{
	protected static Logger log =
		Logger.getLogger(DataSourceProviderImpl.class.getName());

	private Hashtable dataSources = new Hashtable();
	
	private DataSourcePersistenceProvider dataSourcePersistenceProvider;
	
	public DataSourceProviderImpl() throws ProviderException
	{

		dataSourcePersistenceProvider = new DataSourcePersistenceProvider();

		loadDataSources();

		log.info("DataSourceProviderImpl");
	}

	protected void loadDataSources() throws ProviderException
	{
		Iterator iterator =
			dataSourcePersistenceProvider.getDataSources().iterator();

		while (iterator.hasNext())
		{
			ReportDataSource dataSource = (ReportDataSource) iterator.next();
			dataSources.put(dataSource.getId(), dataSource);
		}
	}

	public Connection getConnection(Integer id) throws ProviderException
	{
		ReportDataSource dataSource = (ReportDataSource) dataSources.get(id);

		try
		{
			return dataSource.getConnection();
		}
		catch (Exception e)
		{
			log.error(e.toString());
			throw new ProviderException(e.getMessage());
		}
	}

	public boolean isValidDataSource(Integer id)
	{
		if (dataSources.containsKey(id))
			return true;

		return false;
	}

	public List getDataSources() throws ProviderException
	{
		return dataSourcePersistenceProvider.getDataSources();
	}

	public ReportDataSource getDataSource(Integer id) throws ProviderException
	{
		return dataSourcePersistenceProvider.getDataSource(id);
	}

	public ReportDataSource insertDataSource(ReportDataSource dataSource)
		throws ProviderException
	{
		testDataSource(dataSource);

		dataSource = dataSourcePersistenceProvider.insertDataSource(dataSource);
		dataSources.put(dataSource.getId(), dataSource);

		return dataSource;
	}

	public void updateDataSource(ReportDataSource dataSource)
		throws ProviderException
	{
		testDataSource(dataSource);

		dataSourcePersistenceProvider.updateDataSource(dataSource);
		dataSources.put(dataSource.getId(), dataSource);
	}

	public void deleteDataSource(ReportDataSource dataSource)
		throws ProviderException
	{
		dataSourcePersistenceProvider.deleteDataSource(dataSource);
		dataSources.remove(dataSource.getId());
	}

	public void testDataSource(ReportDataSource dataSource)
		throws ProviderException
	{
		Connection conn = null;

		try
		{
			if (dataSource.isJndi())
			{
				Context initCtx = new InitialContext();

				DataSource jndiDataSource =
					(DataSource) initCtx.lookup(dataSource.getUrl());

				conn = jndiDataSource.getConnection();
			}
			else
			{
				conn = dataSource.getConnection();
			}
		}
		catch (Exception e)
		{
			throw new ProviderException(
				"Error testing connection, verify DataSource configuration and try again: "
					+ e.getMessage());
		}
		finally
		{
			try
			{
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{
				log.error(e.toString());
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -