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

📄 accountmanagerimpl.java

📁 基于corba的资源池,实现了实例池供客户端调用,并且实例池对实例的分配采用策略模式支持随即抽取,最近使用和循环抽取三个不同策略.(在visibroke下面开发的)
💻 JAVA
字号:
//账户管理员对象的具体实现
package interfaceImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import pool.*;
import interfaceImpl.AccountImpl;
import org.omg.PortableServer.*; 
import database.DatabaseAccess;

public class AccountManagerImpl extends Bank.AccountManagerPOA 
{
    //属性定义
	//该账户管理员所负责的账户清单
    protected Map accountList;        
    private DatabaseAccess dbAccess;
    String DatabaseSource ="jdbc:odbc:BankingDataSource";
    private ObjectPooling pool = null;
    private POA rootPOA;
	
    private static AccountManagerImpl uniqueInstance;
    
	private AccountManagerImpl(boolean usePool,String policies,POA rootPOA)
	{
		accountList = new Hashtable();
		dbAccess = new DatabaseAccess(DatabaseSource);
		this.rootPOA = rootPOA;
		if (usePool) {
			// 采用资源池
			// 策略集
			SelectorStrategy policie = StrategyFactory.create(policies); 
			// 初始化资源池
			this.pool = new ObjectPooling(policie);
		}		
	}
	
	/**
	 * 单根类
	 */
	public static AccountManagerImpl getInstance(boolean usePool,String policies,POA rootPOA)
	{
		if(uniqueInstance == null){
			uniqueInstance = new AccountManagerImpl(usePool,policies,rootPOA);
		}
		return uniqueInstance;
	}
	
	/**
	 * 根据帐户id得到该帐户
	 * @param id 帐户id
	 * @return 返回该帐户接口
	 */
	public synchronized Bank.Account getAccountById(String id)
	{
		Bank.Account account = null;
		AccountImpl accountServant = null;
		//没用实例池
		if(null == pool){
			accountServant = (AccountImpl)getFromAccountList(id);
			String sql = "SELECT * FROM Accounts WHERE id = '"+ id + "'";
			if (accountServant == null){
				accountServant = getAccount(sql);
				if (accountServant != null) addToAccountList(accountServant);
			}
			try{
				org.omg.CORBA.Object obj = _default_POA().servant_to_reference((Servant)accountServant);
				account = Bank.AccountHelper.narrow(obj);
			}catch (NullPointerException e){
				System.out.println("刚用户输入的ID号不存在,正在重新输入...\n");
			}catch(StringIndexOutOfBoundsException exc){
				System.out.println("刚用户输入的ID号不存在,正在重新输入...\n");
			}catch (Exception e){
				e.printStackTrace();
			}
			return account;
		}else{       //采用实例池
			accountServant = (AccountImpl)pool.checkOut(id);
			try{
				org.omg.CORBA.Object obj = _default_POA().servant_to_reference((Servant)accountServant);
				account = Bank.AccountHelper.narrow(obj);
				account.setId(id);
				account.setName(id.substring(5));
			}catch (NullPointerException e){
				System.out.println("刚用户输入的ID号不存在,正在重新输入...\n");
				if (null != pool) {
					pool.checkIn(accountServant);
				}
				// 释放引用
				accountServant = null;
			}catch(StringIndexOutOfBoundsException exc){
				System.out.println("刚用户输入的ID号不存在,正在重新输入...\n");
				if (null != pool) {
					pool.checkIn(accountServant);
				}
				// 释放引用
				accountServant = null;
			}catch (Exception e){
				e.printStackTrace();
			}
			return account;
		}
	}
	
	/**
	 * 一次访问结束后,将资源放回资源池
	 */
	public void releaseObject(Bank.Account acc) 
	{
		// TODO Auto-generated method stub
		try{
			if (null != pool) {
				AccountImpl ai = (AccountImpl)rootPOA.reference_to_servant(acc);
				pool.checkIn(ai);
			}	
		}catch(Exception e){
			System.out.println(e.getClass());
			e.printStackTrace();
		}
		// 释放引用
		acc = null;			
	}
	
	/**
	 * 与数据库建立连接,根据查询语句,查询数据库,返回结果集
	 * 并从结果集中把数据取出来
	 * @param sql sql语句
	 * @return  返回从结果集中取出来的持久帐户类
	 */
	private AccountImpl getAccount(String sql)
	{
		AccountImpl accountServant = null;
		try{
			ResultSet rs = dbAccess.query(sql);    			
			while (rs.next())
				accountServant = new AccountImpl(rs.getString(1),rs.getString(2),rs.getFloat(3));
			rs.close();
		}catch(SQLException e){
			e.printStackTrace();
			System.out.println("SQL出错");
		}catch(Exception e){
			e.printStackTrace();
		}
		return accountServant;
	}
	
	/**
	 * 与数据库建立连接,查询数据库中参数指定的id号的帐户是否存在
	 * @param id  帐户id
	 * @return   存在返回真,不存在返回假
	 */
	public boolean isIdExist(String id)
	{
		String sql = "SELECT * FROM Accounts WHERE _id = "+ id;
		try{
			ResultSet rs = dbAccess.query(sql);
			if(rs.next())return true;
			else return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

    // 查找指定名字的账户,找不到则以该名字新开一个账户
    public synchronized Bank.Account open(String id) {
        // 在账户清单中查找指定名字的账户
        Bank.Account account = (Bank.Account) accountList.get(id);
        // 如果不存在则新创建一个
        if (account == null) {
            // 随机虚构账户的初始余额,金额在0至1000之间
            Random random = new Random();
            float balance = Math.abs(random.nextInt()) % 100000 / 100f;
            // 按指定余额创建账户的伺服对象
            AccountImpl accountServant = new AccountImpl(id,id.substring(5),balance);
            try {
                // 用默认的POA激活伺服对象,这里默认的POA就是根POA
                org.omg.CORBA.Object obj = _default_POA().servant_to_reference((Servant)accountServant);
                // 将对象引用收窄为账户类型
                account = Bank.AccountHelper.narrow(obj);
            } catch(Exception exc) {
                exc.printStackTrace();
            }
            // 将账户保存到账户清单中
            accountList.put(id, account);
            // 在服务端控制台打印已创建新账户的提示信息
            System.out.println("新开账户:" + id);
        }
        // 返回找到的账户或新开设的账户
        return account;
    }
    
    //注销一个帐户
    public synchronized void close(String id)
    {
    	
    }

	private void addToAccountList(AccountImpl acc)
	{
		accountList.put(acc.getId(), acc);
	}
	
	private AccountImpl getFromAccountList(String id)
	{
		return (AccountImpl)accountList.get(id);
	}
}

⌨️ 快捷键说明

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