package com.dy.common.mw.protocol; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; public class ProtocolCach { //本系统中,Driver在单线程中运行,所以只用一个实例 private static HashMap drivers = new HashMap() ; //注解与处理类的映射 private static HashMap driverMap = new HashMap() ; private static HashMap prefixedDataAvailableMap = new HashMap() ; private static HashMap onLineMap = new HashMap() ; /** * 在单线程环境中运行 * 通过协议驱动的类名,得到类单例 * @param protocolName * @return * @throws Exception */ public static Driver getDriver(String protocolName) throws Exception{ Driver dri = drivers.get(protocolName); if(dri == null){ AnnotationDriverVo vo = driverMap.get(protocolName) ; if(vo != null && vo.clazz != null){ dri = (Driver)vo.clazz.newInstance() ; drivers.put(protocolName, dri) ; } } return dri ; } /** * 得到驱动的数量,即中间件支持的协议数量 * @return */ public static int driverCount(){ if(drivers == null || drivers.size() == 0){ return 0 ; }else{ return drivers.size() ; } } /** * 得到第一个驱动 * @return */ public static Driver getFirstDriver() throws Exception{ Driver dri = null ; if(drivers != null && drivers.size() > 0){ Map.Entry ent = drivers.entrySet().iterator().next() ; dri = ent.getValue(); } return dri ; } /** * 得到所有协议名称 * @return */ public static List getProtocolList() { List list = new ArrayList() ; Collection col = driverMap.keySet() ; for(String pname : col){ list.add(pname) ; } return list; } protected static HashMap getDriverMap() { return driverMap; } protected static HashMap getPrefixedDataAvailableMap() { return prefixedDataAvailableMap; } protected static HashMap getOnLineMap() { return onLineMap; } }