/**
* 2012-8-3 上午10:25:29
* @author zhangjun
*/
package org.fdzz.service;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fdzz.db.DBManager;
import org.fdzz.proxy.ServiceProxy;
import org.fdzz.server.BusinessClient;
import org.fdzz.service.impl.BalanceServiceImpl;
import org.fdzz.service.impl.ChargeServiceImpl;
import org.fdzz.service.impl.LoginServiceImpl;
import org.fdzz.service.impl.MessageServiceImpl;
import org.fdzz.service.impl.SPServiceImpl;
import org.fdzz.service.impl.WeatherServiceImpl;
import org.fdzz.util.PropertyUtil;
import org.fdzz.util.ProtocolAnalysis;
/**
* @author zhangjun
* 2012-8-3 上午10:25:29
* 业务转发功能模块,主要获得各种业务功能接口
*/
public class ServiceManager {
private static ServiceManager serviceManager = null;
private Map<String, BaseService> services = new HashMap<String, BaseService>();//存放各种业务接口
private ProtocolAnalysis protocolAnalysis = ProtocolAnalysis.getInstance();//协议工具类
private DBManager dbManager = DBManager.getInstance();//数据库管理类
private String path = "src/proxy.properties";//代理方法事务配置文件
private PropertyUtil propertyUtil = PropertyUtil.getInstance();//读取配置文件工具类
private Set<String> methodNames = new HashSet<String>();
private Log logger = LogFactory.getLog(ServiceManager.class);
//这里的常量主要是获取代理方法的配置
public static String LOGIN_SERVICE = "LoginService";//登陆业务
public static String MESSAGE_SERVICE = "MessageService";//短信业务
public static String SP_SERVICE = "SPService";//SP服务业务
public static String CHARGE_SERVICE = "ChargeService";//话费业务
public static String WEATHER_SERVICE = "WeatherService";//天气业务
public static String Balance_SERVICE = "BalanceService";//话费查询
private ServiceManager() {
}
/**
* 打开一个连接
* @return
*/
public Connection openConnection() {
return dbManager.getConnection();
}
/**
* 获取业务接口
* @param serviceName
* @return
*/
public BaseService getService(String serviceName) {
Connection connection = dbManager.getConnection();
BaseService service = services.get(serviceName);
service.setConnection(connection);
return service;
}
/**
* 获取业务功能模块接口
* @return
*/
public static ServiceManager getInstance() {
if (serviceManager == null) {
serviceManager = new ServiceManager();
serviceManager.initialServices();
}
return serviceManager;
}
/**
* 初始化业务管理类,主要是实例化各业务类的代理对象和读取需要添加事务的方法名
*/
@SuppressWarnings("unchecked")
public void initialServices() {
Properties prop = propertyUtil.getProperty(path);
String loginMethod = prop.getProperty(LOGIN_SERVICE);
if (loginMethod != null) {
String[] methods = loginMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
String spMethod = prop.getProperty(SP_SERVICE);
if (spMethod != null) {
String[] methods = spMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
String messageMethod = prop.getProperty(MESSAGE_SERVICE);
if (messageMethod != null) {
String[] methods = messageMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
String weatherMethod = prop.getProperty(WEATHER_SERVICE);
if (weatherMethod != null) {
String[] methods = weatherMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
String balanceMethod = prop.getProperty(Balance_SERVICE);
if (balanceMethod != null) {
String[] methods = balanceMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
String chargeMethod = prop.getProperty(CHARGE_SERVICE);
if (chargeMethod != null) {
String[] methods = chargeMethod.split(",");
for (int i = 0; i < methods.length; i++) {
methodNames.add(methods[i].trim());
}
}
LoginServiceImpl ls = new LoginServiceImpl();
Class clazz = ls.getClass();
BaseService loginProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(ls));
serviceManager.services.put(LOGIN_SERVICE, loginProxy);
SPServiceImpl si = new SPServiceImpl();
clazz = si.getClass();
BaseService spProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(si));
serviceManager.services.put(SP_SERVICE, spProxy);
MessageServiceImpl ms = new MessageServiceImpl();
clazz = ms.getClass();
BaseService messageProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(ms));
serviceManager.services.put(MESSAGE_SERVICE, messageProxy);
WeatherServiceImpl ws = new WeatherServiceImpl();
clazz = ws.getClass();
BaseService weatherProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(ws));
serviceManager.services.put(ProtocolAnalysis.MAP_DESTINATE_WEATHER, weatherProxy);
BalanceServiceImpl bs = new BalanceServiceImpl();
clazz = bs.getClass();
BaseService balanceProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(bs));
serviceManager.services.put(ProtocolAnalysis.MAP_DESTINATE_LEFTMONEY, balanceProxy);
ChargeServiceImpl cs = new ChargeServiceImpl();
clazz = cs.getClass();
BaseService chargeProxy = (BaseService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), new ServiceProxy(cs));
serviceManager.services.put(ProtocolAnalysis.MAP_DESTINATE_CHARGE, chargeProxy);
}
/**
* 获取代理方法名称
* @return
*/
public Set<String> getMethodNames() {
return methodNames;
}
/**
* 实现业务转发
* @param request
* @
*/
public void doBusiness(BusinessClient bizClient, Map<String, String> request) {
if (logger.isInfoEnabled()) {
logger.info("用户" + bizClient.getUsername() + "进行业务选择....");
}
String cmd = request.get(ProtocolAnalysis.MAP_PDU);
Map<String, String> response = null;
if (cmd.equals(ProtocolAnalysis.CMD_LOGOUT)) {//登出请求
if (logger.isInfoEnabled()) {
logger.info("用户" + bizClient.getUsername() + "启动登出业务....");
}
LoginService loginService = (LoginService) serviceManager.getService(ServiceManager.LOGIN_SERVICE);
String username = request.get(ProtocolAnalysis.MAP_USERNAME);
response = loginService.logout(username);
String message = protocolAnalysis.makeResponseMessage(response);
bizClient.sendMessage(message);
bizClient.clients.remove(username);
bizClient.beConnected = false;
} else if (cmd.equals(ProtocolAnalysis.CMD_LOGIN)) {//登入请求
if (logger.isInfoEnabled()) {
logger.info("用户" + bizClient.getUsername() + "启动登陆业务....已在登陆状态,重复登入,出错啦!");
}
//已在登陆状态,重复登入,出错啦
response = new HashMap<String, String>();
response.put(ProtocolAnalysis.MAP_STATUS, ProtocolAnalysis.STATUS_REPEATLOGIN);
String message = protocolAnalysis.makeResponseMessage(response);
bizClient.sendMessage(message);
} else if (cmd.equals(ProtocolAnalysis.CMD_MESSAGE)) {//短信请求
if (logger.isInfoEnabled()) {
logger.info("用户" + bizClient.getUsername() + "启动短信请求业务....");
logger.info("把这条短信记录下来....");
}
String destinate = request.get(ProtocolAnalysis.MAP_DESTINATE).trim();
//首先记录这条短信
MessageService messageService = (Message