| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 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 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
94 |
public class DefaultServiceLocator implements ServiceLocator { |
| 22 |
|
|
| 23 |
57 |
private final Map map = new Hashtable(); |
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 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 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 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 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
public final void register(class="keyword">final Object o) { |
| 65 |
24 |
register(o.getClass().getName(), o); |
| 66 |
24 |
} |
| 67 |
|
|
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
public final void unregister(class="keyword">final String name) { |
| 74 |
12 |
map.remove(name); |
| 75 |
12 |
} |
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
|
|
| 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 |
|
| 91 |
|
|
| 92 |
0 |
if (map.entrySet().contains(o)) { |
| 93 |
0 |
map.remove(o); |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
3 |
} |
| 97 |
|
|
| 98 |
42 |
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
public final Object findService(class="keyword">final String name) { |
| 104 |
42 |
return map.get(name); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
15 |
|
| 108 |
15 |
|
| 109 |
|
|
| 110 |
|
|
| 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 |
|
| 116 |
6 |
|
| 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 |
|
|
| 134 |
|
|
| 135 |
|
|
| 136 |
|
|
| 137 |
|
public final Set keySet() { |
| 138 |
3 |
return map.keySet(); |
| 139 |
|
} |
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
protected final Set servicesSet() { |
| 145 |
0 |
return map.entrySet(); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
} |