1 17 package org.apache.servicemix.jsr181.xfire; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.component.ComponentContext; 21 import javax.jbi.servicedesc.ServiceEndpoint; 22 import javax.wsdl.Definition; 23 import javax.wsdl.factory.WSDLFactory; 24 import javax.wsdl.xml.WSDLReader; 25 import javax.xml.namespace.QName ; 26 27 import org.codehaus.xfire.XFire; 28 import org.codehaus.xfire.client.Client; 29 import org.codehaus.xfire.client.XFireProxyFactory; 30 import org.codehaus.xfire.service.Service; 31 import org.codehaus.xfire.service.ServiceFactory; 32 import org.w3c.dom.Document ; 33 34 import com.ibm.wsdl.Constants; 35 36 public class JbiProxy { 37 38 protected XFire xfire; 39 protected ComponentContext context; 40 protected QName interfaceName; 41 protected QName serviceName; 42 protected String endpointName; 43 protected Object proxy; 44 protected Class serviceClass; 45 protected Definition description; 46 protected ServiceEndpoint endpoint; 47 48 public static Object create(XFire xfire, 49 ComponentContext context, 50 QName interfaceName, 51 QName serviceName, 52 String endpointName, 53 Class serviceClass) throws Exception { 54 JbiProxy p = new JbiProxy(xfire, context, serviceClass, interfaceName, serviceName, endpointName); 55 return p.getProxy(); 56 } 57 58 public JbiProxy(XFire xfire, 59 ComponentContext context, 60 Class serviceClass, 61 Definition description) throws Exception { 62 this.xfire = xfire; 63 this.context = context; 64 this.serviceClass = serviceClass; 65 this.description = description; 66 } 67 68 public JbiProxy(XFire xfire, 69 ComponentContext context, 70 Class serviceClass, 71 QName interfaceName, 72 QName serviceName, 73 String endpointName) throws Exception { 74 this.xfire = xfire; 75 this.context = context; 76 this.interfaceName = interfaceName; 77 this.serviceName = serviceName; 78 this.endpointName = endpointName; 79 this.serviceClass = serviceClass; 80 } 81 82 public Object getProxy() throws Exception { 83 if (proxy == null) { 84 ServiceFactory factory = ServiceFactoryHelper.findServiceFactory(xfire, serviceClass, null, null); 85 Service service = factory.create(serviceClass, null, getDescription(), null); 86 JBIClient client = new JBIClient(xfire, service); 87 if (interfaceName != null) { 88 client.getService().setProperty(JbiChannel.JBI_INTERFACE_NAME, interfaceName); 89 } 90 if (serviceName != null) { 91 client.getService().setProperty(JbiChannel.JBI_SERVICE_NAME, serviceName); 92 } 93 if (endpoint != null) { 94 client.getService().setProperty(JbiChannel.JBI_ENDPOINT, endpoint); 95 } 96 XFireProxyFactory xpf = new XFireProxyFactory(xfire); 97 proxy = xpf.create(client); 98 } 99 return proxy; 100 } 101 102 public Definition getDescription() throws Exception { 103 if (this.description == null) { 104 ServiceEndpoint[] endpoints = getEndpoints(); 105 if (endpoints == null || endpoints.length == 0) { 106 throw new IllegalStateException ("No endpoints found for interface " + interfaceName + ", serviceName " + serviceName + " and endpoint " + endpointName); 107 } 108 ServiceEndpoint endpoint = chooseEndpoint(endpoints); 109 if (endpoint == null) { 110 throw new IllegalStateException ("No suitable endpoint found"); 111 } 112 if (serviceName != null && endpointName != null) { 113 this.endpoint = endpoint; 114 } 115 Document doc = context.getEndpointDescriptor(endpoint); 116 WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); 117 reader.setFeature(Constants.FEATURE_VERBOSE, false); 118 this.description = reader.readWSDL(null, doc); 119 } 120 return this.description; 121 } 122 123 protected ServiceEndpoint[] getEndpoints() throws JBIException { 124 ServiceEndpoint[] endpoints; 125 if (endpointName != null && serviceName != null) { 126 ServiceEndpoint endpoint = context.getEndpoint(serviceName, endpointName); 127 if (endpoint == null) { 128 endpoints = new ServiceEndpoint[0]; 129 } else { 130 this.endpoint = endpoint; 131 endpoints = new ServiceEndpoint[] { endpoint }; 132 } 133 } else if (serviceName != null) { 134 endpoints = context.getEndpointsForService(serviceName); 135 } else if (interfaceName != null) { 136 endpoints = context.getEndpoints(interfaceName); 137 } else { 138 throw new IllegalStateException ("One of interfaceName or serviceName should be provided"); 139 } 140 return endpoints; 141 } 142 143 protected ServiceEndpoint chooseEndpoint(ServiceEndpoint[] endpoints) throws JBIException { 144 for (int i = 0; i < endpoints.length; i++) { 145 if (context.getEndpointDescriptor(endpoints[i]) != null) { 146 return endpoints[i]; 147 } 148 } 149 return null; 150 } 151 152 protected static class JBIClient extends Client { 153 154 public JBIClient(XFire xfire, Service service) throws Exception { 155 super(xfire.getTransportManager().getTransport(JbiTransport.JBI_BINDING), 156 service, 157 null); 158 setXFire(xfire); 159 } 160 161 } 162 } 163 | Popular Tags |