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

📄 isppmofactory.java

📁 一个SNMP4J开发的agent端程序
💻 JAVA
字号:
package com.poson.nmi.common;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Vector;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.poson.common.DAOException;
import com.poson.common.DAOManager;
import com.poson.common.LogManager;
import com.poson.common.XResultSet;
import com.poson.common.ReturnMsg;
import com.poson.npi.common.EqptBean;
import com.poson.npi.common.NPIResponse;

/**
 * 取值枚举类型
 *
 */
class DealType
{
	public final static int DEFAULT = 1 ;   // 默认值
	public final static int DYNSQL = 2 ;    // 动态SQL
	public final static int UNIXSHELL = 3 ; // UNIXshell脚本
	public final static int SPECIALFUNC = 4 ; // 特殊函数
}
/**
 * 标量类型枚举
 *
 */
class VarType
{
	public final static int SCARLAR = 1 ;
	public final static int TABLE = 2 ;
}

public class ISPPMOFactory {
	
	protected static Logger logger = LogManager.getLogger(ISPPMOFactory.class) ;
	
	protected static HashMap<String,MOBean> MOMap ;
	
	protected static ISPPMOFactory instance ;
	
	
	private ISPPMOFactory() {
		loadMO() ; 
	}
	
	public static ISPPMOFactory getInstance()
	{
		if(instance == null)instance = new ISPPMOFactory() ;
		return instance ;
	}
	/*
	 * 获取OID总数
	 */
	public int getOIDNum()
	{
		return MOMap.size() ;
	}
	
	/**
	 * 获取值数组
	 * @return
	 */
	public String[] getKeyOidArry()
	{
		return (String[])MOMap.keySet().toArray();
	}
	
	/**
	 * 
	 * @param portEntry
	 * @return
	 */
	public int getALRowNumber(String portEntry)
	{
		MOBean moBean ;
		int retVal = 0 ;
		Iterator iter = MOMap.values().iterator();
		while(iter.hasNext())
		{
			moBean = (MOBean)iter.next() ;
			if(moBean.getObjName().trim().endsWith(portEntry)) 
			{
				retVal = moBean.getRowNum() ;
				break ;
			}
				
		}
		if(retVal == 0)
			logger.info("** 不存在 "+portEntry+" 对应的记录....... ") ;
		
		return retVal ;
	}
	/**
	 * 从数据库中一次性 load 所有被管对象
	 *
	 */
	public void loadMO(){
		
		try {
			
			StringBuffer strBufSql = new StringBuffer();
			Vector vecCond = new Vector() ;
			DAOManager daoMgr ;
			XResultSet xrtSet ;
			
			strBufSql.append("select OID,OBJ_NAME, OBJ_TYPE,DEAL_TYPE,VAR_TYPE,DEFAULT_VALUE,DYN_SQL,FUN_NAME,SHELL_NAME,ROW_NUM" +
					" from TB_IM_MIB_CONF where STATE = ? order by oid ");
			
			 vecCond.add("S0A");

			 daoMgr = new DAOManager();
			 
			ReturnMsg rtnMsg = daoMgr.DAOQuery(strBufSql.toString(), vecCond);
			if (rtnMsg.getReturnValue() < 0) {
				throw new DAOException(" 查询被管对象失败!:");
			}

			xrtSet = new XResultSet(rtnMsg.getXMLString());
			if (xrtSet.getRowCount() <= 0) {
				throw new Exception(" 没有被管对象记录!");
			}
			
			MOMap = new HashMap<String,MOBean>() ;
			
			for (int i = 1; i <= xrtSet.getRowCount(); i++) {
				MOBean moBean = new MOBean();
				moBean.setOid(xrtSet.getValue(i, "OID")) ;
				moBean.setObjName(xrtSet.getValue(i, "OBJ_NAME")) ;
				moBean.setObjType(xrtSet.getValue(i, "OBJ_TYPE")) ;
				moBean.setDealType(new Integer(xrtSet.getValue(i, "DEAL_TYPE"))) ;
				moBean.setVarType(new Integer(xrtSet.getValue(i, "VAR_TYPE"))) ;
				moBean.setDefaultValue(xrtSet.getValue(i, "DEFAULT_VALUE")) ;
				moBean.setDynSql(xrtSet.getValue(i, "DYN_SQL")) ;
				moBean.setFunName(xrtSet.getValue(i, "FUN_NAME")) ;
				moBean.setShellName(xrtSet.getValue(i, "SHELL_NAME")) ;
				moBean.setRowNum(new Integer(xrtSet.getValue(i, "ROW_NUM")));
				
				MOMap.put(moBean.getOid(), moBean) ;
			}
			
			daoMgr.DAOClose();
			
		} catch (DAOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
	
	public String getValue(String oid)
	{
		String retStr = "Unkown Managed Object..." ;
		
		if(!MOMap.containsKey(oid)) 
			return retStr ;
		
		MOBean moBean = MOMap.get(oid) ;
		
		switch(moBean.dealType)
		{
			case DealType.DEFAULT :
				retStr = moBean.getDefaultValue() ;
				break ;
			case DealType.DYNSQL :
				retStr = getValueByDynSQL(moBean.dynSql) ;
				break ;
			case DealType.UNIXSHELL :
				retStr = getValueByUNIXShell(moBean.shellName) ;
				break ;
			case DealType.SPECIALFUNC :
				retStr = getValueByFUNC(moBean.funName) ;
				break ;
			
			default:
				break ;
		
		}
		return retStr ;
	}
	
	
	public String getDefaultValue(String oid)
	{
		String retVal = "-1" ;
		if(!MOMap.containsKey(oid)) 
		{
			logger.error("未知OID: "+oid+" ,请检查配置...");
			return retVal ;
		}
			
		MOBean moBean = MOMap.get(oid) ;
		
		if(moBean.dealType == DealType.DEFAULT  && moBean.getVarType() == VarType.SCARLAR)
		{
			retVal = moBean.getDefaultValue();
		}
		
		return retVal ;
	}
	
	/**
	 * 
	 * @param oid
	 * @return
	 */
	public int getValueByOID(String oid)
	{
		int retVal = -1 ;
		if(!MOMap.containsKey(oid)) 
		{
			logger.error("未知OID: "+oid+" ,请检查配置...");
			return retVal ;
		}
			
		MOBean moBean = MOMap.get(oid) ;
		
		if(moBean.dealType == DealType.DYNSQL  && moBean.getVarType() == VarType.SCARLAR)
		{
			try {
				
				DAOManager daoMgr ;
				XResultSet xrtSet ;
				daoMgr = new DAOManager();
				 
				ReturnMsg rtnMsg = daoMgr.DAOQuery(moBean.getDynSql(), null);
				if (rtnMsg.getReturnValue() < 0) {
					throw new DAOException(" 执行动态SQL,数据库报错!:");
				}
				xrtSet = new XResultSet(rtnMsg.getXMLString());
				
				retVal  = new Integer(xrtSet.getValue(1, "RESULT")) ;
				
				daoMgr.DAOClose();
				
			} catch (DAOException e) {

				logger.error(e.getMessage());
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.getMessage());
			}
			
		}
		
		return retVal ;
	}
	/**
	 * 通过动态SQL取值
	 * @param strSql
	 * @return
	 */
	private String getValueByDynSQL(String strSql)
	{
		String strVal ="Error occur while getting get object value...";
		try {
			
			DAOManager daoMgr ;
			XResultSet xrtSet ;
			daoMgr = new DAOManager();
			 
			ReturnMsg rtnMsg = daoMgr.DAOQuery(strSql, null);
			if (rtnMsg.getReturnValue() < 0) {
				throw new DAOException(" 执行动态SQL,数据库报错!:");
			}
			xrtSet = new XResultSet(rtnMsg.getXMLString());
			
			strVal = xrtSet.getValue(1, "RESULT") ;
			
			daoMgr.DAOClose();
			
		} catch (DAOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace() ;
			logger.error(e.getMessage());
		}
		
		return strVal ;
	}
	
	/**
	 * 根据Shell脚本取值
	 * @param shellName
	 * @return
	 */
	
	private String getValueByUNIXShell(String shellName)
	{
		//		 to be continue ;
		return "to be implimented !  " ;
	}
	
	/**
	 * 根据特殊函数取值
	 * @param shellName
	 * @return
	 */
	private String getValueByFUNC(String funcName)
	{
		String clzName = funcName.substring(0, funcName.lastIndexOf(".")) ;
		String methodName = funcName.substring(funcName.lastIndexOf(".")+1,funcName.length()) ;
		
		String retValue = "Error occur while getting get object value..."; 
		Class clz ;
		try
		{
			clz =  Class.forName(clzName) ;
			IValueFunc valObj = (IValueFunc)clz.newInstance() ;
			
			Method   methInst = clz.getMethod(methodName, new Class[]{});   
			retValue = (String) methInst.invoke(valObj,  new Class[]{}) ;
			
		}catch (ClassNotFoundException e) {
			logger.info("类名:" + clzName+ " 不存在!" ) ;
			logger.error(e.getMessage()) ;
			e.printStackTrace();
		} catch (SecurityException e) {
			logger.info("非法访问方法名:" + clzName ) ;
			logger.error(e.getMessage()) ;
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			logger.info("方法名:" + methodName+ " 不存在!" ) ;
			logger.error(e.getMessage()) ;
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retValue ;
	}
	
	
	
	
	/**
	 * 测试函数
	 * @param agrs
	 */
	public static void main(String[] agrs)
	{
		ISPPMOFactory moLoader = new ISPPMOFactory() ;
		moLoader.loadMO() ;
		String clzName = "com.poson.mni.TESTCLAA.createMO" ;
		
		System.out.println(moLoader.getValue("1.3.6.1.4.1.20942.1.1.2.2.1.1"))  ;
	}
}


⌨️ 快捷键说明

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