View Javadoc

    1   package org.codehaus.xfire.transport;
    2
    3   import java.util.Collection;
    4   import java.util.HashMap;
    5   import java.util.Iterator;
    6   import java.util.Map;
    7
    8   import org.apache.commons.logging.Log;
    9   import org.apache.commons.logging.LogFactory;
    10  import org.codehaus.xfire.service.Service;
    11  import org.codehaus.xfire.service.ServiceRegistry;
    12  import org.codehaus.xfire.service.event.RegistrationEvent;
    13  import org.codehaus.xfire.service.event.RegistrationEventListener;
    14  import org.codehaus.xfire.soap.SoapTransport;
    15  import org.codehaus.xfire.transport.dead.DeadLetterTransport;
    16  import org.codehaus.xfire.transport.local.LocalTransport;
    17
    18  /***
    19   * The default <code>TransportManager</code> implementation.
    20   *
    21   * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
    22   */
    23  public class DefaultTransportManager
    24          implements TransportManager, RegistrationEventListener
    25  {
    26      private static final Log log = LogFactory.getLog(DefaultTransportManager.class);
    27
    28      private Map services = new HashMap();
    29      private Map transports = new HashMap();
    30      private Map transportUris = new HashMap();
    31
    32      private ServiceRegistry serviceRegistry;
    33
    34      public DefaultTransportManager()
    35      {
    36      }
    37
    38      /***
    39       * Creates a DefaultTransportManager. A LocalTransport and DeadLetterTransport
    40       * are registered automatically.
    41       * @param xfire
    42       */
    43      public DefaultTransportManager(ServiceRegistry registry)
    44      {
    45          this.serviceRegistry = registry;
    46
    47          initialize();
    48      }
    49
    50      public ServiceRegistry getServiceRegistry()
    51      {
    52          return serviceRegistry;
    53      }
    54
    55      public void setServiceRegistry(ServiceRegistry serviceRegistry)
    56      {
    57          this.serviceRegistry = serviceRegistry;
    58      }
    59
    60      /***
    61       * Initializes transports for each service.  This also registers a LocalTransport and
    62       * DeadLetterTransport.
    63       */
    64      public void initialize()
    65      {
    66          initializeTransports();
    67
    68          register(SoapTransport.createSoapTransport(new LocalTransport()));
    69          register(SoapTransport.createSoapTransport(new DeadLetterTransport()));
    70      }
    71
    72      /***
    73       * @param registry
    74       */
    75      protected void initializeTransports()
    76      {
    77          for (Iterator itr = serviceRegistry.getServices().iterator(); itr.hasNext();)
    78          {
    79              Service endpoint = (Service) itr.next();
    80              enableAll(endpoint.getName());
    81          }
    82          serviceRegistry.addRegistrationEventListener(this);
    83      }
    84
    85      public void register(Transport transport)
    86      {
    87          transports.put(transport.getName(), transport);
    88
    89          String[] schemes = transport.getKnownUriSchemes();
    90          for (int i = 0; i < schemes.length; i++)
    91          {
    92              transportUris.put(schemes[i], transport);
    93          }
    94
    95          for (Iterator itr = services.values().iterator(); itr.hasNext();)
    96          {
    97              Map serviceTransports = (Map) itr.next();
    98              serviceTransports.put(transport.getName(), transport);
    99          }
    100
    101         log.debug("Registered transport " + transport.getName());
    102     }
    103
    104     public void unregister(Transport transport)
    105     {
    106         transports.remove(transport);
    107
    108         for (Iterator itr = services.values().iterator(); itr.hasNext();)
    109         {
    110             Map serviceTransports = (Map) itr.next();
    111             if (serviceTransports != null)
    112             {
    113                 serviceTransports.remove(transport);
    114             }
    115         }
    116     }
    117
    118     /***
    119      * @see org.codehaus.xfire.transport.TransportManager#getTransports(java.lang.String)
    120      */
    121     public Transport getTransport(String name)
    122     {
    123         return (Transport) transports.get(name);
    124     }
    125
    126     public void enable(String transport, String serviceName)
    127     {
    128         Map serviceTransports = (Map) services.get(serviceName);
    129         if (serviceTransports == null)
    130         {
    131             serviceTransports = new HashMap();
    132             services.put(serviceName, serviceTransports);
    133         }
    134
    135         serviceTransports.put(transport, getTransport(transport));
    136     }
    137
    138     public void disable(String transport, String serviceName)
    139     {
    140         Map serviceTransports = (Map) services.get(serviceName);
    141         if (serviceTransports == null)
    142         {
    143             return;
    144         }
    145
    146         serviceTransports.remove(transport);
    147     }
    148
    149     /***
    150      * @param service
    151      * @return
    152      * @see org.codehaus.xfire.transport.TransportManager#getTransports(java.lang.String)
    153      */
    154     public Collection getTransports(String service)
    155     {
    156         Map transports = ((Map) services.get(service));
    157
    158         if (transports != null)
    159             return transports.values();
    160         else
    161             return null;
    162     }
    163
    164     public Collection getTransports()
    165     {
    166         return transports.values();
    167     }
    168
    169     /***
    170      * @param serviceName
    171      */
    172     public void enableAll(String serviceName)
    173     {
    174         Map serviceTransports = (Map) services.get(serviceName);
    175         if (serviceTransports == null)
    176         {
    177             serviceTransports = new HashMap();
    178             services.put(serviceName, serviceTransports);
    179         }
    180
    181         for (Iterator itr = transports.values().iterator(); itr.hasNext();)
    182         {
    183             Transport t = (Transport) itr.next();
    184
    185             serviceTransports.put(t.getName(), t);
    186         }
    187     }
    188
    189     /***
    190      * @param serviceName
    191      */
    192     public void disableAll(String serviceName)
    193     {
    194         Map serviceTransports = (Map) services.get(serviceName);
    195         if (serviceTransports == null)
    196         {
    197             return;
    198         }
    199
    200         for (Iterator itr = transports.keySet().iterator(); itr.hasNext();)
    201         {
    202             String name = (String) itr.next();
    203
    204             serviceTransports.remove(name);
    205         }
    206     }
    207
    208     /***
    209      * @param service
    210      * @param transportName
    211      * @return
    212      */
    213     public boolean isEnabled(String service, String transportName)
    214     {
    215         Map serviceTransports = (Map) services.get(service);
    216         if (serviceTransports == null)
    217         {
    218             return false;
    219         }
    220
    221         if (serviceTransports.containsKey(transportName))
    222         {
    223             return true;
    224         }
    225
    226         return false;
    227     }
    228
    229     /***
    230      * Notifies this <code>RegistrationEventListener</code> that the <code>ServiceEndpointRegistry</code> has registered
    231      * an endpoint.
    232      *
    233      * @param event an event object describing the source of the event
    234      */
    235     public void endpointRegistered(RegistrationEvent event)
    236     {
    237         enableAll(event.getEndpoint().getName());
    238     }
    239
    240     /***
    241      * Notifies this <code>RegistrationEventListener</code> that the <code>ServiceEndpointRegistry</code> has
    242      * deregistered an endpoint.
    243      *
    244      * @param event an event object describing the source of the event
    245      */
    246     public void endpointUnregistered(RegistrationEvent event)
    247     {
    248         disableAll(event.getEndpoint().getName());
    249     }
    250
    251     public Transport getTransportForUri(String uri)
    252     {
    253         for (Iterator itr = transportUris.entrySet().iterator(); itr.hasNext();)
    254         {
    255             Map.Entry entry = (Map.Entry) itr.next();
    256
    257             if (uri.startsWith((String) entry.getKey()))
    258             {
    259                 return (Transport) entry.getValue();
    260             }
    261         }
    262
    263         return null;
    264     }
    265 }