?? mibparserapi.java
字號:
package yww.snmpif;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;
import net.percederberg.mibble.Mib;
import net.percederberg.mibble.MibLoader;
import net.percederberg.mibble.MibLoaderException;
import net.percederberg.mibble.MibSymbol;
import net.percederberg.mibble.MibValue;
import net.percederberg.mibble.MibValueSymbol;
import net.percederberg.mibble.value.ObjectIdentifierValue;
import yww.util.Log;
/**
* 用戶加載指定的MIB文件,從中根據name獲得oid
* <br>1.文件加載
* <br>2.name和oid的轉換
* @author Administrator
*
*/
public class MibParserAPI {
static MibLoader loader = new MibLoader();;
static Hashtable mibs = new Hashtable();
/**
* 單個載入MIB庫
* @param path - the path of mib file
*/
public static void load(String path)
{
try{
File file = new File(path);
Log.info("loading "+file);
loader.addDir(file.getParentFile());
Mib m = loader.load(file);
Log.info("loaded "+m);
if(m!=null) mibs.put(m.getName(),m);
else
Log.error("can not load "+path);
}catch(IOException e)
{
Log.error("io error",e);
}catch(MibLoaderException e){
Log.error("load error",e);
}
}
/**
* 載入多個MIB庫
* @param path - the String array to hold mib file paths
*/
public static void load(String[] path)
{
for(int i=0;i<path.length;i++)
{
load(path[i]);
}
}
/**
* 根據mib中定義的oid名稱獲取OID值
* @param name - the name defined in mib
* @return the oid correspond to name
*/
public static String getOIDByName(String name)
{
Enumeration enu = mibs.keys();
while(enu.hasMoreElements())
{
String mibName = (String)enu.nextElement();
String oid = getOIDByName(mibName,name);
if(oid!=null) return oid;
}
return null;
}
/**
* 在指定mib庫文件時獲取與oid名稱對應的OID值
* @param mibName - mib file name,exclude path,just a file name
* @param name - the name defined in mib
* @return the oid correspond to name in specified mib file
*/
public static String getOIDByName(String mibName,String name)
{
Mib mib = (Mib)mibs.get(mibName);
MibSymbol symbol = mib.getSymbol(name);
if(symbol instanceof MibValueSymbol)
{
MibValue value = ((MibValueSymbol)symbol).getValue();
if(value instanceof ObjectIdentifierValue){
ObjectIdentifierValue oid = (ObjectIdentifierValue)value;
return oid.toString();
}else return null;
}else return null;
}
/**
* 根據OID值獲得相應的oid名稱
* @param OID - oid value,
* @return oid name correspond to oid value
*/
public static String getNameByOID(String OID)
{
Enumeration enu = mibs.keys();
while(enu.hasMoreElements())
{
String mibName = (String)enu.nextElement();
String name = getNameByOID(mibName,OID);
if(name!=null) return name;
}
return null;
}
/**
* 在指定的MIB文件中獲取OID對應的名稱
* @param mibName - mib file name,exclude path,just a file name
* @param OID - oid vlaue
* @return the oid name correspond to oid value in specified mib file
*/
public static String getNameByOID(String mibName,String OID)
{
Mib mib = (Mib)mibs.get(mibName);
MibSymbol symbol = mib.getSymbolByOid(OID);
if(symbol!=null)
{
String name = symbol.getName();
return name;
}else
return null;
}
public static void main(String[] args)
{
MibParserAPI.load("D:\\eclipse-SDK-3.1-win32\\eclipse\\workspace\\mibble\\mibs\\ietf\\RFC1213-MIB");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -