1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.sail.core.beans.assembly;
24
25 import java.beans.PropertyChangeEvent;
26 import java.beans.PropertyChangeListener;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.MissingResourceException;
30
31 import net.sf.sail.core.beans.Pod;
32 import net.sf.sail.core.uuid.PodUuid;
33
34
35
36
37
38
39
40 public class PodRegistry implements PropertyChangeListener, Cloneable {
41
42 Map<PodUuid, Pod> podMap = new HashMap<PodUuid, Pod>();
43
44 private static PodRegistry registrySingleton = new PodRegistry();
45
46
47
48
49
50 public static PodRegistry getDefaultRegistry() {
51 return registrySingleton;
52 }
53
54
55
56
57
58
59
60
61
62
63 private PodRegistry() {
64
65 }
66
67
68
69
70
71
72 public void register(Pod pod) {
73 PodUuid podId = pod.getPodId();
74 if (podId == null)
75 throw new NullPointerException("null podId");
76 podMap.put(podId, pod);
77 pod.addPropertyChangeListener(Pod.PROPERTY_POD_ID, this);
78 }
79
80 public Pod getPod(PodUuid key) {
81 return podMap.get(key);
82 }
83
84
85
86
87
88
89 public void reregister(Pod pod) {
90 podMap.remove(pod);
91 register(pod);
92 }
93
94 @Override
95 public String toString() {
96 return "PodRegistry:" + podMap.toString();
97 }
98
99
100
101
102
103
104 public void propertyChange(PropertyChangeEvent evt) {
105 if (!evt.getPropertyName().equals(Pod.PROPERTY_POD_ID))
106 return;
107
108 Pod pod = (Pod) evt.getSource();
109 reregister(pod);
110 }
111
112
113
114
115
116 public boolean unregister(Pod pod) {
117 PodUuid key = pod.getPodId();
118 boolean containsKey = podMap.containsKey(key);
119 boolean containsValue = podMap.containsValue(pod);
120
121 if ((containsKey && !containsValue) || (!containsKey && containsValue))
122 throw new IllegalStateException();
123 if (!containsValue)
124 return false;
125 pod.removePropertyChangeListener(Pod.PROPERTY_POD_ID, this);
126
127 podMap.remove(key);
128 return true;
129 }
130
131
132
133
134
135
136 @Override
137 public Object clone() throws CloneNotSupportedException {
138 PodRegistry clone = (PodRegistry) super.clone();
139 clone.podMap = new HashMap<PodUuid, Pod>();
140 return clone;
141 }
142
143 }