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 void registerObject(String key, Object value) throws RegistrationException
143 {
144 registerObject(key, value, null);
145 }
146
147 public void registerObject(String key, Object value, Object metadata) throws RegistrationException
148 {
149 Iterator it = getRegistries().iterator();
150 Registry reg;
151 while (it.hasNext())
152 {
153 reg = (Registry) it.next();
154 if (!reg.isReadOnly())
155 {
156 reg.registerObject(key, value, metadata);
157 break;
158 }
159 }
160 }
161
162 public void registerObjects(Map objects) throws RegistrationException
163 {
164 Iterator it = objects.keySet().iterator();
165 Object key;
166 while (it.hasNext())
167 {
168 key = it.next();
169 registerObject((String) key, objects.get(key));
170 }
171 }
172
173 public void unregisterObject(String key) throws RegistrationException
174 {
175 unregisterObject(key, null);
176 }
177
178 public void unregisterObject(String key, Object metadata) throws RegistrationException
179 {
180 Iterator it = getRegistries().iterator();
181 Registry reg;
182 while (it.hasNext())
183 {
184 reg = (Registry) it.next();
185 if (!reg.isReadOnly() && reg.lookupObject(key) != null)
186 {
187 reg.unregisterObject(key, metadata);
188 break;
189 }
190 }
191 }
192 }