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

📄 acmanager.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.server.helper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

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

import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy;

import com.cownew.PIS.framework.common.exception.ServiceBeanInitException;
import com.cownew.PIS.framework.common.services.ACInfo;
import com.cownew.ctk.common.ExceptionUtils;

/**
 * 帐套管理器,负责帐套信息的查询
 * @author 杨中科
 *
 */
public class ACManager
{
	private static ACManager instance = null;

	private Map acInfoMap = null;
	
	private Map dsMap;
	
	private Context ctx;

	private ACManager()
	{
		super();
		dsMap = new HashMap();
		Properties prop = new Properties();
		try
		{
			ctx = new InitialContext(prop);
		} catch (NamingException e)
		{
			ExceptionUtils.toRuntimeException(e);
		}
	}

	public static ACManager getInstance() 
	{
		if (instance == null)
		{
			instance = new ACManager();
			instance.registerACItems();
		}
		return instance;
	}

	/**
	 * 注册所有的帐套信息
	 */
	private void registerACItems()
	{
		ACInfo[] acInfos = ServerConfig.getInstance().getAcInfos();
		acInfoMap = Collections.synchronizedMap(new HashMap(acInfos.length));
		for (int i = 0, n = acInfos.length; i < n; i++)
		{
			ACInfo acInfo = acInfos[i];
			acInfoMap.put(acInfo.getName(), acInfo);
		}
	}

	/**
	 * 根据帐套名得到帐套信息
	 * @param acName
	 * @return
	 */
	public ACInfo getACInfo(String acName)
	{
		return (ACInfo) acInfoMap.get(acName);
	}

	/**
	 * 根据帐套名得到数据源
	 * @param acName
	 * @return
	 * @
	 */
	public DataSource getDataSource(String acName)
	{
		DataSource ds = null;
		ds = (DataSource) dsMap.get(acName);
		if(ds!=null)
		{
			return ds;
		}
		
		ACInfo acInfo = getACInfo(acName);
		String dsName = acInfo.getDataSourceName();		
		try
		{
			// 此处可以采用独立于平台的数据源,将数据源的配置放到ServerConfig.xml中
			ds = (DataSource) ctx.lookup(dsName);
			// 用TransactionAwareDataSourceProxy对数据源进行包装
			// 以保证受事务关联
			ds = new TransactionAwareDataSourceProxy(ds);
			dsMap.put(acName,ds);
		} catch (NamingException e)
		{
			throw new ServiceBeanInitException(
					ServiceBeanInitException.DATASOURCEERROR, e);
		}
		return ds;

	}

	/**
	 * 根据帐套名得到数据库连接
	 * @param acName
	 * @return
	 * @
	 */
	public Connection getConnection(String acName) 
	{
		try
		{
			return getDataSource(acName).getConnection();
		} catch (SQLException e)
		{
			throw new ServiceBeanInitException(
					ServiceBeanInitException.CONNECTIONERROR, e);
		}
	}

}

⌨️ 快捷键说明

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