test

package com.metlife.ins.product.metguard.util;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;

import com.metlife.ins.product.metguard.framework.cache.Cache;
import com.metlife.ins.product.metguard.framework.cache.CacheManager;

/**
 * Title: Description: when application server initial,set data to cache.
 * Copyright: Copyright (c) 2011
 *
 * @author: Cognizant
 * @version 1.0
 */
public class MetGuardTestUtil {
 
 /**
  * set properties data data to cache
  *
  * @param
  * @return
  * @throws
  * @return void
  */
  public static void setPropertiesDataToCache() {
   Properties prop = null;
   InputStream is = null;
   try {
     is = MetGuardTestUtil.class.getResourceAsStream("metguard_test_config.properties");
     prop = new Properties();
     prop.load(is);
    for (Enumeration e = prop.propertyNames(); e.hasMoreElements();) {
     String key = (String) e.nextElement();
     String value = (String) prop.getProperty(key);
     Cache cache = new Cache();
     cache.setKey(key);
     cache.setValue(value);
     CacheManager.putCache(key, cache);
    }
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(1);
  }
 }
}

 

 

 

 

 

 

/**
  * get connection
  *
  * @param dbName
  * @return Connection
  */
 public static Connection getConnection(String dbName) {
  Connection connection;
  try {
   if(dbName.equals(Constants.INFORMXIDB)){
    Class.forName(INFORMIXDRIVER);
    connection = DriverManager.getConnection(INFORMIXURL, INFORMIXUSER, INFORMIXPWD);
//    connection.setAutoCommit(false);
   }else{
    Class.forName(SQLSERVERDRIVER);
    connection = DriverManager.getConnection(SQLSERVERURL, SQLSERVERUSER, SQLSERVERPWD);
//    connection.setAutoCommit(false);
   }
  }catch(SQLException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,"Get Connection Using Jdbc Error!", ex);
   throw new MetguardException("Get Connection Using Jdbc Error!");
  }catch(ClassNotFoundException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,"Get Connection Using Jdbc Error!", ex);
   throw new MetguardException("Get Connection Using Jdbc Error!");
  }
  return connection;
 }

 

 

 


 private static Map<String, DataSource> dataSourceMap = Collections
   .synchronizedMap(new HashMap<String, DataSource>());
 
 /**
  * Get data source, which is configurated in Application Server
  * 
  * @return DataSource
  * @exception DAOException
  */
 private static DataSource getDataSource() {
  String dataSourceLookupName = CacheManager.getCacheInfo(
    Constants.SQLSERVER_DS_JNDI_NAME).getValue().toString();
  DataSource ds = (DataSource) dataSourceMap.get(dataSourceLookupName);
  if (null != ds) {
   return ds;
  }
  Context ctx = null;
  try {
   ctx = new InitialContext();
   if (ctx == null) {
    throw new MetguardException("Initialize SQL Server Context error.");
   }
   ds = (DataSource) ctx.lookup(dataSourceLookupName);
   dataSourceMap.put(dataSourceLookupName, ds);
  } catch (NamingException e) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server DataSource Error: NamingException");
  } catch (NullPointerException e) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server DataSource Error: NullPointerException");
  }
  return ds;
 }
 
 /**
  * Set Connection Transaction
  * 
  * @return
  * @exception DAOException
  */
 public static void startTransaction(Connection conn) throws DAOException {
  try {
   conn.setAutoCommit(false);
  } catch (SQLException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server Transaction Error: SQLException");
   throw new DAOException(ex.getMessage());
  } catch (NullPointerException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server Transaction Error: NullPointerException");
   throw new DAOException(ex.getMessage());
  }
 }
 
 /**
  * Get SQL Server database connection from threadlocal
  * 
  * @return Connection
  * @exception DAOException
  */
 public static Connection getConnection() throws DAOException {
  try {
   if (threadlocal.get() == null || ((Connection) threadlocal.get()).isClosed()) {
    DataSource ds = (DataSource) getDataSource();
    conn = ds.getConnection();
    threadlocal.set(conn);
   }
   return (Connection) threadlocal.get();
  } catch (SQLException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server Connection Error: SQLException");
   throw new DAOException(ex.getMessage());
  } catch (NullPointerException ex) {
   IBW_LOG.error(LOG_CONTEXT, EVENTS.ADHOC,
     "", "Get SQL Server Connection Error: NullPointerException");
   throw new DAOException(ex.getMessage());
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值