View Javadoc

    1   package org.codehaus.xfire.spring;
    2
    3   import java.util.List;
    4
    5   import org.apache.commons.logging.Log;
    6   import org.apache.commons.logging.LogFactory;
    7   import org.codehaus.xfire.XFire;
    8   import org.codehaus.xfire.service.Service;
    9   import org.codehaus.xfire.service.ServiceFactory;
    10  import org.codehaus.xfire.service.binding.AbstractBinding;
    11  import org.codehaus.xfire.service.binding.BeanInvoker;
    12  import org.springframework.aop.framework.ProxyFactory;
    13  import org.springframework.beans.factory.BeanNameAware;
    14  import org.springframework.beans.factory.InitializingBean;
    15
    16
    17  /***
    18   * A convenience bean which creates a Service from a ServiceFactory instance. Alternatively, the
    19   * Jsr181BeanPostProcessor may be used.
    20   *
    21   * @see org.codehaus.xfire.service.Service
    22   * @see org.codehaus.xfire.spring.Jsr181BeanPostProcessor
    23   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse </a>
    24   * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
    25   */
    26  public class ServiceBean
    27      implements InitializingBean, BeanNameAware
    28  {
    29      private final static Log logger = LogFactory.getLog(ServiceBean.class);
    30
    31      private Service xfireService;
    32      private ServiceFactory serviceFactory;
    33      private XFire xFire;
    34      private String name;
    35      private String namespace;
    36      private String beanName;
    37      private Class serviceInterface;
    38      private Object service;
    39      private List inHandlers;
    40      private List outHandlers;
    41      private List faultHandlers;
    42
    43      public void afterPropertiesSet()
    44              throws Exception
    45      {
    46          // Use specific name if given, else fall back to bean name.
    47          String theName = (this.name != null ? this.name : this.beanName);
    48          if (theName != null && theName.startsWith("/"))
    49          {
    50              theName = theName.substring(1);
    51          }
    52
    53          Class intf = getServiceInterface();
    54          if (intf == null) intf = getService().getClass();
    55
    56          xfireService = serviceFactory.create(intf,
    57                                           theName,
    58                                           namespace,
    59                                           null);
    60
    61          AbstractBinding binding = (AbstractBinding) xfireService.getBinding();
    62          if (logger.isInfoEnabled())
    63          {
    64              logger.info("Exposing SOAP v." + xfireService.getSoapVersion().getVersion() +
    65                          " service " + xfireService.getName() + " as " + binding.getStyle() +
    66                          "/" + binding.getUse());
    67          }
    68
    69          xFire.getServiceRegistry().register(xfireService);
    70
    71          if (serviceInterface != null)
    72              binding.setInvoker(new BeanInvoker(getProxyForService()));
    73          else
    74              binding.setInvoker(new BeanInvoker(getService()));
    75
    76          // set up in handlers
    77          if (xfireService.getInHandlers() == null)
    78              xfireService.setInHandlers(getInHandlers());
    79          else if (getInHandlers() != null)
    80              xfireService.getInHandlers().addAll(getInHandlers());
    81
    82          // set up out handlers
    83          if (xfireService.getOutHandlers() == null)
    84              xfireService.setOutHandlers(getOutHandlers());
    85          else if (getOutHandlers() != null)
    86              xfireService.getOutHandlers().addAll(getOutHandlers());
    87
    88          // set up fault handlers
    89          if (xfireService.getFaultHandlers() == null)
    90              xfireService.setFaultHandlers(getFaultHandlers());
    91          else if (getFaultHandlers() != null)
    92              xfireService.getFaultHandlers().addAll(getFaultHandlers());
    93      }
    94
    95      protected Object getProxyForService()
    96      {
    97          ProxyFactory proxyFactory = new ProxyFactory();
    98          proxyFactory.addInterface(getServiceInterface());
    99
    100         proxyFactory.setTarget(getService());
    101         return proxyFactory.getProxy();
    102     }
    103
    104     public Service getXFireService()
    105     {
    106         return xfireService;
    107     }
    108
    109     public Object getService()
    110     {
    111         return service;
    112     }
    113
    114     public void setService(Object service)
    115     {
    116         this.service = service;
    117     }
    118
    119     public Class getServiceInterface()
    120     {
    121         return serviceInterface;
    122     }
    123
    124     public void setServiceInterface(Class serviceInterface)
    125     {
    126         this.serviceInterface = serviceInterface;
    127     }
    128
    129     public void setServiceFactory(ServiceFactory serviceFactory)
    130     {
    131         this.serviceFactory = serviceFactory;
    132     }
    133
    134     public void setXfire(XFire xFire)
    135     {
    136         this.xFire = xFire;
    137     }
    138
    139     public XFire getXfire()
    140     {
    141         return xFire;
    142     }
    143
    144     /***
    145      * Sets the service name. Default is the bean name of this exporter.
    146      */
    147     public void setName(String name)
    148     {
    149         this.name = name;
    150     }
    151
    152     /***
    153      * Sets the service default namespace. Default is a namespace based on the package of the {@link
    154      * #getServiceInterface() service interface}.
    155      */
    156     public void setNamespace(String namespace)
    157     {
    158         this.namespace = namespace;
    159     }
    160
    161     public void setBeanName(String beanName)
    162     {
    163         this.beanName = beanName;
    164     }
    165
    166     public List getFaultHandlers()
    167     {
    168         return faultHandlers;
    169     }
    170
    171     public void setFaultHandlers(List faultHandlers)
    172     {
    173         this.faultHandlers = faultHandlers;
    174     }
    175
    176     public List getInHandlers()
    177     {
    178         return inHandlers;
    179     }
    180
    181     public void setInHandlers(List inHandlers)
    182     {
    183         this.inHandlers = inHandlers;
    184     }
    185
    186     public List getOutHandlers()
    187     {
    188         return outHandlers;
    189     }
    190
    191     public void setOutHandlers(List outHandlers)
    192     {
    193         this.outHandlers = outHandlers;
    194     }
    195 }