1 /*
2 * Copyright (c) 2005 Regents of the University of California (Regents). Created
3 * by TELS, Graduate School of Education, University of California at Berkeley.
4 *
5 * This software is distributed under the GNU Lesser General Public License, v2.
6 *
7 * Permission is hereby granted, without written agreement and without license
8 * or royalty fees, to use, copy, modify, and distribute this software and its
9 * documentation for any purpose, provided that the above copyright notice and
10 * the following two paragraphs appear in all copies of this software.
11 *
12 * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15 * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16 * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17 *
18 * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21 * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 * Maintains map of pod ids to pod objects
36 *
37 * @author turadg
38 * AUDIT07-
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 * @return the default PodRegistry for this JVM
49 */
50 public static PodRegistry getDefaultRegistry() {
51 return registrySingleton;
52 }
53
54 /**
55 * This constructor is private because right now so much of the code assumes
56 * one registry for the whole JVM.
57 *
58 * At one point there was the idea of having multiple registries, but
59 * whether that change is worth it depends on how pod versioning and
60 * identification shakes out.
61 *
62 */
63 private PodRegistry() {
64 // TODO Auto-generated constructor stub
65 }
66
67 /**
68 * Mapping the podId to the a pod and listening for changes in the podId
69 *
70 * @param pod
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 * Remove the pod from its old pod id key and put it in with its new one.
86 *
87 * @param pod
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 * Listen when the pod's podId changes
101 *
102 * @param evt
103 */
104 public void propertyChange(PropertyChangeEvent evt) {
105 if (!evt.getPropertyName().equals(Pod.PROPERTY_POD_ID))
106 return;
107 // assume that only Pod has podId property
108 Pod pod = (Pod) evt.getSource();
109 reregister(pod);
110 }
111
112 /**
113 * @param pod
114 * @return true iff the pod was in the registry
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 // should always have both key and value or neither
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 * (non-Javadoc)
133 *
134 * @see java.lang.Object#clone()
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 }