modispatcher.java
来自「采用JAVA开发」· Java 代码 · 共 381 行
JAVA
381 行
package com.gctech.sms.platform;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
import com.gctech.sms.core.CommandStatus;
import com.gctech.sms.core.IsAbstractMOHandler;
import com.gctech.sms.dao.MOBusinessDao;
import com.gctech.sms.dao.MOCommandDao;
import com.gctech.sms.msg.MOInfo;
import com.gctech.sms.util.MyClassLoader;
import com.gctech.sms.vo.MOBusinessVO;
import com.gctech.sms.vo.MessageDao;
import com.gctech.sms.vo.MessageVO;
import com.gctech.sms.vo.MoCommandObject;
/**
* <p>Title: 上行分拣类</p>
* <p>Description: 上行分拣类,主要负责把上行信息分拣到业务处理类。</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: gctech</p>
* @author 王红宝
* @version $Id: MODispatcher.java,v 1.9 2004/09/07 01:44:01 lijz Exp $
*/
public class MODispatcher {
public static void main(String[] args) {
}
//缓冲区的长度
// private final int cachLength = 10;
// private String[][] cachCommand = new String[cachLength][3];
// private int cachNowLength = 0;
// private int cachContentLength = 0;
private Map allMessage = null;
private Collection allCmd = null;
/** 初始化的业务类对象。
*
* */
private Map moHandlers = new Hashtable();
/**
* 得到所有上行指令。
* */
public Collection getAllCommand() {
return allCmd;
}
/**
* 分拣指令,并执行指令。
* */
public void dispatch(MOInfo moi) {
/**匹配指令的标记*/
boolean bFind = false;
//去掉服务代码
System.out.println("desNUM:"+moi.destTermId);
//Refactor by liyi 2004-09-01 从短信中分拣长代码
String strCode = splitCode(moi);
//Refactor by liyi 2004-09-01 从短信中分拣业务指令
String strKey = splitKey(moi);
MoCommandObject ok = null;
for (Iterator it = allCmd.iterator(); it.hasNext();) {
MoCommandObject c = (MoCommandObject) it.next();
System.out.println("cGateway:"+c.getGateway()+"cCode:"+c.getLongCode()+"cKey:"+c.getKeyWord());
System.out.println("Gateway:"+moi.gateway+"Code:"+strCode+"Key:"+strKey);
if (c.match(moi.gateway, strCode, strKey)) {
if (ok == null)
ok = c;
else {
if (ok.compareTo(c) <= 0)
ok = c;
}
}
}
if (ok != null) {
//对上行短信进行处理
handle(moi, ok.getClassName());
} else {
logger.debug("not find the mocommand(Code:" + strCode + "Key:" + strKey + "Mobile:" + moi.srcPhoneNo);
}
}
// /**
// * 根据分拣规则对指令进行排序
// *
// * @param list长代码满足的集合
// * @param codeLength 特服号长度
// * @param keyLength 关键字长度
// * @return 排序好的MAP
// */
// private Map sortList(List list, int codeLength, int keyLength) {
// System.out.println("codeLength:" + codeLength + " keyLength:" + keyLength);
// Map map = new HashMap();
// List temp1 = new ArrayList();
// List temp2 = new ArrayList();
// List temp3 = new ArrayList();
// List temp4 = new ArrayList();
// List temp5 = new ArrayList();
// List temp6 = new ArrayList();
// String strCmdCode = "";
// String strCmdKey = "";
// for (Iterator ite = list.iterator(); ite.hasNext();) {
// MoCommandObject mocmd = (MoCommandObject) ite.next();
// if (mocmd.getLongCode() == null)
// strCmdCode = "";
// else
// strCmdCode = mocmd.getLongCode();
// if (mocmd.getKeyWord() == null)
// strCmdKey = "";
// else
// strCmdKey = mocmd.getKeyWord();
// System.out.println("strCmdKey.length():" + strCmdKey.length());
// System.out.println(strCmdKey.substring(strCmdKey.length() - 1));
// if ((strCmdKey.length() >= keyLength))
// System.out.println("strCmdKey:" + strCmdKey + ":");
// //主特服号精确匹配
// if (codeLength == 0
// && strCmdCode.length() == 0
// && (strCmdKey.length() == keyLength)
// && !strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp1.add(mocmd);
// //主特服号模糊匹配
// else if (
// codeLength == 0
// && strCmdCode.length() == 0
// && (strCmdKey.length() <= keyLength)
// && strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp2.add(mocmd);
// //精确+精确匹配
// else if (
// (codeLength != 0 && strCmdCode.length() == codeLength)
// && !strCmdCode.substring(strCmdCode.length() - 1).equals("*")
// && (strCmdKey.length() == keyLength)
// && !strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp3.add(mocmd);
// //精确+模糊匹配
// else if (
// (codeLength != 0 && strCmdCode.length() == codeLength)
// && !strCmdCode.substring(strCmdCode.length() - 1).equals("*")
// && (strCmdKey.length() <= keyLength)
// && strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp4.add(mocmd);
// //模糊+精确匹配
// else if (
// (codeLength != 0 && strCmdCode.length() <= codeLength)
// && strCmdCode.substring(strCmdCode.length() - 1).equals("*")
// && (strCmdKey.length() == keyLength)
// && !strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp5.add(mocmd);
// //模糊+模糊匹配
// else if (
// (codeLength != 0 && strCmdCode.length() <= codeLength)
// && strCmdCode.substring(strCmdCode.length() - 1).equals("*")
// && (strCmdKey.length() <= keyLength)
// && strCmdKey.substring(strCmdKey.length() - 1).equals("*"))
// temp6.add(mocmd);
// }
// map.put("1", temp1);
// map.put("2", temp2);
// map.put("3", temp3);
// map.put("4", temp4);
// map.put("5", temp5);
// map.put("6", temp6);
// System.out.println("temp1:" + temp1.size());
// System.out.println("temp2:" + temp2.size());
// System.out.println("temp3:" + temp3.size());
// System.out.println("temp4:" + temp4.size());
// System.out.println("temp5:" + temp5.size());
// System.out.println("temp6:" + temp6.size());
//
// return map;
// }
// /**
// * 比较长代码是否满足
// *
// * @param srcKey 手机上行长代码
// * @param cmdKey 平台注册的长代码
// * @return 查到了返回真
// */
// private boolean compareCode(String srcCode, String cmdCode) {
// if (cmdCode.length() > srcCode.length())
// return false;
// else if ((srcCode.length() == cmdCode.length()) && srcCode.equals(cmdCode))
// return true;
// else if (
// cmdCode.length() > 1
// && cmdCode.substring(cmdCode.length() - 1).equals("*")
// && srcCode.trim().indexOf(cmdCode.substring(0, cmdCode.length() - 1).trim()) == 0)
// return true;
// else if (cmdCode.length() == 1 && cmdCode.substring(cmdCode.length() - 1).equals("*"))
// return true;
// else
// return false;
// }
// /**
// * 比较关键字是否满足
// *
// * @param srcKey 手机上行关键字
// * @param cmdKey 平台注册的关键字
// * @return 查到了返回真
// */
// private boolean compareKey(String srcKey, String cmdKey) {
// if (cmdKey.length() > srcKey.length())
// return false;
// else if ((srcKey.length() == cmdKey.length()) && srcKey.equals(cmdKey))
// return true;
// else if (
// cmdKey.length() > 1
// && cmdKey.substring(cmdKey.length() - 1).equals("*")
// && srcKey.trim().indexOf(cmdKey.substring(0, cmdKey.length() - 1).trim()) == 0)
// return true;
// else if (cmdKey.length() == 1 && cmdKey.substring(cmdKey.length() - 1).equals("*"))
// return true;
// else
// return false;
// }
/**
* 根据类名初始化处理类对上行短信进行处理
*
* @param moi 上行短信息
* @param strClassName 处理类类名
*/
private void handle(MOInfo moi, String strClassName) {
try {
/**判断在类列表中是否有该类的名字*/
logger.debug("The class name is " + strClassName);
if (this.moHandlers.containsKey(strClassName)) {
MOHandler objMOHandler = (MOHandler) moHandlers.get(strClassName);
objMOHandler.handle(moi);
} else {
/**动态加入类*/
MyClassLoader loader = new MyClassLoader();
MOHandler objMOHandler = (MOHandler) loader.loadFromCustomRepository(strClassName.trim()).newInstance();
objMOHandler.handle(moi);
}
//logMO( moi, strClassName ) ;
} catch (Throwable e) {
logger.error(e, e);
}
}
/**
* 从上行短信中分拣出长代码
*
* @param moi 上行短信息
* @return 长代码
*/
private String splitCode(MOInfo moi) {
String strCode;
strCode = moi.destTermId.trim();
System.out.println("strCode:" + strCode+":");
System.out.println("Gateway:" + moi.getGateway()+":");
logger.info("moi.serviceNo:"+moi.serviceNo);
if (strCode.equals(moi.serviceNo)) {
strCode = "";
} else if (strCode.equals(""))
strCode = "";
else {
strCode = strCode.substring(moi.serviceNo.length());
}
return strCode.trim();
}
/**
* 从上行短信内容中分拣出业务指令
*
* @param moi上行短信息
* @return 业务指令
*/
private String splitKey(MOInfo moi) {
int keyIdx = moi.content.indexOf(' ');
if (keyIdx < 0) { //没有找到空格找"*"
keyIdx = moi.content.indexOf('*');
}
if (keyIdx < 0) { //没有找到空格找"#"
keyIdx = moi.content.indexOf('#');
}
String strKey = "";
if (keyIdx > 0) {
strKey = moi.content.substring(0, keyIdx);
} else
strKey = moi.content;
return strKey.trim();
}
private MODispatcher() {
moHandlers = new Hashtable(); //初始化的类对象
//载入指令列表和类列表
allMessage = new Hashtable();
this.initCommand();
System.out.println("end create singleton");
}
private static MODispatcher singleton;
public static MODispatcher getInstance() {
if (singleton == null)
singleton = new MODispatcher();
return singleton;
}
/**
* 重新载入指令。
* */
public void reloadCommand() {
//清除指令,业务对象
allCmd.clear();
moHandlers.clear();
allMessage.clear();
initCommand();
}
public String getMessage(String msgId) {
Object obj = allMessage.get(msgId);
if (obj == null)
return msgId;
else
return obj.toString();
}
public String getMessage(String msgId, String[] args) {
Object obj = allMessage.get(msgId);
if (obj == null)
return msgId;
else {
StringBuffer sb = new StringBuffer(obj.toString());
for (int i = 0; i < args.length; i++) {
int start = sb.indexOf("{" + i + "}");
if (start < 0) {
} else {
sb.replace(start, start + 3, args[i]);
}
}
return sb.toString();
}
}
/**
* 载入指令。
* */
private void initCommand() {
//System.out.println("start initCommand");
//Get all MOCommand
allCmd = MOCommandDao.findAllValidCmd();
//System.out.println("start initCommand 1");
for (Iterator it = MOBusinessDao.findAllValidCmd().iterator(); it.hasNext();) {
MOBusinessVO mocmd = (MOBusinessVO) it.next();
// System.out.println("start initCommand 1.1"+mocmd.getId());
/**只有正式上线的业务才加入内存*/
try {
if (mocmd.getStatus() == CommandStatus.STATUS_ONLINE.getValue()) {
Object obj = Class.forName(mocmd.getClassName()).newInstance();
if (mocmd.getIsAbstract() == IsAbstractMOHandler.IS_ABSTRACT_MOHANDLER) {
AbstractMOHandler hdl = (AbstractMOHandler) obj;
hdl.setProductId(mocmd.getProductId());
//hdl.setRetMsg(mocmd.getRetMsg());
}
moHandlers.put(mocmd.getClassName(), obj);
}
} catch (Throwable ex) {
logger.error(ex, ex);
}
}
//System.out.println("start initCommand 2");
for (Iterator it = MessageDao.findAll().iterator(); it.hasNext();) {
MessageVO msg = (MessageVO) it.next();
allMessage.put(msg.getId(), msg.getMsg());
}
// System.out.println("start initCommand3");
}
static final Logger logger = Logger.getLogger(MODispatcher.class);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?