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();
|
}
|
}
|
}
|