1 package org.codehaus.xfire.client;
2
3 import java.lang.reflect.Proxy;
4 import java.net.MalformedURLException;
5
6 import org.codehaus.xfire.XFire;
7 import org.codehaus.xfire.service.Service;
8 import org.codehaus.xfire.transport.Transport;
9
10 /***
11 * Factory for creating XFire SOAP client stubs. The returned stub will call the remote object for all methods.
12 * <pre>
13 * String url = "http://localhost:8080/services/Echo");
14 * Echo echo = (Echo) factory.create(HelloHome.class, url);
15 * </pre>
16 * After creation, the stub can be like a regular Java class. Because it makes remote calls, it can throw more
17 * exceptions than a Java class. In particular, it may throw protocol exceptions, and <code>XFireFaults</code>
18 *
19 * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
20 * @see org.codehaus.xfire.fault.XFireFault
21 */
22 public class XFireProxyFactory
23 {
24 private XFire xfire;
25
26 public XFireProxyFactory()
27 {
28 }
29
30 public XFireProxyFactory(XFire xfire)
31 {
32 this.xfire = xfire;
33 }
34
35 /***
36 * Creates a new proxy with the specified URL. The returned object is a proxy with the interface specified by the
37 * given service interface.
38 * <pre>
39 * String url = "http://localhost:8080/services/Echo");
40 * Echo echo = (Echo) factory.create(myService, url);
41 * </pre>
42 *
43 * @param service the service to create a client for.
44 * @param url the URL where the client object is located.
45 * @return a proxy to the object with the specified interface.
46 */
47 public Object create(Service service, String url)
48 throws MalformedURLException
49 {
50 Transport transport = xfire.getTransportManager().getTransportForUri(url);
51
52 return create(transport, service, url);
53 }
54
55 /***
56 * Creates a new proxy with the specified URL. The returned object is a proxy with the interface specified by the
57 * given service interface.
58 * <pre>
59 * String url = "http://localhost:8080/services/Echo");
60 * Echo echo = (Echo) factory.create(transport, myService, url);
61 * </pre>
62 *
63 * @param transport The transport to use.
64 * @param serviceInterface the service to create a client for.
65 * @param url the URL where the client object is located.
66 * @return a proxy to the object with the specified interface.
67 */
68 public Object create(Transport transport, Service service, String url)
69 throws MalformedURLException
70 {
71 Client client = new Client(transport, service, url);
72 client.setXFire(xfire);
73
74 XFireProxy handler = new XFireProxy(client);
75 Class serviceClass = service.getServiceInfo().getServiceClass();
76
77 return Proxy.newProxyInstance(serviceClass.getClassLoader(),
78 new Class[]{serviceClass},
79 handler);
80 }
81 }