微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

这几天研究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

 

很简单的东西,就当玩玩了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐