ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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");
        }
    }
 
}