package com.vci.server.mw;
|
|
import java.util.List;
|
import java.util.ArrayList;
|
import java.util.concurrent.CompletionStage;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.hibernate.Session;
|
import com.alibaba.fastjson.JSONObject;
|
import com.vci.common.log.ServerWithLog4j;
|
import com.vci.corba.common.data.VCIInvocationInfo;
|
import com.vci.server.BaseService;
|
import com.vci.server.base.persistence.dao.HibernateSessionFactory;
|
import com.vci.server.base.utility.ServerServiceProvider;
|
import com.zeroc.Ice.Current;
|
import com.zeroc.Ice.DispatchInterceptor;
|
import com.zeroc.Ice.OutputStream;
|
import com.zeroc.Ice.Request;
|
import com.zeroc.Ice.UserException;
|
|
/**
|
* 拦截器,拦截客户端请求,解决数据库启动事务和提交、回滚事务的操作,同时将用户登录信息写入当前用户状态
|
* @author Jason
|
*
|
*/
|
public class ServerContextInterceptor extends DispatchInterceptor {
|
|
private com.zeroc.Ice.Object _servant = null;
|
|
private static List<String> _lstOP = null;
|
|
static {
|
_lstOP = new ArrayList<String>();
|
_lstOP.add("ice_isA");
|
_lstOP.add("ice_ping");
|
_lstOP.add("ice_ids");
|
_lstOP.add("ice_id");
|
}
|
|
public ServerContextInterceptor(com.zeroc.Ice.Object servant) {
|
_servant = servant;
|
}
|
|
@Override
|
public CompletionStage<OutputStream> dispatch(Request request) throws UserException {
|
Current current = request.getCurrent();
|
//
|
// Check if the operation requires authorization to invoke.
|
//
|
|
String op = current.operation;
|
|
if (_lstOP.contains(op)) {
|
return _servant.ice_dispatch(request);
|
}
|
|
String serviceName = "";
|
if (_servant instanceof BaseService) {
|
BaseService bs = (BaseService)_servant;
|
serviceName = bs.getServiceName();
|
}
|
ServerWithLog4j.logger.debug("ServerContextInterceptor.dispatch begin : " + serviceName + "." + op);
|
|
try {
|
Session session = HibernateSessionFactory.getSession();
|
session.beginTransaction();
|
|
String token = current.ctx.get("token");
|
if(!StringUtils.isBlank(token))
|
{
|
VCIInvocationInfo vcii = (VCIInvocationInfo)JSONObject.parseObject(token, VCIInvocationInfo.class);
|
if (vcii != null && !StringUtils.isBlank(vcii.userID)) {
|
ServerWithLog4j.logger.debug("ServerContextInterceptor.dispatch : curUser = " + vcii.userName);
|
|
ServerContextVariable.setInvocationInfo(vcii);
|
HibernateSessionFactory.setVciSessionInfo(vcii);
|
}
|
} else {
|
ServerWithLog4j.logger.debug("ServerContextInterceptor.receive_request no login user");
|
}
|
} catch (Throwable e) {
|
ServerWithLog4j.logger.error("dispatch begin Throwable expression", e);
|
}
|
|
try {
|
ServerServiceProvider.setCurrent(current);
|
CompletionStage<OutputStream> os = _servant.ice_dispatch(request);
|
Session session = HibernateSessionFactory.getSession();
|
if (session != null && session.getTransaction().isActive()) {
|
session.getTransaction().commit();
|
}
|
|
return os;
|
} catch (Throwable e) {
|
ServerWithLog4j.logger.error("dispatch exec throwable exception", e);
|
try {
|
Session session = HibernateSessionFactory.getSession();
|
if (session != null && session.getTransaction().isActive()) {
|
session.getTransaction().rollback();
|
}
|
} catch (Throwable ex) {
|
ServerWithLog4j.logger.error("HibernateSessionFactory rollback exception", ex);
|
}
|
throw e;
|
} finally {
|
//ServerServiceProvider.setCurrent(null);
|
|
//HibernateSessionFactory.closeSession();
|
ServerWithLog4j.logger.debug("ServerContextInterceptor.dispatch end");
|
}
|
}
|
|
}
|