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