1
2
3
4
5
6
7
8
9
10
11 package org.mule.registry;
12
13 import org.mule.api.lifecycle.Disposable;
14 import org.mule.api.lifecycle.Initialisable;
15 import org.mule.api.lifecycle.InitialisationException;
16 import org.mule.api.lifecycle.LifecycleException;
17 import org.mule.api.registry.RegistrationException;
18 import org.mule.api.registry.Registry;
19 import org.mule.api.registry.RegistryBroker;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 public abstract class AbstractRegistryBroker implements RegistryBroker
28 {
29 public void initialise() throws InitialisationException
30 {
31 for (Registry registry : getRegistries())
32 {
33 registry.initialise();
34 }
35 }
36
37 public void dispose()
38 {
39 for (Registry registry : getRegistries())
40 {
41 registry.dispose();
42 }
43 }
44
45 public void fireLifecycle(String phase) throws LifecycleException
46 {
47 if (Initialisable.PHASE_NAME.equals(phase))
48 {
49 initialise();
50 }
51 else if (Disposable.PHASE_NAME.equals(phase))
52 {
53 dispose();
54 }
55 else
56 {
57 for (Registry registry : getRegistries())
58 {
59 registry.fireLifecycle(phase);
60 }
61 }
62
63 }
64
65 public String getRegistryId()
66 {
67 return this.toString();
68 }
69
70 public boolean isReadOnly()
71 {
72 return false;
73 }
74
75 public boolean isRemote()
76 {
77 return false;
78 }
79
80 abstract protected Collection<Registry> getRegistries();
81
82
83
84
85
86
87 @SuppressWarnings("unchecked")
88 public <T> T get(String key)
89 {
90 return (T) lookupObject(key);
91 }
92
93 @SuppressWarnings("unchecked")
94 public <T> T lookupObject(String key)
95 {
96 Object obj = null;
97 Iterator it = getRegistries().iterator();
98 while (obj == null && it.hasNext())
99 {
100 obj = ((Registry) it.next()).lookupObject(key);
101 }
102 return (T) obj;
103 }
104
105 public <T> T lookupObject(Class<T> type) throws RegistrationException
106 {
107 Object object;
108 for (Registry registry : getRegistries())
109 {
110 object = registry.lookupObject(type);
111 if (object != null)
112 {
113 return (T) object;
114 }
115 }
116 return null;
117 }
118
119 public <T> Collection<T> lookupObjects(Class<T> type)
120 {
121 Collection<T> objects = new ArrayList<T>();
122
123 Iterator it = getRegistries().iterator();
124 while (it.hasNext())
125 {
126 objects.addAll(((Registry) it.next()).lookupObjects(type));
127 }
128 return objects;
129 }
130
131 public <T> Map<String, T> lookupByType(Class<T> type)
132 {
133 Map<String, T> results = new HashMap<String, T>();
134 for (Registry registry : getRegistries())
135 {
136 results.putAll(registry.lookupByType(type));
137 }
138
139 return results;
140 }
141
142 public <T> Collection<T> lookupObjectsForLifecycle(Class<T> type)
143 {
144 Collection<T> objects = new ArrayList<T>();
145
146 Iterator it = getRegistries().iterator();
147 while (it.hasNext())
148 {
149 objects.addAll(((Registry) it.next()).lookupObjectsForLifecycle(type));
150 }
151 return objects;
152 }
153
154 public void registerObject(String key, Object value) throws RegistrationException
155 {
156 registerObject(key, value, null);
157 }
158
159 public void registerObject(String key, Object value, Object metadata) throws RegistrationException
160 {
161 Iterator it = getRegistries().iterator();
162 Registry reg;
163 while (it.hasNext())
164 {
165 reg = (Registry) it.next();
166 if (!reg.isReadOnly())
167 {
168 reg.registerObject(key, value, metadata);
169 break;
170 }
171 }
172 }
173
174 public void registerObjects(Map objects) throws RegistrationException
175 {
176 Iterator it = objects.keySet().iterator();
177 Object key;
178 while (it.hasNext())
179 {
180 key = it.next();
181 registerObject((String) key, objects.get(key));
182 }
183 }
184
185 public void unregisterObject(String key) throws RegistrationException
186 {
187 unregisterObject(key, null);
188 }
189
190 public void unregisterObject(String key, Object metadata) throws RegistrationException
191 {
192 Iterator it = getRegistries().iterator();
193 Registry reg;
194 while (it.hasNext())
195 {
196 reg = (Registry) it.next();
197 if (!reg.isReadOnly() && reg.lookupObject(key) != null)
198 {
199 reg.unregisterObject(key, metadata);
200 break;
201 }
202 }
203 }
204 }