View Javadoc

    1   package org.codehaus.xfire.service.binding;
    2
    3   import java.util.Iterator;
    4   import java.util.List;
    5
    6   import javax.xml.stream.XMLStreamException;
    7   import javax.xml.stream.XMLStreamReader;
    8
    9   import org.apache.commons.logging.Log;
    10  import org.apache.commons.logging.LogFactory;
    11  import org.codehaus.xfire.MessageContext;
    12  import org.codehaus.xfire.XFireRuntimeException;
    13  import org.codehaus.xfire.exchange.MessageExchange;
    14  import org.codehaus.xfire.exchange.OutMessage;
    15  import org.codehaus.xfire.fault.XFireFault;
    16  import org.codehaus.xfire.handler.AbstractHandler;
    17  import org.codehaus.xfire.handler.Phase;
    18  import org.codehaus.xfire.service.MessageHeaderInfo;
    19  import org.codehaus.xfire.service.OperationInfo;
    20
    21  public abstract class AbstractBinding
    22      extends AbstractHandler
    23      implements ObjectBinding, Cloneable
    24  {
    25      private static final Log logger = LogFactory.getLog(AbstractBinding.class.getName());
    26
    27      public static final String OPERATION_KEY = "xfire.operation";
    28
    29      public static final String RESPONSE_VALUE = "xfire.java.response";
    30
    31      public static final String RESPONSE_PIPE = "xfire.java.responsePipe";
    32
    33      private String style;
    34      private String use;
    35      private Invoker invoker;
    36      private BindingProvider bindingProvider;
    37      private boolean clientModeOn = false;
    38
    39      public String getPhase()
    40      {
    41          return Phase.SERVICE;
    42      }
    43
    44      public void setOperation(OperationInfo operation, MessageContext context)
    45      {
    46          MessageExchange exchange = context.createMessageExchange(operation);
    47
    48          context.setExchange(exchange);
    49      }
    50
    51      public void invoke(final MessageContext context)
    52          throws Exception
    53      {
    54          try
    55          {
    56              // Read in the parameters...
    57              final List params = (List) context.getInMessage().getBody();
    58
    59              // Don't read the operation in until after reading. Otherwise
    60              // it won't work for document style services.
    61              final OperationInfo operation = context.getExchange().getOperation();
    62
    63              // read in the headers
    64              final List headerInfos = operation.getInputMessage().getMessageHeaders();
    65              for (Iterator itr = headerInfos.iterator(); itr.hasNext();)
    66              {
    67                  MessageHeaderInfo header = (MessageHeaderInfo) itr.next();
    68                  BindingProvider provider = context.getService().getBinding().getBindingProvider();
    69                  params.add(header.getIndex(), provider.readHeader(header, context));
    70              }
    71
    72              final Invoker invoker = getInvoker();
    73
    74              // invoke the service method...
    75              if (!operation.isAsync())
    76              {
    77                  sendMessage(context, params, operation, invoker);
    78              }
    79              else
    80              {
    81                  Runnable runnable = new Runnable()
    82                  {
    83                      public void run()
    84                      {
    85                          try
    86                          {
    87                              sendMessage(context, params, operation, invoker);
    88                          }
    89                          catch (Exception e)
    90                          {
    91                              XFireFault fault = XFireFault.createFault(e);
    92
    93                              context.getInPipeline().handleFault(fault, context);
    94                          }
    95                      }
    96                  };
    97
    98                  Thread opthread = new Thread(runnable);
    99                  opthread.start();
    100             }
    101         }
    102         catch (XFireRuntimeException e)
    103         {
    104             logger.warn("Error invoking service.", e);
    105             throw new XFireFault("Error invoking service.", e, XFireFault.SENDER);
    106         }
    107     }
    108
    109     protected void sendMessage(final MessageContext context,
    110                                final List params,
    111                                final OperationInfo operation,
    112                                final Invoker invoker)
    113         throws Exception
    114     {
    115         final Object value = invoker.invoke(operation.getMethod(),
    116                                             params.toArray(),
    117                                             context);
    118
    119         if (context.getExchange().hasOutMessage())
    120         {
    121             OutMessage outMsg = (OutMessage) context.getExchange().getOutMessage();
    122             outMsg.setBody(new Object[] {value});
    123             outMsg.setSerializer(context.getService().getBinding());
    124             context.getOutPipeline().invoke(context);
    125         }
    126     }
    127
    128     protected void nextEvent(XMLStreamReader dr)
    129     {
    130         try
    131         {
    132             dr.next();
    133         }
    134         catch (XMLStreamException e)
    135         {
    136             throw new XFireRuntimeException("Couldn't parse stream.", e);
    137         }
    138     }
    139
    140     public Invoker getInvoker()
    141     {
    142         return invoker;
    143     }
    144
    145     public void setInvoker(Invoker invoker)
    146     {
    147         this.invoker = invoker;
    148     }
    149
    150     public String getStyle()
    151     {
    152         return style;
    153     }
    154
    155     protected void setStyle(String style)
    156     {
    157         this.style = style;
    158     }
    159
    160     public String getUse()
    161     {
    162         return use;
    163     }
    164
    165     protected void setUse(String use)
    166     {
    167         this.use = use;
    168     }
    169
    170     public BindingProvider getBindingProvider()
    171     {
    172         return bindingProvider;
    173     }
    174
    175     public void setBindingProvider(BindingProvider bindingProvider)
    176     {
    177         this.bindingProvider = bindingProvider;
    178     }
    179
    180     public boolean isClientModeOn()
    181     {
    182         return clientModeOn;
    183     }
    184
    185     public void setClientModeOn(boolean clientModeOn)
    186     {
    187         this.clientModeOn = clientModeOn;
    188     }
    189
    190     public abstract Object clone();
    191 }