田源
2025-01-16 39269c81905457378a73dc83050349d7a364a1f8
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.vci.client;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;
import java.util.regex.Pattern;
 
import com.vci.client.common.objects.DefaultAppEventManager;
import com.vci.client.common.providers.ServiceProvider;
import com.vci.client.common.ClientLog4j;
import com.vci.client.common.interfaces.IAppEventManager;
import com.vci.common.resource.CommonProperties;
import com.vci.corba.common.VCIError;
import com.vci.corba.framework.FrameworkServicePrx;
//import com.vci.corba.framework.method.VolumeManagerPrx;
import com.vci.corba.workflow.WorkflowServicePrx;
 
public class ClientSession {
    
    private static IAppEventManager _appEventMng = new DefaultAppEventManager(); 
    private static FrameworkServicePrx frameworkService = null;
//    private static VolumeManagerPrx _volumeFactory = null;
    private static WorkflowServicePrx workflowService = null;
    private static Date loginTime = null;
    private static long min = 0;
    private static int betweenTime = 0;
    private static int loginovertime = Integer.parseInt(CommonProperties.getStringProperty("logon.loginovertime"));
    private static boolean timeOutFlag = false;
    
    
    private static ThreadLocal<SimpleDateFormat> sdfTheradLocal = new ThreadLocal<SimpleDateFormat>();
    private static SimpleDateFormat getSimpleDateFormate(){
        SimpleDateFormat sdf = sdfTheradLocal.get();
        if(sdf == null){
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        return sdf;
    }
    
    public static boolean isTimeOutFlag() {
        return timeOutFlag;
    }
 
    public static void setTimeOutFlag(boolean timeOutFlag) {
        ClientSession.timeOutFlag = timeOutFlag;
    }
 
    // 是否需要检查客户端口已超时,如果超时,则弹出登录窗口,进行重新登录
    // 考虑到项目的jar包会嵌入到其它集成系统或宿主环境中进行(以JAR的方式提供集成)
    // 以这种方式集成时,是不需要检查客户端是否已经超时(此种方式运行时,根本也没有客户端UI)
    // 但在RMIP主系统中使用时,就需要进行客户端口UI超时检查了
    private static boolean checkClientUITimeout = false;
    
    /**
     * 返回 是否需要检查客户端UI是否超时
     * <p>Description: </p>
     * @author xchao
     * @time 2013-8-16
     * @param checkClientUITimeout
     */
    public static boolean isCheckClientUITimeout() {
        return ClientSession.checkClientUITimeout;
    }
    /**
     * 设置是否需要检查客户端口UI是否超时
     * <p>Description: </p>
     * @author xchao
     * @time 2013-8-16
     * @return
     */
    public static void setCheckClientUITimeout(boolean checkClientUITimeout) {
        ClientSession.checkClientUITimeout = checkClientUITimeout;
    }
 
    // 最近一次成功连接到Corba服务器的时间
    private static long latestConnectToCorbaServerTime = 0;
    
    // 数值正则
    private static Pattern numPtn = Pattern.compile("[0-9]+");
    
    // Corba客户端最大空闲时间,如果配置文件中未定义,则按默认值 1000*1*60*60*20,即:20小时
    private static long corbaClientIdle = (
            (CommonProperties.getStringProperty("corbaClientMaxIdle") != null 
            && CommonProperties.getStringProperty("corbaClientMaxIdle").trim().length() != 0
            && numPtn.matcher(CommonProperties.getStringProperty("corbaClientMaxIdle").trim()).matches()) ?
            Long.parseLong(CommonProperties.getStringProperty("corbaClientMaxIdle")) : (1000*1*60*60*20));
    
    // 最大尝试连接到Corba服务器的次数,如果配置文件中未定义,则按 5
    private static int maxTryNum =  (
            (CommonProperties.getStringProperty("corbaConnectMaxTryNum") != null 
            && CommonProperties.getStringProperty("corbaConnectMaxTryNum").trim().length() != 0
            && numPtn.matcher(CommonProperties.getStringProperty("corbaConnectMaxTryNum").trim()).matches()) ?
            Integer.parseInt(CommonProperties.getStringProperty("corbaConnectMaxTryNum")) : (5));
    
    public static void reInit() {
        // 最多执行5次连接,
        for(int i = 0; i < maxTryNum; i++){
            try {
                frameworkService = ServiceProvider.getFrameService();
                setAllToNull(false);
                // 存储最近一次成功连接到Corba服务器的时间
                latestConnectToCorbaServerTime = System.currentTimeMillis();
                ClientLog4j.logger.info("执行第" + (i+1) + "次与平台服务之间的连接成功!");
                // 如果一次连接成功,则退出循环
                if (frameworkService != null)
                    break;
            } catch (Exception e) {
                ClientLog4j.logger.error("执行第" + (i+1) + "次与平台服务之间的连接时发生错误!", e);
                e.printStackTrace();
                setAllToNull(true);
            }
        }
    }
    
    private static void setAllToNull(boolean hasRMIP){
        if(hasRMIP) {
            frameworkService = null;
        }
        workflowService = null;
    }
    
    private static void checkConnect() throws VCIError{
        // 检查当前时间与最近一次成功连接到Corba服务器的时间之间的间隔
        // 检查时间间隔是否超出指标,如果超出,则执行重新连接
        if(System.currentTimeMillis() - latestConnectToCorbaServerTime >= corbaClientIdle){
            reInit();
        }
        
        if(frameworkService == null){
            throw new VCIError("-10000", new String[]{});
        }
    }
    
    public static void setAppEventManager(IAppEventManager eventMng) {
        _appEventMng = eventMng;
    }
    
    public static boolean checkExpired() {
        if (_appEventMng != null)
            return _appEventMng.checkExpired();
        else
            return false;
    }
    
    public static void relogin() {
        if (_appEventMng != null)
            _appEventMng.relogin();
    }
    
    public static FrameworkServicePrx getFrameworkService() throws VCIError {
        try {
            checkConnect();
            if (frameworkService == null) {
                frameworkService = ServiceProvider.getFrameService();
            } 
            checkTimeOut();
            return frameworkService; 
            
        } catch (VCIError ve) {
            throw ve;
        } catch (TimeoutException e) {
            return null;
        }
    }
    
    
//    public static VolumeManagerPrx getVolumnManager() throws VCIError {
//        try {
//            checkConnect();
//            if (_volumeFactory == null) {
//                _volumeFactory = ServiceProvider.getVolumeManager();
//            } 
//            checkTimeOut();
//            return _volumeFactory; 
//            
//        } catch (VCIError ve) {
//            throw ve;
//        } catch (TimeoutException e) {
//            return null;
//        }
//    }
 
    
    public static WorkflowServicePrx getWorkflowService() throws VCIError {
        try {
            checkConnect();
            if (workflowService == null) {
                workflowService = ServiceProvider.getWFService();
            }
            checkTimeOut();
            return workflowService;
        } catch (VCIError ve) {
            throw ve;
        } catch (TimeoutException e) {
            return null;
        }
    }
    
//    public static org.omg.CORBA.Object getNameService(String name) throws VCIError {
//        try {
//            return nsu.getObjectByName(name);
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            throw new VCIError("getNameService", new String[] {e.getMessage()});
//        }
//    }
 
    
    /**
     * 检查Client会话是否过期
     * @return
     */
    public static boolean isExpired(){
        boolean res = false;
        // add by xchao 2013.08.16 begin 根据选项配置,确定是否需要检查客户端UI是否超时
        if(!isCheckClientUITimeout()) return false;
        // add by xchao 2013.08.16 end
        
        if (loginTime != null){
             // 当前时间 - 登录时间  -》 转换为分钟
             min = (System.currentTimeMillis() - loginTime.getTime()) / (60 * 1000);
             betweenTime = (int)min;
        }
        // 与会话超时阀值进行比较
        if (betweenTime > loginovertime){
            betweenTime = 0;
            loginTime = null;
            res = true;
        }else{
            setLoginTime();
            res = false;
        }
        setTimeOutFlag(res);
        return res;
    }
    
//    /**
//     * 显示会话过期的提示信息
//     * @return
//     */
//    public static boolean showExpiredMessage(){
//        boolean expired = isExpired();
//        if(expired){
//            VCIOptionPane.showMessageDialog(LogonApplication.frame, "会话过期,请重新登录!!!!");
//        }
//        return expired;
//    }
//    
//    public static void relogin(){
//        if (LogonApplication.frame != null){
//            LogonApplication.frame.dispose();
//        }
//        new LogonApplication();
//    }
    
    /**
     * 设置登录时间
     */
    private static void setLoginTime(){
        SimpleDateFormat sdf = getSimpleDateFormate();
        betweenTime = 0;
        try {
            loginTime = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 查看链接是否器是否超时,如果超时,则必须重新登录
     * added by wangxl 2011.9.7
     * @throws TimeoutException 
     */
    private static void checkTimeOut() throws TimeoutException{
        boolean expired = isExpired();
        if(expired){
            //relogin();
            throw new TimeoutException("超时未操作!");
        }else{
            setLoginTime();
        }
    }
}