166 lines
4.8 KiB
Java
166 lines
4.8 KiB
Java
package com.sunyard.proxy;
|
|
|
|
import com.sunyard.SydApi;
|
|
import com.sunyard.SydApiException;
|
|
import com.sunyard.log.ILogFactory;
|
|
import com.sunyard.log.ILogger;
|
|
import com.sunyard.pool.HsmLinkInfo;
|
|
import racal.sunyard.main.SydApi4j;
|
|
import racal.sunyard.main.SydApiMuti;
|
|
|
|
import java.lang.reflect.InvocationHandler;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Proxy;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
public class ProxyInvocationHandler implements InvocationHandler {
|
|
|
|
private static Map<String, SydApiException> innerFuncs = new HashMap<String, SydApiException>();
|
|
|
|
static {
|
|
innerFuncs.put("connect", new SydApiException("集群连接模式,不需要该连接", 0));
|
|
}
|
|
|
|
private SydApi4j[] apis;
|
|
private HsmLinkInfo[] apisInfos;
|
|
private boolean isShortLinkMode = true;
|
|
|
|
|
|
public ProxyInvocationHandler(SydApi4j[] apis) {
|
|
this.apis = apis;
|
|
}
|
|
|
|
public ProxyInvocationHandler(SydApi4j[] apis, HsmLinkInfo[] info, boolean isShortLinkMode) {
|
|
this.apis = apis;
|
|
this.apisInfos = info;
|
|
this.isShortLinkMode = isShortLinkMode;
|
|
}
|
|
|
|
public SydApi4j[] getApis() {
|
|
return apis;
|
|
}
|
|
|
|
|
|
public HsmLinkInfo[] getApisInfos() {
|
|
return apisInfos;
|
|
}
|
|
|
|
public SydApi getProxy() {
|
|
SydApi api = (SydApi) Proxy.newProxyInstance(
|
|
SydApi4j.class.getClassLoader(),
|
|
new Class[]{SydApi.class},
|
|
this
|
|
);
|
|
api.setBindObject( this );
|
|
return api;
|
|
}
|
|
|
|
public void disconnect() {
|
|
|
|
if (null != apis && isShortLinkMode) {
|
|
for (SydApi4j api4j : this.apis) {
|
|
try {
|
|
if (null != api4j) {
|
|
api4j.disconnect();
|
|
}
|
|
} catch (Exception e) {
|
|
// pass
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//处理代理实例并返回结果
|
|
@Override
|
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
|
|
SydApiException le = null;
|
|
|
|
if ("disconnect".equals(method.getName())) {
|
|
this.disconnect();
|
|
}
|
|
|
|
// 屏蔽部分非业务接口
|
|
if ("connect".equals(method.getName())) {
|
|
throw new RuntimeException("不允许使用此接口");
|
|
}
|
|
if (innerFuncs.containsKey(method.getName())) {
|
|
innerFuncs.get(method.getName());
|
|
}
|
|
|
|
|
|
if ("deleteCert".equals(method.getName()) || "SYD_Get_X509_CertInfo_HA".equals(method.getName())) {
|
|
for (SydApi4j api : apis) {
|
|
if (null == api) {
|
|
if ("deleteCert".equals(method.getName())) {
|
|
throw new SydApiException("删除失败",56307);
|
|
} else {
|
|
throw new SydApiException("导入失败",56300);
|
|
}
|
|
}
|
|
try {
|
|
|
|
Object ret = method.invoke(api, args);
|
|
if ( ret instanceof Boolean ) {
|
|
if ( ! ( Boolean ) ret ) {
|
|
throw new RuntimeException("结果错误");
|
|
}
|
|
}
|
|
|
|
} catch (SydApiException e) {
|
|
if ("deleteCert".equals(method.getName())) {
|
|
throw new SydApiException("删除失败",56307);
|
|
} else {
|
|
throw new SydApiException("导入失败",56300);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for (SydApi4j api : apis) {
|
|
if (null == api) {
|
|
continue;
|
|
}
|
|
if ( isShortLinkMode ) {
|
|
try {
|
|
|
|
return method.invoke(api, args);
|
|
|
|
} catch (SydApiException e) {
|
|
le = e;
|
|
if (e.getRetCode() < 0) {
|
|
continue;
|
|
}
|
|
throw e;
|
|
}
|
|
} else {
|
|
synchronized (api) {
|
|
try {
|
|
return method.invoke(api, args);
|
|
|
|
} catch (SydApiException e) {
|
|
le = e;
|
|
if (e.getRetCode() < 0) {
|
|
continue;
|
|
}
|
|
throw e;
|
|
} catch ( Exception e ) {
|
|
ILogFactory factory = SydApi4j.getLogFactory();
|
|
if ( null != factory ) {
|
|
ILogger logger = factory.getLogger(ProxyInvocationHandler.class);
|
|
logger.error( "调用方法 {} 失败", method.getName() );
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
throw le;
|
|
}
|
|
}
|
|
|
|
|
|
|