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 | 0 | public abstract class AbstractRegistryBroker implements RegistryBroker |
28 | |
{ |
29 | |
public void initialise() throws InitialisationException |
30 | |
{ |
31 | 0 | for (Registry registry : getRegistries()) |
32 | |
{ |
33 | 0 | registry.initialise(); |
34 | |
} |
35 | 0 | } |
36 | |
|
37 | |
public void dispose() |
38 | |
{ |
39 | 0 | for (Registry registry : getRegistries()) |
40 | |
{ |
41 | 0 | registry.dispose(); |
42 | |
} |
43 | 0 | } |
44 | |
|
45 | |
public void fireLifecycle(String phase) throws LifecycleException |
46 | |
{ |
47 | 0 | if (Initialisable.PHASE_NAME.equals(phase)) |
48 | |
{ |
49 | 0 | initialise(); |
50 | |
} |
51 | 0 | else if (Disposable.PHASE_NAME.equals(phase)) |
52 | |
{ |
53 | 0 | dispose(); |
54 | |
} |
55 | |
else |
56 | |
{ |
57 | 0 | for (Registry registry : getRegistries()) |
58 | |
{ |
59 | 0 | registry.fireLifecycle(phase); |
60 | |
} |
61 | |
} |
62 | |
|
63 | 0 | } |
64 | |
|
65 | |
public String getRegistryId() |
66 | |
{ |
67 | 0 | return this.toString(); |
68 | |
} |
69 | |
|
70 | |
public boolean isReadOnly() |
71 | |
{ |
72 | 0 | return false; |
73 | |
} |
74 | |
|
75 | |
public boolean isRemote() |
76 | |
{ |
77 | 0 | 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 | 0 | return (T) lookupObject(key); |
91 | |
} |
92 | |
|
93 | |
@SuppressWarnings("unchecked") |
94 | |
public <T> T lookupObject(String key) |
95 | |
{ |
96 | 0 | Object obj = null; |
97 | 0 | Iterator it = getRegistries().iterator(); |
98 | 0 | while (obj == null && it.hasNext()) |
99 | |
{ |
100 | 0 | obj = ((Registry) it.next()).lookupObject(key); |
101 | |
} |
102 | 0 | return (T) obj; |
103 | |
} |
104 | |
|
105 | |
public <T> T lookupObject(Class<T> type) throws RegistrationException |
106 | |
{ |
107 | |
Object object; |
108 | 0 | for (Registry registry : getRegistries()) |
109 | |
{ |
110 | 0 | object = registry.lookupObject(type); |
111 | 0 | if (object != null) |
112 | |
{ |
113 | 0 | return (T) object; |
114 | |
} |
115 | |
} |
116 | 0 | return null; |
117 | |
} |
118 | |
|
119 | |
public <T> Collection<T> lookupObjects(Class<T> type) |
120 | |
{ |
121 | 0 | Collection<T> objects = new ArrayList<T>(); |
122 | |
|
123 | 0 | Iterator it = getRegistries().iterator(); |
124 | 0 | while (it.hasNext()) |
125 | |
{ |
126 | 0 | objects.addAll(((Registry) it.next()).lookupObjects(type)); |
127 | |
} |
128 | 0 | return objects; |
129 | |
} |
130 | |
|
131 | |
public <T> Map<String, T> lookupByType(Class<T> type) |
132 | |
{ |
133 | 0 | Map<String, T> results = new HashMap<String, T>(); |
134 | 0 | for (Registry registry : getRegistries()) |
135 | |
{ |
136 | 0 | results.putAll(registry.lookupByType(type)); |
137 | |
} |
138 | |
|
139 | 0 | return results; |
140 | |
} |
141 | |
|
142 | |
public void registerObject(String key, Object value) throws RegistrationException |
143 | |
{ |
144 | 0 | registerObject(key, value, null); |
145 | 0 | } |
146 | |
|
147 | |
public void registerObject(String key, Object value, Object metadata) throws RegistrationException |
148 | |
{ |
149 | 0 | Iterator it = getRegistries().iterator(); |
150 | |
Registry reg; |
151 | 0 | while (it.hasNext()) |
152 | |
{ |
153 | 0 | reg = (Registry) it.next(); |
154 | 0 | if (!reg.isReadOnly()) |
155 | |
{ |
156 | 0 | reg.registerObject(key, value, metadata); |
157 | 0 | break; |
158 | |
} |
159 | |
} |
160 | 0 | } |
161 | |
|
162 | |
public void registerObjects(Map objects) throws RegistrationException |
163 | |
{ |
164 | 0 | Iterator it = objects.keySet().iterator(); |
165 | |
Object key; |
166 | 0 | while (it.hasNext()) |
167 | |
{ |
168 | 0 | key = it.next(); |
169 | 0 | registerObject((String) key, objects.get(key)); |
170 | |
} |
171 | 0 | } |
172 | |
|
173 | |
public void unregisterObject(String key) throws RegistrationException |
174 | |
{ |
175 | 0 | unregisterObject(key, null); |
176 | 0 | } |
177 | |
|
178 | |
public void unregisterObject(String key, Object metadata) throws RegistrationException |
179 | |
{ |
180 | 0 | Iterator it = getRegistries().iterator(); |
181 | |
Registry reg; |
182 | 0 | while (it.hasNext()) |
183 | |
{ |
184 | 0 | reg = (Registry) it.next(); |
185 | 0 | if (!reg.isReadOnly() && reg.lookupObject(key) != null) |
186 | |
{ |
187 | 0 | reg.unregisterObject(key, metadata); |
188 | 0 | break; |
189 | |
} |
190 | |
} |
191 | 0 | } |
192 | |
} |