View Javadoc

1   package org.controlhaus.xfire.client;
2   
3   import java.io.IOException;
4   import java.lang.reflect.Method;
5   
6   import javax.xml.stream.XMLStreamException;
7   import javax.xml.stream.XMLStreamReader;
8   import javax.xml.stream.XMLStreamWriter;
9   
10  import org.apache.beehive.controls.api.context.Context;
11  import org.apache.beehive.controls.api.context.ResourceContext;
12  import org.apache.beehive.controls.api.context.ControlBeanContext;
13  import org.apache.beehive.controls.api.events.EventHandler;
14  import org.apache.beehive.controls.api.bean.ControlImplementation;
15  import org.apache.beehive.controls.api.bean.Extensible;
16  import org.apache.xmlbeans.XmlObject;
17  import org.apache.xmlbeans.XmlOptions;
18  import org.codehaus.xfire.client.ClientHandler;
19  import org.codehaus.xfire.client.http.SoapHttpClient;
20  import org.codehaus.xfire.fault.XFireFault;
21  import org.codehaus.xfire.xmlbeans.client.XMLBeansClientHandler;
22  import org.controlhaus.xfire.client.XFireClientControl.ServiceUrl;
23  
24  /***
25   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
26   * @since Nov 5, 2004
27   */
28  @ControlImplementation
29  public class XFireClientControlImpl
30      implements XFireClientControl, Extensible
31  {
32      @Context ControlBeanContext context;
33      @Context ResourceContext resourceContext;
34      
35      private String encoding;
36      private String serviceUrl;
37      
38      private XmlOptions options;
39      private SoapHttpClient client;
40  
41      @EventHandler (field="resourceContext", 
42                     eventSet=ResourceContext.ResourceEvents.class, 
43                     eventName="onAcquire")
44      public void onAcquire()
45      {
46          options = new XmlOptions();
47          options.setCharacterEncoding( getEncoding() );
48          
49          ServiceUrl destProp = 
50              (ServiceUrl) context.getControlPropertySet(XFireClientControl.ServiceUrl.class);
51          if ( destProp != null )
52          {
53              serviceUrl = destProp.value();
54          }
55          
56          Encoding encProp = 
57              (Encoding) context.getControlPropertySet(XFireClientControl.Encoding.class);
58          if ( encProp != null )
59          {
60              encoding = encProp.value();
61          }
62      }
63  
64      public XmlObject[] invoke(XmlObject[] request) 
65          throws IOException, XFireFault
66      {
67          XMLBeansClientHandler handler = new XMLBeansClientHandler(options);
68          handler.setRequest(request);
69          
70          SoapHttpClient client = new SoapHttpClient(handler, getHeaderHandler(), getServiceUrl());
71          client.invoke();
72          
73          return handler.getResponse();
74      }
75      
76      public String getServiceUrl()
77      {
78          return serviceUrl;
79      }
80      
81      public String getEncoding()
82      {
83          return encoding;
84      }
85      
86      /***
87       * Gets the HeaderHandler.  Override this or fill this in
88       * to provide your own HeaderHandler.
89       * @return null by default.
90       */
91      public ClientHandler getHeaderHandler()
92      {
93          return null;
94      }
95      
96      public Object invoke(Method m, Object [] args) throws Throwable
97      {
98          if ( getServiceUrl() == null )
99          {
100             ServiceUrl a = m.getDeclaringClass().getAnnotation(ServiceUrl.class);
101             serviceUrl = a.value();            
102         }
103 
104         XmlObject[] arr = new XmlObject[args.length];
105         for ( int i = 0; i < args.length; i++ )
106         {
107             arr[i] = (XmlObject) args[i];
108         }
109         
110         if ( m.getReturnType().isArray() )
111             return invoke( (XmlObject[]) arr );
112         else
113             return invoke( (XmlObject[]) arr )[0];
114     }
115 }