加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-16 23:15:11 所属栏目:安全 来源:网络整理
导读:这几天研究WebService,测试启什么server很烦,直接写main又嫌写烦,干脆写了个简单的发布工具,通过load properties来实现publish。 上代码: public class Publisher {public static void main(String[] args) throws IOException,ClassNotFoundException,

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

?

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读