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());
}
}