Ice Service Annotation

原创
2017/01/11 10:25
阅读数 105
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value=ElementType.FIELD)
public @interface IceService {
	
	/**
	 * 远程ice服务的identify,当为空时默认取当前定义的Filed的字符串
	 */
	String name() default "";
	
	/**
	 * 如果未找到对应服务时,是否报错
	 */
	boolean check() default false;

}

 


import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.core.io.Resource;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldFilter;

import com.google.common.collect.Maps;

import Ice.InitializationData;
import Ice.ObjectPrx;
import Ice.Util;

public class IceServiceProcessor extends InstantiationAwareBeanPostProcessorAdapter {

	private static Map<String, ObjectPrx> remoteIceServiceReferenceCache = Maps.newHashMap();

	private String clientConfig = "iceclient.properties";

	private static volatile Ice.Communicator ic = null;
	
	private Resource resource;

	/**
	 * 理论上一个Controller Bean 只会被处理一次
	 */
	@Override
	public boolean postProcessAfterInstantiation(final Object bean, String beanName) throws BeansException {
		ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
			@Override
			public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
				// 某个类型的IcePrx
				Class<?> icePrxClass = field.getType();
				IceService iceServiceDefine = field.getAnnotation(IceService.class);
				String identityName = iceServiceDefine.name();
				if (StringUtils.isBlank(identityName)) {
					identityName = field.getName();
				}
				// 是否强制检测
				boolean strongCheck = iceServiceDefine.check();
				ObjectPrx target = null;
				try {
					target = getSerivcePrx(identityName, icePrxClass);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (strongCheck && (target == null)) {
					throw new IllegalArgumentException("未找到" + iceServiceDefine.name() + "的远程服务");
				}
				ReflectionUtils.makeAccessible(field);
				field.set(bean, target);
			}
		}, new FieldFilter() {
			@Override
			public boolean matches(Field field) {
				// 排除静态属性和final属性
				boolean basicCondition = !(Modifier.isStatic(field.getModifiers())|| Modifier.isFinal(field.getModifiers()));
				// 只取带有IceService注解的属性
				return basicCondition && field.getAnnotation(IceService.class) != null;
			}
		});
		return true;
	}

	public Ice.Communicator getICECommunictor() throws IOException {
		if (ic == null) {
			InitializationData initData = new InitializationData();
			initData.properties = Util.createProperties();
			if(resource!=null){
				initData.properties.load(resource.getFile().getAbsolutePath());
			}else{
				initData.properties.load(getClientConfig());
			}
			ic = Util.initialize(initData);
		}
		return ic;
	}

	private static ObjectPrx createIceProxy(Ice.Communicator communicator, Class<?> serviceCls, String identityName) {
		ObjectPrx proxy = null;
		String clsName = serviceCls.getName();
		int pos = clsName.lastIndexOf("Prx");
		if (pos <= 0) {
			throw new java.lang.IllegalArgumentException("Invalid ObjectPrx class, class name must end with Prx");
		}
		try {
			Class<?> proxyClass = Class.forName(clsName + "Helper");
			Ice.ObjectPrx base = communicator.stringToProxy(identityName);
			proxy = (ObjectPrx) proxyClass.newInstance();
			Method checkedCast = proxyClass.getDeclaredMethod("checkedCast", ObjectPrx.class);
			proxy = (ObjectPrx) checkedCast.invoke(proxy, base);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return proxy;
	}

	public ObjectPrx getSerivcePrx(String identityName, Class<?> serviceCls) throws IOException {
		String token = serviceCls + "_" + identityName;
		ObjectPrx proxy = remoteIceServiceReferenceCache.get(token);
		if (proxy != null) {
			return proxy;
		}
		proxy = createIceProxy(getICECommunictor(), serviceCls, identityName);
		if (proxy != null) {
			remoteIceServiceReferenceCache.put(token, proxy);
		}
		return proxy;
	}
 
	public String getClientConfig() {
		return clientConfig;
	}

	public void setClientConfig(String clientConfig) {
		this.clientConfig = clientConfig;
	}
	
	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}
}

 

展开阅读全文
ice
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部