KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jsr181 > xfire > JbiProxy


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
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 JavaDoc;
33
34 import com.ibm.wsdl.Constants;
35
36 public class JbiProxy {
37     
38     protected XFire xfire;
39     protected ComponentContext context;
40     protected QName JavaDoc interfaceName;
41     protected QName JavaDoc serviceName;
42     protected String JavaDoc endpointName;
43     protected Object JavaDoc proxy;
44     protected Class JavaDoc serviceClass;
45     protected Definition description;
46     protected ServiceEndpoint endpoint;
47     
48     public static Object JavaDoc create(XFire xfire,
49                                 ComponentContext context,
50                                 QName JavaDoc interfaceName,
51                                 QName JavaDoc serviceName,
52                                 String JavaDoc endpointName,
53                                 Class JavaDoc serviceClass) throws Exception JavaDoc {
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 JavaDoc serviceClass,
61                     Definition description) throws Exception JavaDoc {
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 JavaDoc serviceClass,
71                     QName JavaDoc interfaceName,
72                     QName JavaDoc serviceName,
73                     String JavaDoc endpointName) throws Exception JavaDoc {
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 JavaDoc getProxy() throws Exception JavaDoc {
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 JavaDoc {
103         if (this.description == null) {
104             ServiceEndpoint[] endpoints = getEndpoints();
105             if (endpoints == null || endpoints.length == 0) {
106                 throw new IllegalStateException JavaDoc("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 JavaDoc("No suitable endpoint found");
111             }
112             if (serviceName != null && endpointName != null) {
113                 this.endpoint = endpoint;
114             }
115             Document JavaDoc 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 JavaDoc("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 JavaDoc {
155             super(xfire.getTransportManager().getTransport(JbiTransport.JBI_BINDING),
156                   service,
157                   null);
158             setXFire(xfire);
159         }
160         
161     }
162 }
163
Popular Tags