您的位置:首页 > 其它

实现简单的WebService测试用发布工具

2014-11-06 15:22 246 查看
这几天研究WebService,测试启什么server很烦,直接写main又嫌写烦,干脆写了个简单的发布工具,通过load properties来实现publish。

上代码:

public class Publisher {
	
	public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
		Properties pros = new Properties();
		pros.load(Publisher.class.getResourceAsStream("service.properties"));
		Set<String> keys = pros.stringPropertyNames();
		if(keys != null){
			Map<String, ServiceProperty> services = new HashMap<String, Publisher.ServiceProperty>();
			for (String key : keys) {
	            String[] ks = key.split("\\.");
	            if(ks != null && ks.length==3 && "service".equalsIgnoreCase(ks[0])){
	            	String name = ks[1];
	            	ServiceProperty sp;
	            	if(services.containsKey(name)){
	            		sp = services.get(name);
	            	}else{
	            		sp =  new ServiceProperty();
	            		sp.name = name;
	            		services.put(name, sp);
	            	}
	            	if("interface".equalsIgnoreCase(ks[2])){
	            		sp.interfaceClass = pros.getProperty(key);
	            	}else if("implements".equalsIgnoreCase(ks[2])){
	            		sp.implementsClass = pros.getProperty(key);
	            	}else if("address".equalsIgnoreCase(ks[2])){
	            		sp.address = pros.getProperty(key);
	            	}
	            }
            }
			outter : for (ServiceProperty sp : services.values()) {
	            Class<?> intClass = Class.forName(sp.interfaceClass);
	            Class<?> impClass = Class.forName(sp.implementsClass);
	            if(!intClass.isInterface()){
	            	throw new RuntimeException("Service which named : " + sp.name + " the interface setting is error.");
	            }
	            for (Class<?> clazz : impClass.getInterfaces()) {
	                if(clazz.equals(intClass)){
	    	            String address = sp.address;
	    	            Object imp = impClass.newInstance();
	    	            System.out.println("Publish service : " + sp.name);
	    	            System.out.println("Service class : " + sp.implementsClass);
	    	            System.out.println("Service address : " + sp.address);
	    	            Object it = intClass.cast(imp);
	    	            Endpoint.publish(address, it);
	                	continue outter;
	                }
                }
            }
		}
		
	}
	
	static class ServiceProperty{
		private String name;
		private String interfaceClass;
		private String implementsClass;
		private String address;
	}
	
}




要发布也很简单,在同目录下的service.properties中配置就行:

service.userService.interface=com.wuningsi.ms.ws.UserService
service.userService.implements=com.wuningsi.ms.ws.UserServiceImpl
service.userService.address=http://ws.ms.wuningsi.com/user



很简单的东西,就当玩玩了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: