View Javadoc

1   /*
2    * Created on Jun 7, 2005
3    *
4    * Provides a default implementation of the core ServiceLocator interface.
5    */
6   package net.sf.tlc.core.impl;
7   
8   import java.util.Hashtable;
9   import java.util.Iterator;
10  import java.util.LinkedHashSet;
11  import java.util.Map;
12  import java.util.Set;
13  
14  import net.sf.tlc.core.ServiceLocator;
15  
16  /***
17   * Provides a default implementation of the core ServiceLocator interface.
18   * 
19   * @author aisrael
20   */
21  public class DefaultServiceLocator implements ServiceLocator {
22  
23      private final Map map = new Hashtable();
24  
25      /***
26       * (non-Javadoc)
27       * 
28       * @see net.sf.tlc.core.ServiceLocator#register(java.lang.String,
29       *      java.lang.Object)
30       */
31      public final void register(final String name, final Object o) {
32          if (null == name) {
33              throw new IllegalArgumentException("Cannot use a null value as key!");
34          }
35          if (null == o) {
36              throw new IllegalArgumentException("Cannot register a null object as a service!");
37          }
38          map.put(name, o);
39      }
40  
41      /***
42       * (non-Javadoc)
43       * 
44       * @see net.sf.tlc.core.ServiceLocator#register(java.lang.Class,
45       *      java.lang.Object)
46       */
47      public final void register(final Class c, final Object o) {
48          if (null == c) {
49              throw new IllegalArgumentException("Cannot use a null value as key!");
50          }
51          if (c.isAssignableFrom(o.getClass())) {
52              register(c.getName(), o);
53          } else {
54              throw new IllegalArgumentException(c.getName() + " is not assignable from "
55                      + o.getClass().getName());
56          }
57      }
58  
59      /***
60       * (non-Javadoc)
61       * 
62       * @see net.sf.tlc.core.ServiceLocator#register(java.lang.Object)
63       */
64      public final void register(final Object o) {
65          register(o.getClass().getName(), o);
66      }
67  
68      /***
69       * (non-Javadoc)
70       * 
71       * @see net.sf.tlc.core.ServiceLocator#unregister(java.lang.String)
72       */
73      public final void unregister(final String name) {
74          map.remove(name);
75      }
76  
77      /***
78       * (non-Javadoc)
79       * 
80       * @see net.sf.tlc.core.ServiceLocator#unregister(java.lang.Object)
81       */
82      public final void unregister(final Object o) {
83          final Object found = findService(o.getClass().getName());
84          if (null != found) {
85              if (found != o) {
86                  throw new IllegalArgumentException("Attempt to unregister the wrong object!");
87              }
88              unregister(o.getClass().getName());
89          } else {
90              // if the object wasn't found by name, but it's a registered service
91              // simply dereference it
92              if (map.entrySet().contains(o)) {
93                  map.remove(o);
94              }
95          }
96      }
97  
98      /***
99       * (non-Javadoc)
100      * 
101      * @see net.sf.tlc.core.ServiceLocator#findService(java.lang.String)
102      */
103     public final Object findService(final String name) {
104         return map.get(name);
105     }
106 
107     /***
108      * (non-Javadoc)
109      * 
110      * @see net.sf.tlc.core.ServiceLocator#findService(java.lang.Class)
111      */
112     public final Object findService(final Class c) {
113         Object o = findService(c.getName());
114         if (null == o) {
115             // attempt to find a suitable class, but only return something if we
116             // can find only one
117             final Set found = new LinkedHashSet(1);
118             final Iterator i = map.values().iterator();
119             while (i.hasNext()) {
120                 final Object n = i.next();
121                 if (c.isAssignableFrom(n.getClass())) {
122                     found.add(n);
123                 }
124             }
125             if (1 == found.size()) {
126                 o = found.iterator().next();
127             }
128         }
129         return o;
130     }
131 
132     /***
133      * (non-Javadoc)
134      * 
135      * @see net.sf.tlc.core.ServiceLocator#keySet()
136      */
137     public final Set keySet() {
138         return map.keySet();
139     }
140 
141     /***
142      * @return the Set of services
143      */
144     protected final Set servicesSet() {
145         return map.entrySet();
146     }
147 
148 }