package com.vci.mw; import com.vci.corba.common.data.VCIInvocationInfo; import java.awt.Frame; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ClientContextVariable { /** * 客户启动模式 */ private static LaunchModeEnum clientLanuchMode = LaunchModeEnum.Normal; public static LaunchModeEnum getClientLanuchMode() { return clientLanuchMode; } public static void setClientLanuchMode(LaunchModeEnum clientLanuchMode) { ClientContextVariable.clientLanuchMode = clientLanuchMode; } private static String webBasePath = ""; public static String getWebBasePath() { return webBasePath; } public static void setWebBasePath(String webBasePath) { ClientContextVariable.webBasePath = webBasePath; } public static final String CLIENT_LAST_REQUEST_TS = "CLIENT.LAST.REQUEST.TS"; private static Frame mainFrame = null; private static VCIInvocationInfo vcii = null; private static Map userMap = new HashMap(); public static String[] GLOBAL_VAR_KEYS = {"CURRENTUSER.ID", "CURRENTUSER.NAME", "CURRENTUSER.SECRETGRADE", "CURRENTUSER.EMAIL", "CURRENTUSER.GROUPNUM", "CURRENTMACHINE.SECRET", "IPSECRETSWITCH", "CURRENTUSER.GROUPNAME", "USERSECRETSWITCH", CLIENT_LAST_REQUEST_TS}; public ClientContextVariable() { } /** * 返回与客户端(会话)上下文相关的 VCIInvocationInfo *

同时兼容的富客户端、WEB客户端调用,前提条件:必须登录是登录后调用生效

* @return */ public static VCIInvocationInfo getVCIInvocationInfoForSessionContext(){ VCIInvocationInfo res = InvocationUtility.getInvocation(); if(res != null) { // IS WEB APP return res; } else if(ClientContextVariable.getClientLanuchMode() == LaunchModeEnum.WebApp) { res = InvocationUtility.getInvocation(); // WEB APP } else { // 富客户端(C) res = ClientContextVariable.getInvocationInfo(); } return res; } /** * 判断输入得值是否为全局变量 * @param key * @return */ public static boolean isGlobalVarKey(String key) { boolean isGlobalKey = false; if (key == null || key.equals("")) { return isGlobalKey; } for (int i = 0; i < GLOBAL_VAR_KEYS.length; i++) { if (key.equals(GLOBAL_VAR_KEYS[i])) { isGlobalKey = true; break; } } return isGlobalKey; } public static void setInvocationInfo(VCIInvocationInfo vcii) { ClientContextVariable.vcii = vcii; } public static void setUserMap(String userName, VCIInvocationInfo vcii) { ClientContextVariable.userMap.put(userName, vcii); } public static VCIInvocationInfo getUserInvocation(String userName) { return (VCIInvocationInfo)userMap.get(userName); } public static VCIInvocationInfo getInvocationInfo() { return vcii; } public static void setFrame(Frame mainFrame) { ClientContextVariable.mainFrame = mainFrame; } public static Frame getFrame() { return mainFrame; } /** * CS系统获取变量值的方法 * @param key * @return */ public static String getVariablebyKey(String key) { if(getInvocationInfo() == null) return ""; if(getInvocationInfo().extAttribs == null) return ""; String exAttrs[] = getInvocationInfo().extAttribs; for(int i = 0; i < exAttrs.length; i++) if(exAttrs[i] != null && exAttrs[i].indexOf((new StringBuilder(String.valueOf(key))).append("=").toString()) == 0) return exAttrs[i].split("=").length > 1 ? exAttrs[i].split("=")[1] : ""; return ""; } public static void putGlobalVariable(String key, String value) { if(getInvocationInfo() == null) { return; } VCIInvocationInfo cinfo = getInvocationInfo(); putGlobalVariable(cinfo, key, value); } protected static void putGlobalVariable(VCIInvocationInfo cinfo, String key, String value) { String[] extAttribs = cinfo.extAttribs; if (extAttribs == null) { extAttribs = new String[0]; } List list = new ArrayList(); int index = -1; for (int i = 0; i < extAttribs.length; i++) { String ckey = extAttribs[i].split("=")[0]; if (ckey.equals(key)) { index = i; list.add(key + "=" + value); } else { list.add(extAttribs[i]); } } if (index == -1) { list.add(key + "=" + value); } cinfo.extAttribs = list.toArray(new String[0]); } /** * 获取cs全局变量值 * @return */ public static Map getGlobalKeyValues() { Map values = new HashMap(); for (int i = 0; i < GLOBAL_VAR_KEYS.length; i++) { values.put(GLOBAL_VAR_KEYS[i], getVariablebyKey(GLOBAL_VAR_KEYS[i])); } return values; } /** * 获取BS全局变量值 * @param userName * @return */ public static Map getGlobalKeyValues(String userName) { Map values = new HashMap(); for (int i = 0; i < GLOBAL_VAR_KEYS.length; i++) { values.put(GLOBAL_VAR_KEYS[i], getVariablebyKey(userName, GLOBAL_VAR_KEYS[i])); } return values; } /** * BS环境获取变量值的方法 * @param userName * @param key * @return */ public static String getVariablebyKey(String userName, String key) { if(getUserInvocation(userName) == null) return ""; if(getUserInvocation(userName).extAttribs == null) return ""; String exAttrs[] = getUserInvocation(userName).extAttribs; for(int i = 0; i < exAttrs.length; i++) if(exAttrs[i] != null && exAttrs[i].indexOf((new StringBuilder(String.valueOf(key))).append("=").toString()) == 0) return exAttrs[i].split("=").length > 1 ? exAttrs[i].split("=")[1] : ""; return ""; } public static void putGlobalVariable(String userName, String key, String value) { if(getUserInvocation(userName) == null) { return; } VCIInvocationInfo cinfo = getUserInvocation(userName); putGlobalVariable(cinfo, key, value); } /** * 清空非全局变量的值 */ public static void cleanOtherGlobalVariable() { if(getInvocationInfo() == null) { return; } if (getInvocationInfo().extAttribs == null || getInvocationInfo().extAttribs.length == 0) { return; } VCIInvocationInfo cinfo = getInvocationInfo(); cleanOtherGloablAttribute(cinfo); } /** * 清空非全局变量的值 * @param userName */ public static void cleanOtherGlobalVariable(String userName) { if(getUserInvocation(userName) == null) { return; } if (getUserInvocation(userName).extAttribs == null || getUserInvocation(userName).extAttribs.length == 0) { return; } VCIInvocationInfo cinfo = getUserInvocation(userName); cleanOtherGloablAttribute(cinfo); } private static void cleanOtherGloablAttribute(VCIInvocationInfo info) { List list = new ArrayList(); for (String key : info.extAttribs) { String[] keys = key.split("="); if (keys.length == 0) { continue; } if (isGlobalVarKey(keys[0])) { list.add(key); } } info.extAttribs = list.toArray(new String[0]); } /** * 获取用户ID * @return */ public static String getUserId() { return getVariablebyKey("CURRENTUSER.ID"); } /** * 获取用户ID * @param userName,当前登录的用户名 * @return */ public static String getUserId(String userName) { return getVariablebyKey(userName, "CURRENTUSER.ID"); } /** * 获取当前登录用户的用户名 * @return */ public static String getUserName() { return getVariablebyKey("CURRENTUSER.NAME"); } /** * 获取当前登录用户的用户名 * @param userName,当前登录的用户名 * @return */ public static String getUserName(String userName) { return getVariablebyKey(userName, "CURRENTUSER.NAME"); } /** * 获取当前用户的密级 * @return */ public static String getUserSecret() { return getVariablebyKey("CURRENTUSER.SECRETGRADE"); } /** * 获取当前用户的密级 * @param userName,当前登录的用户名 * @return */ public static String getUserSecret(String userName) { return getVariablebyKey(userName, "CURRENTUSER.SECRETGRADE"); } /** * 获取当前用户的email地址 * @return */ public static String getUserEmail() { return getVariablebyKey("CURRENTUSER.EMAIL"); } /** * 获取当前用户的email地址 * @param userName,当前登录的用户名 * @return */ public static String getUserEmail(String userName) { return getVariablebyKey(userName, "CURRENTUSER.EMAIL"); } /** * 获取当前用户所在组的组号 * @return */ public static String getUserGroupNum() { return getVariablebyKey("CURRENTUSER.GROUPNUM"); } /** * 获取当前用户所在组的组号 * @param userName,当前登录的用户名 * @return */ public static String getUserGroupNum(String userName) { return getVariablebyKey(userName, "CURRENTUSER.GROUPNUM"); } /** * 获取登录用户使用的计算机机器密级 * @return */ public static String getMachineSecret() { return getVariablebyKey("CURRENTMACHINE.SECRET"); } /** * 获取登录用户使用的计算机机器密级 * @param userName,当前登录的用户名 * @return */ public static String getMachineSecret(String userName) { return getVariablebyKey(userName, "CURRENTMACHINE.SECRET"); } /** * 获取系统的密级开关值,true为打开,false为关闭 * @return */ public static String getIpSecretSwitch() { return getVariablebyKey("IPSECRETSWITCH"); } /** * 获取系统的密级开关值,true为打开,false为关闭 * @param userName,当前登录的用户名 * @return */ public static String getIpSecretSwitch(String userName) { return getVariablebyKey(userName, "IPSECRETSWITCH"); } /** * 获取系统的用户密级开关值,on或true为打开,off或false为关闭 * @param userName 当前登录的用户名 * @return */ public static String getUserSecretSwith(){ return getVariablebyKey("USERSECRETSWITCH"); } /** * 获取系统的用户密级开关值,on或true为打开,off或false为关闭 * @param userName 当前登录的用户名 * @return */ public static String getUserSecretSwith(String userName){ return getVariablebyKey(userName, "USERSECRETSWITCH"); } /** * 获取当前用户部门名称 * @return */ public static String getGroupName() { return getVariablebyKey("CURRENTUSER.GROUPNAME"); } /** * 获取当前用户部门名称 * @param userName * @return */ public static String getGroupName(String userName) { return getVariablebyKey(userName, "CURRENTUSER.GROUPNAME"); } /** * 返回客户端最后一次请求服务端的时间 * @return */ public static String getClientLastRequestTS(){ return getVariablebyKey(CLIENT_LAST_REQUEST_TS); } /** * 返回客户端最后一次请求服务端的时间 * @param userName * @return */ public static String getClientLastRequestTS(String userName){ return getVariablebyKey(userName, CLIENT_LAST_REQUEST_TS); } }