???? XFire是新一代的Java Web服务引擎,XFire使得在JavaEE应用中发布Web服务变得轻而易举。和其他Web服务引擎相比,XFire的配置非常简单,可以非常容易地和Spring集成。
??? 最近一项目,需要用到webservice,刚开始,使用xfire,甚是方便,特别是客户端代码,干净整洁,但是xfire用了jdom1.0.jar,而我们项目是基于原有的一个平台上面开发的子系统,用的是jdom.jar,较低版本,可恶,无奈之下,由于项目交期问题,只好换rmi。
rmi很简单,
rmi服务器:
public class BI RmiServiceContextListener? implements ServletContextListener{
??? public void contextInitialized(ServletContextEvent arg0) {
?????? try {
?????????? BIDataWebService obj = new BIDataImp() ;
?????????? LocateRegistry.createRegistry(1299);
?????????? Naming.rebind("rmi://localhost:1299/BIDataWebService", obj);
??????????? System.out.println("RMI Server ready");
?????? } catch (Exception e) {
?????????? System.err.println("RmiServiceContextListener err: " + e.getMessage());
?????????? e.printStackTrace();
?????? }
??? }
}
?
rmi客户端:
String rmiRoot = "rmi://"+rb.getRestaurantService()+":1299/BIDataWebService";
BIDataWebService rbws=(BIDataWebService)Naming.lookup(rmiRoot);
?
?
?? 系统发布使用了,对之前的xrife运用不甘心就此放过,觉得包冲突的问题可以解决,继续研究了下,想到类加载器,是否可以自定义一加载器,专门加载xfire的相关包和使用到的jdom1.0.jar,由于类加载器的隔离原因,不会影响平台其他系统的使用,一试,结果可行,问题就是在客户端在加载xfire的时候到必须使用反射调用,要不然会有ClassCastException,原因是自定义的加载器会和tomcat的加载器同时加载同一个class。
大体代码如下:
自定义的classloader: 撇开了双亲委派模式,优先用本加载器加载xfire相关库类,最终能使用上jdom-1.0.jar. 还要一大推的反射调用。 public class XfireClassLoader extends URLClassLoader{ ??? protected ClassLoader parent = null; ??? public XfireClassLoader(URL[] urls,ClassLoader classLoader) { ?????? super(urls); ?????? this.parent = classLoader; ??? } ??? ??? @Override ??? public final synchronized Class<?> loadClass(String name,boolean r) throws ClassNotFoundException { ?????? Class c = super.findLoadedClass(name); ?????? if(c==null){ ?????????? try{ ????????????? c = super.findClass(name);//调用URLClassLoader的findClass ?????????? }catch(ClassNotFoundException e){ ????????????? c = parent.loadClass(name); ?????????? } ?????? } ?????? return c; ??? } } ?
?
Xfire客户端反射调用:
public BIDataWebService getBIDataWebService() throws Exception{
?????? Class c = XfireClassLoader.loadClass("org.codehaus.xfire.service.binding.ObjectServiceFactory",true);
?????? Object objectServiceFactory = c.newInstance();
?????? Method method = c.getMethod("create",? Class.class);
??????
?????? Object srvcModel = method.invoke(objectServiceFactory, RestaurantBIData.class);
??????
?????? Class xfireFactoryClass = XfireClassLoader.loadClass("org.codehaus.xfire.XFireFactory",true);
?????? Method newInstanceMethod = xfireFactoryClass.getMethod("newInstance");
?????? Method getXFireMethod = xfireFactoryClass.getMethod("getXFire");
?????? Object XFireFactory = newInstanceMethod.invoke(null, null);
??????
?????? Object o = getXFireMethod.invoke(XFireFactory, null);
??????
?????? Class XFireProxyFactoryClass = XfireClassLoader.loadClass("org.codehaus.xfire.client.XFireProxyFactory",true);
?????? Class XFire = XfireClassLoader.loadClass("org.codehaus.xfire.XFire",true);
?????? Constructor constructor = XFireProxyFactoryClass.getConstructor(XFire);
?????? Object XFireProxyFactory = constructor.newInstance(o);
?????? String url = "http://localhost:8888/bi/services/BIDataWebService";
?????? Method createMethod = XFireProxyFactoryClass.getMethod("create", srvcModel.getClass(),String.class);
?????? BIDataWebService srvc = (BIDataWebService )createMethod.invoke(XFireProxyFactory,srvcModel, url);
?????? return srvc;
??? }
?
?
?