wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.vci.common.util;
 
import java.util.HashMap;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.common.log.ServerWithLog4j;
import com.vci.common.resource.IceClientProperties;
import com.zeroc.Ice.ObjectPrx;
import com.zeroc.Ice.Util;
 
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: VCI</p>
 * @author eddie
 * @time 2009-4-14
 * @version 1.0
 */
public abstract class IceProxyUtility {
 
    private static final String locatorKey = "--Ice.Default.Locator";
    
    private com.zeroc.Ice.Communicator _communicator = null;
    
    protected IceProxyUtility() {
    }
    
 
    public void InitIceEnv()
    {
 
    }
    
     public void InitCommunicator(String[] args)
    {
        try
        {
            String[] initParams = new String[0];
            if (IceClientProperties.Endpoints().contains("/"))
                initParams = new String[]{locatorKey + "=" + IceClientProperties.Endpoints()};
            
            _communicator = Util.initialize(initParams);
            
             //_communicator = com.zeroc.Ice.Util.initialize(args);
             //_communicator.getDefaultRouter();
        } catch (Exception e) {
            ServerWithLog4j.logger.error("InitCommunicator Error", e);
            throw e;
        }
    }
 
 
     /**
      * 获取服务对象代理
      * @param name
      * @return
      * @throws Exception
      */
    public ObjectPrx getObjectByName(String name, String adapterRep) throws Exception
    {
        ObjectPrx prx = null;
        try 
        {
            String proxyLocator = name;
            if (!IceClientProperties.Endpoints().contains("/")) {
                proxyLocator = name + ":" + IceClientProperties.Endpoints();
            } else {
                proxyLocator = name + "@" + adapterRep;
            }
            
            prx = _communicator.stringToProxy(proxyLocator);
 
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        
        String[] ids = prx.ice_ids();
        
        if (prx == null)
            throw new Exception("Not Found ObjectPrx " + name);
        
        Map<String, String> context = getCurUserContext();
        if (context != null) {
            return prx.ice_context(context);
        } else {
            return prx;
        }
    }
    
 
    
    /**
     * 获取服务对象代理,并设置context信息
     * @param name
     * @param context
     * @return
     * @throws Exception
     */
    public ObjectPrx getObjectByName(String name, Map<String, String> context) throws Exception
    {
        try 
        {
            String proxyLocator = name + ":" + IceClientProperties.Endpoints();
            
            ObjectPrx prx = _communicator.stringToProxy(proxyLocator);
            
            if (context != null) {
                return prx.ice_context(context);
            }
            
            return prx;
            
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
    
    
 
    /**
     * 获取当前用户信息构建服务代理对象上下文
     * @return
     */
    private Map<String, String> getCurUserContext() {
        Map<String, String> context = new HashMap<String, String>();
        
        String token = getInvocationInfo();
        if (token == null)
            return null;
        
        context.put("token", getInvocationInfo());
        return context;
    }
 
    
    protected abstract String getInvocationInfo();
//    {
//        VCIInvocationInfo vcii = null;
//        if (ServerContextVariable.getInvocationInfo() != null) {
//            
//            vcii = ServerContextVariable.getInvocationInfo();
//            
//        }
//        else {
//            if (ClientContextVariable.getClientLanuchMode().equals(LaunchModeEnum.Normal)) {
//                vcii = ClientContextVariable.getInvocationInfo();
//            } else {
//                vcii = InvocationUtility.getInvocation();
//            }
//        }
//        if (vcii == null || StringUtils.isBlank(vcii.userID)) {
//            return null;
//        } else {
//            if (vcii.extAttribs == null)
//                vcii.extAttribs = new String[0];
//            if (vcii.groupIDs == null)
//                vcii.groupIDs = new String[0];
//            if (vcii.groupNames == null)
//                vcii.groupNames = new String[0];
//            if (vcii.roleIDs == null)
//                vcii.roleIDs = new String[0];
//            if (vcii.roleNames == null)
//                vcii.roleNames = new String[0];
//        }
//        
//        return JSONObject.toJSONString(vcii);
//    }
 
}