View Javadoc

1   package org.codehaus.xfire.service;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.HashMap;
7   import java.util.Iterator;
8   import java.util.List;
9   import java.util.Map;
10  
11  import org.codehaus.xfire.service.event.RegistrationEvent;
12  import org.codehaus.xfire.service.event.RegistrationEventListener;
13  
14  /***
15   * @author <a href="mailto:poutsma@mac.com">Arjen Poutsma</a>
16   */
17  public class DefaultServiceRegistry
18          implements ServiceRegistry
19  {
20      // maps string names to service endpoints
21      private Map services = new HashMap();
22      private List eventListeners = new ArrayList();
23  
24      /***
25       * Returns the <code>ServiceEndpoint</code> with the given qualified name, if found. Returns <code>null</code> if
26       * not found.
27       *
28       * @param name the service name.
29       * @return the service endpoint, or <code>null</code> if not found.
30       */
31      public Service getService(String name)
32      {
33          return (Service) services.get(name);
34      }
35  
36      /***
37       * Registers a given <code>ServiceEndpoint</code> with this registry.
38       *
39       * @param endpoint the endpoint.
40       */
41      public void register(Service endpoint)
42      {
43          services.put(endpoint.getName(), endpoint);
44  
45          for (Iterator iterator = eventListeners.iterator(); iterator.hasNext();)
46          {
47              RegistrationEventListener listener = (RegistrationEventListener) iterator.next();
48              RegistrationEvent event = new RegistrationEvent(this, endpoint);
49              listener.endpointRegistered(event);
50          }
51      }
52  
53      /***
54       * Unregisters the service with the given qualified name, if found.
55       *
56       * @param name the service name.
57       */
58      public void unregister(String name)
59      {
60          Service endpoint = getService(name);
61  
62          for (Iterator iterator = eventListeners.iterator(); iterator.hasNext();)
63          {
64              RegistrationEventListener listener = (RegistrationEventListener) iterator.next();
65              RegistrationEvent event = new RegistrationEvent(this, endpoint);
66              listener.endpointUnregistered(event);
67          }
68  
69          services.remove(name);
70      }
71  
72      /***
73       * Indicates whether this registry has a service with the given name.
74       *
75       * @param name the service name.
76       * @return <code>true</code> if this registry has a service with the given name; <code>false</code> otherwise.
77       */
78      public boolean hasService(String name)
79      {
80          return services.containsKey(name);
81      }
82  
83      /***
84       * Returns all <code>ServiceEndpoint</code> registered to this registry.
85       *
86       * @return all service endpoints.
87       */
88      public Collection getServices()
89      {
90          return Collections.unmodifiableCollection(services.values());
91      }
92  
93      /***
94       * Add a listener for registration events.
95       *
96       * @param listener the listener.
97       */
98      public void addRegistrationEventListener(RegistrationEventListener listener)
99      {
100         eventListeners.add(listener);
101     }
102 
103     /***
104      * Remove a listener for registration events.
105      *
106      * @param listener the listener.
107      */
108     public void removeRegistrationEventListener(RegistrationEventListener listener)
109     {
110         eventListeners.remove(listener);
111     }
112 }