%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.tlc.core.impl.DefaultServiceLocator |
|
|
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 | 94 | public class DefaultServiceLocator implements ServiceLocator { |
22 | ||
23 | 57 | 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(class="keyword">final String name, class="keyword">final Object o) { |
|
32 | 60 | if (null == name) { |
33 | 6 | throw new IllegalArgumentException("Cannot use a null value as key!"); |
34 | } |
|
35 | 54 | if (null == o) { |
36 | 6 | throw new IllegalArgumentException("Cannot register a null object as a service!"); |
37 | } |
|
38 | 48 | map.put(name, o); |
39 | 48 | } |
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(class="keyword">final Class c, class="keyword">final Object o) { |
|
48 | 15 | if (null == c) { |
49 | 6 | throw new IllegalArgumentException("Cannot use a null value as key!"); |
50 | } |
|
51 | 9 | if (c.isAssignableFrom(o.getClass())) { |
52 | 6 | register(c.getName(), o); |
53 | } else { |
|
54 | 5 | throw new IllegalArgumentException(c.getName() + " is not assignable from " |
55 | 1 | + o.getClass().getName()); |
56 | } |
|
57 | 6 | } |
58 | ||
59 | /** |
|
60 | * (non-Javadoc) |
|
61 | * |
|
62 | * @see net.sf.tlc.core.ServiceLocator#register(java.lang.Object) |
|
63 | */ |
|
64 | public final void register(class="keyword">final Object o) { |
|
65 | 24 | register(o.getClass().getName(), o); |
66 | 24 | } |
67 | ||
68 | /** |
|
69 | * (non-Javadoc) |
|
70 | * |
|
71 | * @see net.sf.tlc.core.ServiceLocator#unregister(java.lang.String) |
|
72 | */ |
|
73 | public final void unregister(class="keyword">final String name) { |
|
74 | 12 | map.remove(name); |
75 | 12 | } |
76 | ||
77 | /** |
|
78 | * (non-Javadoc) |
|
79 | * |
|
80 | * @see net.sf.tlc.core.ServiceLocator#unregister(java.lang.Object) |
|
81 | */ |
|
82 | public final void unregister(class="keyword">final Object o) { |
|
83 | 12 | final Object found = findService(o.getClass().getName()); |
84 | 12 | if (null != found) { |
85 | 12 | if (found != o) { |
86 | 6 | throw new IllegalArgumentException("Attempt to unregister the wrong object!"); |
87 | } |
|
88 | 6 | unregister(o.getClass().getName()); |
89 | } else { |
|
90 | 3 | // if the object wasn't found by name, but it's a registered service |
91 | // simply dereference it |
|
92 | 0 | if (map.entrySet().contains(o)) { |
93 | 0 | map.remove(o); |
94 | } |
|
95 | } |
|
96 | 3 | } |
97 | ||
98 | 42 | /** |
99 | * (non-Javadoc) |
|
100 | * |
|
101 | * @see net.sf.tlc.core.ServiceLocator#findService(java.lang.String) |
|
102 | */ |
|
103 | public final Object findService(class="keyword">final String name) { |
|
104 | 42 | return map.get(name); |
105 | } |
|
106 | ||
107 | 15 | /** |
108 | 15 | * (non-Javadoc) |
109 | * |
|
110 | * @see net.sf.tlc.core.ServiceLocator#findService(java.lang.Class) |
|
111 | 6 | */ |
112 | 6 | public final Object findService(class="keyword">final Class c) { |
113 | 33 | Object o = findService(c.getName()); |
114 | 21 | if (null == o) { |
115 | 6 | // attempt to find a suitable class, but only return something if we |
116 | 6 | // can find only one |
117 | 6 | final Set found = new LinkedHashSet(1); |
118 | 6 | final Iterator i = map.values().iterator(); |
119 | 20 | while (i.hasNext()) { |
120 | 12 | final Object n = i.next(); |
121 | 6 | if (c.isAssignableFrom(n.getClass())) { |
122 | 6 | found.add(n); |
123 | 15 | } |
124 | } |
|
125 | 6 | if (1 == found.size()) { |
126 | 6 | o = found.iterator().next(); |
127 | } |
|
128 | } |
|
129 | 15 | return o; |
130 | } |
|
131 | ||
132 | 3 | /** |
133 | * (non-Javadoc) |
|
134 | * |
|
135 | * @see net.sf.tlc.core.ServiceLocator#keySet() |
|
136 | */ |
|
137 | public final Set keySet() { |
|
138 | 3 | return map.keySet(); |
139 | } |
|
140 | ||
141 | /** |
|
142 | * @return the Set of services |
|
143 | */ |
|
144 | protected final Set servicesSet() { |
|
145 | 0 | return map.entrySet(); |
146 | } |
|
147 | ||
148 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |