📄 tempfile.tmp
字号:
/* * Copyright(C) 2008, NTT AT Co., Ltd. * Project: AWGStar * * Notes: * N/A * * Record of change: * Date Version Name Content * 2008/12/20 1.0 <TuanNA> First create */package jp.co.ntt.awgview.server.dao;import java.io.Serializable;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import jp.co.ntt.awgview.server.common.LogWriter;import jp.co.ntt.awgview.server.common.Setting;import jp.co.ntt.awgview.server.constant.Constants;import jp.co.ntt.awgview.server.constant.DBConstants;import jp.co.ntt.awgview.server.dao.ConnectionPool;import jp.co.ntt.awgview.server.dao.ConnectionPoolFactory;import jp.co.ntt.awgview.server.dao.DBConnection;import jp.co.ntt.awgview.server.vo.BlockVO;import jp.co.ntt.awgview.server.vo.ConnectorVO;import jp.co.ntt.awgview.server.vo.NodeHelper;import jp.co.ntt.awgview.server.vo.NodeVO;import jp.co.ntt.awgview.server.vo.OperationVO;import jp.co.ntt.awgview.server.vo.PackageTypeVO;import jp.co.ntt.awgview.server.vo.PackageVO;import jp.co.ntt.awgview.server.vo.PortVO;import jp.co.ntt.awgview.server.vo.TrapVO;import jp.co.ntt.awgview.server.vo.UserVO;import jp.co.ntt.awgview.server.vo.ConnectorVO.PPConnection;import jp.co.ntt.awgview.server.vo.ConnectorVO.PackageConnection;/** * Class name : DBFunctionsManager <BR> * Description: This class provide methods necessary in order to communicate with the database. * Also, manage users and distribute resources<BR> * Copyright: Copyright (c) 2008 Company: NTT-AT * * @author : AI&T * @version : 1.0 */public class DBFunctionsManager implements Serializable { private static final long serialVersionUID = -4622298009028780481L; //Declare public variables for all users // private static ConnectionPool conPool = null; private static Hashtable<String,Integer> htblQueueNameRes = new Hashtable<String,Integer>() ; private static boolean isTimerRunning = false; private static boolean isLoadedAlarmType = false; private static Hashtable<String,UserVO> hashUserOnline = new Hashtable<String,UserVO>() ; private static int userTimeout = -1; public static long lastTime = 0; private static Vector<UserVO> vtUserOnline =null; //Define private variables for each user private Hashtable<Object,Object> htblPutField = null; private DBConnection conn = null; /*One object connection received from a connection pool*/ private Vector<NodeVO> g_vtNodes = null; private ArrayList<BlockVO> g_lstBlocks = null; private ArrayList<PackageVO> g_lstPackages = null; private ArrayList<PortVO> g_lstPorts = null; private String userIP = ""; /*The IP address of client (corresponding for each user)*/ private String trapTableView = null; /* view table for load trap*/ private String logUserTableView = null; /* view table for load user log*/ /*Queue name resource distributed for each user*/ private String requestQueueName = null; /*Used for sending the data*/ private String responseQueueName = null; /*Used for receiving data*/ //Declare variables used for loading trap history and user log private long numOfObject = 0; private int offsetRecordToGet = 0; private int limitNumRecordToGet = 0; /** * Constructor */ public DBFunctionsManager(){ try{ htblPutField = new Hashtable<Object,Object>() ; htblPutField.clear(); }catch(Exception e){ System.out.println("Error occurred while initializing DBFunctionsManager object."); e.printStackTrace(); } } /** * Read all parameters necessary to start server, then create one connection pool. * @return true if complete success. */ private boolean init() { /*Read configuration file*/ Setting.readConfig(); long start = System.currentTimeMillis(); try { //Create a connection pool that used for overall connections from client to the database. conPool = ConnectionPoolFactory.getConnectionPool(); conPool.initInstance(Setting.getDBName(), Setting.getDBHost(), Setting.getDBPort(), Setting.getDBUserName(), Setting .getDBUserPass(), Setting.getDBMinConnect(), Setting.getDBWaitBusy()); LogWriter.getInstance(LogWriter.DB_LOG).info("conPool.initInstance: OK"); return true; } catch (Exception e) { LogWriter.getInstance(LogWriter.DB_LOG).error(e.toString()); } LogWriter.getInstance(LogWriter.DB_LOG).info("Time:" + (System.currentTimeMillis() - start)); return false; } /** * Receive a connection to the database * @return JResult object. * Check method isSucceeded() of this is true for success, otherwise false. * @throws Exception */ public JResult open() throws Exception{ JResult result = new JResult(); try{ //The connection pool must be created with only instance if (conPool == null){ if (!init()){ result.returnCode(JResult.ERROR_PARAM_NOT_FOUND); result.setMessage("Initializing connection pool --> Failed!"); return result; } } //Get a available connection if (conPool.isConnected()){ conn = conPool.getConnection(); if (conn == null){ result.setMessage("Cannot connect to the database!."); result.returnCode(JResult.DBOPR_ERROR_RECV_CONN_POOL); return result; } } //Get time prior in minute to check user timed out. userTimeout = Setting.getKeepAliveTimeOut(); if (userTimeout < 0){ userTimeout = Constants.USER_TIMEOUT_DEFAULT; } /*Create one thread to check user timed out. This is needed to release some resources *used by user in the case such as network break down. */ runThread(); result.returnCode(JResult.DBOPR_SUCCESS); return result; } catch (SQLException e){ LogWriter.getInstance(LogWriter.DB_LOG).error(e.getMessage()); result.setMessage(e.getMessage()); } catch (Exception e){ LogWriter.getInstance(LogWriter.DB_LOG).error(e.getMessage()); result.setMessage(e.getMessage()); } result.returnCode(JResult.DBOPR_ERROR_CONNECT_DB_FAILED); return result; } /** * Free the current connection * @return true if success. Otherwise return false. */ public boolean close(){ try{ //free the current connection from connection pool. if (conn !=null){ conPool.free(conn.getConnection()); conn = null; } return true; } catch (Exception e){ return false; } } /** * Get timeout value * @return time unit in second */ public static int getUserTimeout(){ return (userTimeout <= 0?Constants.USER_TIMEOUT_DEFAULT:userTimeout); } /**Create one thread to check user timed out. This is needed to release some resources * used by user in the case such as network break down. */ public static void runThread(){ if (!isTimerRunning){ java.util.Timer timer = new java.util.Timer(true); Timer userTimer = new Timer(); timer.schedule(userTimer,0, getUserTimeout()*1000); isTimerRunning = true; LogWriter.getInstance(LogWriter.DB_LOG).info("Started Timer to check user timed out. Prior " + userTimeout*60000 + "(ms)"); } } public String getInfoConnections(){ return "";//"> Connection address current: "+((conn!=null)?conn.toString():" null")+ " > //"All of info: "+ ConnectionPool.getInfo() ; } /** * Set the IP address of current user. This value is assigned when EJB client connect to server * @param IP address in string such as "192.168.0.1" */ public void setUserIP(String userIP){ this.userIP = userIP; } /** * Get the IP address of current user * @param userIP */ public String getUserIP(){ return this.userIP; } /** * Check alarm type is loaded or not. * @return true if alarm type was loaded, otherwise false. */ public static boolean isLoadedAlarmType (){ return isLoadedAlarmType ; } public static void setLoadedAlarmType(boolean b){ isLoadedAlarmType = b; } public String getTableName(int obj){ switch (obj){ case DBConstants.NODE_OBJECT: return NodeVO.NODE_TBL; case DBConstants.FUNCBLOCK_OBJECT: return BlockVO.BLOCK_TBL; case DBConstants.PACKAGE_OBJECT: return PackageVO.PACKAGE_TBL; case DBConstants.PORT_OBJECT: return PortVO.PORT_TBL; case DBConstants.USER_OBJECT: return UserVO.USER_TBL; case DBConstants.TRAP_OBJECT: return TrapVO.TRAP_TBL; case DBConstants.LOG_OBJECT: return OperationVO.OPERATION_TBL; case DBConstants.PACKAGE_PACKAGE_OBJECT: return PackageConnection.PACKAGE_CONNECT_TBL; case DBConstants.PACKAGE_PORT_OBJECT: return PPConnection.CONNECT_TBL; case DBConstants.PACKAGETYPE_OBJECT: return PackageTypeVO.PACKAGETYPE_TBL; default: return ""; } } /** * Get ID field name in database table * @param obj * Type of object. * @return ID field name */ private String getIDName(int obj){ switch (obj){ case DBConstants.NODE_OBJECT: return NodeVO.NODE_ID; case DBConstants.FUNCBLOCK_OBJECT: return BlockVO.BLOCK_ID; case DBConstants.PACKAGE_OBJECT: return PackageVO.PACKAGE_ID; case DBConstants.PORT_OBJECT: return PortVO.PORT_ID; case DBConstants.USER_OBJECT: return UserVO.USER_ID; case DBConstants.TRAP_OBJECT: return TrapVO.TRAP_ID; case DBConstants.LOG_OBJECT: return OperationVO.OPERATION_ID; case DBConstants.PACKAGE_PACKAGE_OBJECT: return PackageConnection.PACKAGE_CONNECT_ID; case DBConstants.PACKAGE_PORT_OBJECT: return PPConnection.CONNECT_ID; case DBConstants.PACKAGETYPE_OBJECT: return PackageTypeVO.PACKAGETYPE_ID; default: return ""; } } /** * Put key <-> field name and value <-> field data into hash table * @param field * Column name of table object in database * @param value * Value of column */ private void putfval(Object field ,Object value){ if (htblPutField != null){ htblPutField.put(field,value); } } /** * Clear all fields contained in hash table */ private void clearFieldStorer(){ htblPutField.clear(); } /** * Generate a insert SQL statement that depend on input field * @param tableName: table name in database * @return */ private String genInsertCmd(String tableName){ String szInsSQL = null; try{ if ((htblPutField != null) && (!htblPutField.isEmpty())){ String szFields = ""; String szValues = ""; Enumeration<Object> enumV = htblPutField.elements(); Enumeration<Object> enumK = htblPutField.keys(); while (enumK.hasMoreElements()){ szFields += enumK.nextElement(); szValues += enumV.nextElement(); if(!enumK.hasMoreElements()){ break; } else{ szFields += ","; szValues += "','"; } } szInsSQL = "INSERT INTO " + tableName + "(" + szFields + ") VALUES('"+ szValues + "')"; if (htblPutField != null){ htblPutField.clear(); } return szInsSQL; } } catch (Exception e){ e.printStackTrace(); return null; } return szInsSQL; } /** * Generate a update SQL statement that depend on input field * @param tableName: table name in database * @return */ private String genUpdateCmd(String tableName, String szCondition){ String szUpdateSQL = null; try{ if ((htblPutField != null)&&(!htblPutField.isEmpty())){ String szFieldVal = ""; Enumeration<Object> enumV = htblPutField.elements(); Enumeration<Object> enumK = htblPutField.keys(); while (enumK.hasMoreElements()){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -