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;
/**
*
Title:
* Description:
* Copyright: Copyright (c) 2009
* Company: VCI
* @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 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 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 getCurUserContext() {
Map context = new HashMap();
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);
// }
}