實現簡單的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

 

很簡單的東西,就當玩玩了。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章