package com.csair.soc.aircrew.rostereditor.httpinvoke;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import com.csair.soc.aircrew.rulecheck.rmi.RuleCheckService;
/**
* 远程调用工具类
*
* @author Johnson
* @version Saturday March 2nd, 2013
*/
public class HttpInvokeFactory {
private static String URL = "http://10.95.18.88:8080/aircrew-rulecheck-biz";
private static ConcurrentMap<String, Object> serviceProxyMap = new ConcurrentHashMap<String, Object>();
private static ConcurrentMap<Class<?>, String> serviceUrlMap = new ConcurrentHashMap<Class<?>, String>();
/**
* 注册默认服务地址
*/
static {
Properties properties = new Properties();
// jndi配置文件路径
String filePath = "META-INF/prop/httpinvoke.properties";
InputStream in = HttpInvokeFactory.class.getClassLoader().getResourceAsStream(filePath);
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
URL = properties.getProperty("httpinvoke.url");
serviceUrlMap.put(RuleCheckService.class, URL + "/RuleCheckHttpInvokerService");
}
/**
* 创建服务对象
*
* @param <T>
* @param serviceUrl
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
private static <T> T createService(String serviceUrl, Class<T> clazz) {
HttpInvokerProxyFactoryBean bean = new HttpInvokerProxyFactoryBean();
bean.setServiceUrl(serviceUrl);
bean.setServiceInterface(clazz);
bean.afterPropertiesSet();
return (T) bean.getObject();
}
/**
* 添加服务地址
*
* @param clazz
* @param serviceUrl
*/
public static void put(Class<?> clazz, String serviceUrl) {
if (serviceUrlMap.containsKey(clazz)) {
return;
}
serviceUrlMap.put(clazz, serviceUrl);
}
/**
* 获取服务对象
*
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz) {
String serviceUrl = serviceUrlMap.get(clazz);
if (serviceUrl == null) {
return null;
}
Object service = serviceProxyMap.get(serviceUrl);
if (service == null) {
service = HttpInvokeFactory.createService(serviceUrl, clazz);
serviceProxyMap.put(serviceUrl, service);
}
return (T) service;
}
}
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import com.csair.soc.aircrew.rulecheck.rmi.RuleCheckService;
/**
* 远程调用工具类
*
* @author Johnson
* @version Saturday March 2nd, 2013
*/
public class HttpInvokeFactory {
private static String URL = "http://10.95.18.88:8080/aircrew-rulecheck-biz";
private static ConcurrentMap<String, Object> serviceProxyMap = new ConcurrentHashMap<String, Object>();
private static ConcurrentMap<Class<?>, String> serviceUrlMap = new ConcurrentHashMap<Class<?>, String>();
/**
* 注册默认服务地址
*/
static {
Properties properties = new Properties();
// jndi配置文件路径
String filePath = "META-INF/prop/httpinvoke.properties";
InputStream in = HttpInvokeFactory.class.getClassLoader().getResourceAsStream(filePath);
try {
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
}
URL = properties.getProperty("httpinvoke.url");
serviceUrlMap.put(RuleCheckService.class, URL + "/RuleCheckHttpInvokerService");
}
/**
* 创建服务对象
*
* @param <T>
* @param serviceUrl
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
private static <T> T createService(String serviceUrl, Class<T> clazz) {
HttpInvokerProxyFactoryBean bean = new HttpInvokerProxyFactoryBean();
bean.setServiceUrl(serviceUrl);
bean.setServiceInterface(clazz);
bean.afterPropertiesSet();
return (T) bean.getObject();
}
/**
* 添加服务地址
*
* @param clazz
* @param serviceUrl
*/
public static void put(Class<?> clazz, String serviceUrl) {
if (serviceUrlMap.containsKey(clazz)) {
return;
}
serviceUrlMap.put(clazz, serviceUrl);
}
/**
* 获取服务对象
*
* @param <T>
* @param clazz
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T getService(Class<T> clazz) {
String serviceUrl = serviceUrlMap.get(clazz);
if (serviceUrl == null) {
return null;
}
Object service = serviceProxyMap.get(serviceUrl);
if (service == null) {
service = HttpInvokeFactory.createService(serviceUrl, clazz);
serviceProxyMap.put(serviceUrl, service);
}
return (T) service;
}
}