1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
package org.apache.hivemind.impl;
|
16
|
|
|
17
|
|
import java.util.Collections;
|
18
|
|
import java.util.HashMap;
|
19
|
|
import java.util.Iterator;
|
20
|
|
import java.util.LinkedList;
|
21
|
|
import java.util.List;
|
22
|
|
import java.util.Locale;
|
23
|
|
import java.util.Map;
|
24
|
|
|
25
|
|
import org.apache.commons.logging.LogFactory;
|
26
|
|
import org.apache.hivemind.ApplicationRuntimeException;
|
27
|
|
import org.apache.hivemind.ErrorHandler;
|
28
|
|
import org.apache.hivemind.HiveMindMessages;
|
29
|
|
import org.apache.hivemind.Location;
|
30
|
|
import org.apache.hivemind.ShutdownCoordinator;
|
31
|
|
import org.apache.hivemind.SymbolSource;
|
32
|
|
import org.apache.hivemind.SymbolSourceContribution;
|
33
|
|
import org.apache.hivemind.internal.ConfigurationPoint;
|
34
|
|
import org.apache.hivemind.internal.Module;
|
35
|
|
import org.apache.hivemind.internal.RegistryInfrastructure;
|
36
|
|
import org.apache.hivemind.internal.ServiceModelFactory;
|
37
|
|
import org.apache.hivemind.internal.ServicePoint;
|
38
|
|
import org.apache.hivemind.internal.ser.ServiceSerializationHelper;
|
39
|
|
import org.apache.hivemind.internal.ser.ServiceSerializationSupport;
|
40
|
|
import org.apache.hivemind.internal.ser.ServiceToken;
|
41
|
|
import org.apache.hivemind.order.Orderer;
|
42
|
|
import org.apache.hivemind.schema.Translator;
|
43
|
|
import org.apache.hivemind.service.ThreadEventNotifier;
|
44
|
|
import org.apache.hivemind.util.Defense;
|
45
|
|
import org.apache.hivemind.util.PropertyUtils;
|
46
|
|
import org.apache.hivemind.util.ToStringBuilder;
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
|
51
|
|
|
52
|
|
|
53
|
|
public final class RegistryInfrastructureImpl implements RegistryInfrastructure,
|
54
|
|
ServiceSerializationSupport
|
55
|
|
{
|
56
|
|
private static final String SYMBOL_SOURCES = "hivemind.SymbolSources";
|
57
|
|
|
58
|
|
|
59
|
|
|
60
|
|
|
61
|
|
private Map _servicePoints = new HashMap();
|
62
|
|
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
private Map _servicePointsByInterfaceClassName = new HashMap();
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
|
private Map _configurationPoints = new HashMap();
|
72
|
|
|
73
|
|
private SymbolSource[] _variableSources;
|
74
|
|
|
75
|
|
private ErrorHandler _errorHandler;
|
76
|
|
|
77
|
|
private Locale _locale;
|
78
|
|
|
79
|
|
private ShutdownCoordinator _shutdownCoordinator;
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
private Map _serviceTokens;
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
private Map _serviceModelFactories;
|
94
|
|
|
95
|
|
private boolean _started = false;
|
96
|
|
|
97
|
|
private boolean _shutdown = false;
|
98
|
|
|
99
|
|
private ThreadEventNotifier _threadEventNotifier;
|
100
|
|
|
101
|
|
private TranslatorManager _translatorManager;
|
102
|
|
|
103
|
|
private SymbolExpander _expander;
|
104
|
|
|
105
|
123
|
public RegistryInfrastructureImpl(ErrorHandler errorHandler, Locale locale)
|
106
|
|
{
|
107
|
123
|
_errorHandler = errorHandler;
|
108
|
123
|
_locale = locale;
|
109
|
|
|
110
|
123
|
_translatorManager = new TranslatorManager(this, errorHandler);
|
111
|
|
|
112
|
123
|
_expander = new SymbolExpander(_errorHandler, this);
|
113
|
|
}
|
114
|
|
|
115
|
13
|
public Locale getLocale()
|
116
|
|
{
|
117
|
13
|
return _locale;
|
118
|
|
}
|
119
|
|
|
120
|
2187
|
public void addServicePoint(ServicePoint point)
|
121
|
|
{
|
122
|
2187
|
checkStarted();
|
123
|
|
|
124
|
2187
|
_servicePoints.put(point.getExtensionPointId(), point);
|
125
|
|
|
126
|
2187
|
addServicePointByInterface(point);
|
127
|
|
}
|
128
|
|
|
129
|
2187
|
private void addServicePointByInterface(ServicePoint point)
|
130
|
|
{
|
131
|
2187
|
String key = point.getServiceInterfaceClassName();
|
132
|
|
|
133
|
2187
|
List l = (List) _servicePointsByInterfaceClassName.get(key);
|
134
|
|
|
135
|
2187
|
if (l == null)
|
136
|
|
{
|
137
|
1368
|
l = new LinkedList();
|
138
|
1368
|
_servicePointsByInterfaceClassName.put(key, l);
|
139
|
|
}
|
140
|
|
|
141
|
2187
|
l.add(point);
|
142
|
|
}
|
143
|
|
|
144
|
926
|
public void addConfigurationPoint(ConfigurationPoint point)
|
145
|
|
{
|
146
|
926
|
checkStarted();
|
147
|
|
|
148
|
926
|
_configurationPoints.put(point.getExtensionPointId(), point);
|
149
|
|
}
|
150
|
|
|
151
|
3284
|
public ServicePoint getServicePoint(String serviceId, Module module)
|
152
|
|
{
|
153
|
3284
|
checkShutdown();
|
154
|
|
|
155
|
3284
|
ServicePoint result = (ServicePoint) _servicePoints.get(serviceId);
|
156
|
|
|
157
|
3284
|
if (result == null)
|
158
|
1
|
throw new ApplicationRuntimeException(ImplMessages.noSuchServicePoint(serviceId));
|
159
|
|
|
160
|
3283
|
if (!result.visibleToModule(module))
|
161
|
2
|
throw new ApplicationRuntimeException(ImplMessages.serviceNotVisible(serviceId, module));
|
162
|
|
|
163
|
3281
|
return result;
|
164
|
|
}
|
165
|
|
|
166
|
2840
|
public Object getService(String serviceId, Class serviceInterface, Module module)
|
167
|
|
{
|
168
|
2840
|
ServicePoint point = getServicePoint(serviceId, module);
|
169
|
|
|
170
|
2838
|
return point.getService(serviceInterface);
|
171
|
|
}
|
172
|
|
|
173
|
139
|
public Object getService(Class serviceInterface, Module module)
|
174
|
|
{
|
175
|
139
|
String key = serviceInterface.getName();
|
176
|
|
|
177
|
139
|
List servicePoints = (List) _servicePointsByInterfaceClassName.get(key);
|
178
|
|
|
179
|
139
|
if (servicePoints == null)
|
180
|
2
|
servicePoints = Collections.EMPTY_LIST;
|
181
|
|
|
182
|
139
|
ServicePoint point = null;
|
183
|
139
|
int count = 0;
|
184
|
|
|
185
|
139
|
Iterator i = servicePoints.iterator();
|
186
|
139
|
while (i.hasNext())
|
187
|
|
{
|
188
|
139
|
ServicePoint sp = (ServicePoint) i.next();
|
189
|
|
|
190
|
139
|
if (!sp.visibleToModule(module))
|
191
|
1
|
continue;
|
192
|
|
|
193
|
138
|
point = sp;
|
194
|
|
|
195
|
138
|
count++;
|
196
|
|
}
|
197
|
|
|
198
|
139
|
if (count == 0)
|
199
|
2
|
throw new ApplicationRuntimeException(ImplMessages
|
200
|
|
.noServicePointForInterface(serviceInterface));
|
201
|
|
|
202
|
137
|
if (count > 1)
|
203
|
1
|
throw new ApplicationRuntimeException(ImplMessages.multipleServicePointsForInterface(
|
204
|
|
serviceInterface,
|
205
|
|
servicePoints));
|
206
|
|
|
207
|
136
|
return point.getService(serviceInterface);
|
208
|
|
}
|
209
|
|
|
210
|
710
|
public ConfigurationPoint getConfigurationPoint(String configurationId, Module module)
|
211
|
|
{
|
212
|
710
|
checkShutdown();
|
213
|
|
|
214
|
709
|
ConfigurationPoint result = (ConfigurationPoint) _configurationPoints.get(configurationId);
|
215
|
|
|
216
|
709
|
if (result == null)
|
217
|
1
|
throw new ApplicationRuntimeException(ImplMessages.noSuchConfiguration(configurationId));
|
218
|
|
|
219
|
708
|
if (!result.visibleToModule(module))
|
220
|
1
|
throw new ApplicationRuntimeException(ImplMessages.configurationNotVisible(
|
221
|
|
configurationId,
|
222
|
|
module));
|
223
|
|
|
224
|
707
|
return result;
|
225
|
|
}
|
226
|
|
|
227
|
487
|
public List getConfiguration(String configurationId, Module module)
|
228
|
|
{
|
229
|
487
|
ConfigurationPoint point = getConfigurationPoint(configurationId, module);
|
230
|
|
|
231
|
484
|
return point.getElements();
|
232
|
|
}
|
233
|
|
|
234
|
1
|
public String toString()
|
235
|
|
{
|
236
|
1
|
ToStringBuilder builder = new ToStringBuilder(this);
|
237
|
|
|
238
|
1
|
builder.append("locale", _locale);
|
239
|
|
|
240
|
1
|
return builder.toString();
|
241
|
|
}
|
242
|
|
|
243
|
6128
|
public String expandSymbols(String text, Location location)
|
244
|
|
{
|
245
|
6128
|
return _expander.expandSymbols(text, location);
|
246
|
|
}
|
247
|
|
|
248
|
5
|
public String valueForSymbol(String name)
|
249
|
|
{
|
250
|
5
|
checkShutdown();
|
251
|
|
|
252
|
5
|
SymbolSource[] sources = getSymbolSources();
|
253
|
|
|
254
|
5
|
for (int i = 0; i < sources.length; i++)
|
255
|
|
{
|
256
|
7
|
String value = sources[i].valueForSymbol(name);
|
257
|
|
|
258
|
7
|
if (value != null)
|
259
|
4
|
return value;
|
260
|
|
}
|
261
|
|
|
262
|
1
|
return null;
|
263
|
|
}
|
264
|
|
|
265
|
5
|
private synchronized SymbolSource[] getSymbolSources()
|
266
|
|
{
|
267
|
5
|
if (_variableSources != null)
|
268
|
2
|
return _variableSources;
|
269
|
|
|
270
|
3
|
List contributions = getConfiguration(SYMBOL_SOURCES, null);
|
271
|
|
|
272
|
3
|
Orderer o = new Orderer(LogFactory.getLog(SYMBOL_SOURCES), _errorHandler, ImplMessages
|
273
|
|
.symbolSourceContribution());
|
274
|
|
|
275
|
3
|
Iterator i = contributions.iterator();
|
276
|
3
|
while (i.hasNext())
|
277
|
|
{
|
278
|
8
|
SymbolSourceContribution c = (SymbolSourceContribution) i.next();
|
279
|
|
|
280
|
8
|
o.add(c, c.getName(), c.getPrecedingNames(), c.getFollowingNames());
|
281
|
|
}
|
282
|
|
|
283
|
3
|
List sources = o.getOrderedObjects();
|
284
|
|
|
285
|
3
|
int count = sources.size();
|
286
|
|
|
287
|
3
|
_variableSources = new SymbolSource[count];
|
288
|
|
|
289
|
3
|
for (int j = 0; j < count; j++)
|
290
|
|
{
|
291
|
8
|
SymbolSourceContribution c = (SymbolSourceContribution) sources.get(j);
|
292
|
8
|
_variableSources[j] = c.getSource();
|
293
|
|
}
|
294
|
|
|
295
|
3
|
return _variableSources;
|
296
|
|
}
|
297
|
|
|
298
|
115
|
public void setShutdownCoordinator(ShutdownCoordinator coordinator)
|
299
|
|
{
|
300
|
115
|
_shutdownCoordinator = coordinator;
|
301
|
|
}
|
302
|
|
|
303
|
|
|
304
|
|
|
305
|
|
|
306
|
|
|
307
|
23
|
public synchronized void shutdown()
|
308
|
|
{
|
309
|
23
|
checkShutdown();
|
310
|
|
|
311
|
22
|
ServiceSerializationHelper.resetServiceSerializationSupport(this);
|
312
|
|
|
313
|
|
|
314
|
|
|
315
|
22
|
ShutdownCoordinator coordinatorService = (ShutdownCoordinator) getService(
|
316
|
|
"hivemind.ShutdownCoordinator",
|
317
|
|
ShutdownCoordinator.class,
|
318
|
|
null);
|
319
|
|
|
320
|
22
|
coordinatorService.shutdown();
|
321
|
|
|
322
|
22
|
_shutdown = true;
|
323
|
|
|
324
|
|
|
325
|
|
|
326
|
22
|
_shutdownCoordinator.shutdown();
|
327
|
|
|
328
|
22
|
_servicePoints = null;
|
329
|
22
|
_servicePointsByInterfaceClassName = null;
|
330
|
22
|
_configurationPoints = null;
|
331
|
22
|
_shutdownCoordinator = null;
|
332
|
22
|
_variableSources = null;
|
333
|
22
|
_serviceModelFactories = null;
|
334
|
22
|
_threadEventNotifier = null;
|
335
|
22
|
_serviceTokens = null;
|
336
|
|
|
337
|
|
|
338
|
|
|
339
|
|
|
340
|
22
|
PropertyUtils.clearCache();
|
341
|
|
}
|
342
|
|
|
343
|
4031
|
private synchronized void checkShutdown()
|
344
|
|
{
|
345
|
4031
|
if (_shutdown)
|
346
|
2
|
throw new ApplicationRuntimeException(HiveMindMessages.registryShutdown());
|
347
|
|
}
|
348
|
|
|
349
|
3223
|
private void checkStarted()
|
350
|
|
{
|
351
|
3223
|
if (_started)
|
352
|
1
|
throw new IllegalStateException(ImplMessages.registryAlreadyStarted());
|
353
|
|
}
|
354
|
|
|
355
|
|
|
356
|
|
|
357
|
|
|
358
|
|
|
359
|
|
|
360
|
|
|
361
|
|
|
362
|
|
|
363
|
|
|
364
|
|
|
365
|
|
|
366
|
|
|
367
|
|
|
368
|
110
|
public void startup()
|
369
|
|
{
|
370
|
110
|
checkStarted();
|
371
|
|
|
372
|
109
|
ServiceSerializationHelper.setServiceSerializationSupport(this);
|
373
|
|
|
374
|
109
|
_started = true;
|
375
|
|
|
376
|
109
|
Runnable startup = (Runnable) getService("hivemind.Startup", Runnable.class, null);
|
377
|
|
|
378
|
109
|
startup.run();
|
379
|
|
}
|
380
|
|
|
381
|
1239
|
public synchronized ServiceModelFactory getServiceModelFactory(String name)
|
382
|
|
{
|
383
|
1239
|
if (_serviceModelFactories == null)
|
384
|
109
|
readServiceModelFactories();
|
385
|
|
|
386
|
1239
|
ServiceModelFactory result = (ServiceModelFactory) _serviceModelFactories.get(name);
|
387
|
|
|
388
|
1239
|
if (result == null)
|
389
|
1
|
throw new ApplicationRuntimeException(ImplMessages.unknownServiceModel(name));
|
390
|
|
|
391
|
1238
|
return result;
|
392
|
|
}
|
393
|
|
|
394
|
109
|
private void readServiceModelFactories()
|
395
|
|
{
|
396
|
109
|
List l = getConfiguration("hivemind.ServiceModels", null);
|
397
|
|
|
398
|
109
|
_serviceModelFactories = new HashMap();
|
399
|
|
|
400
|
109
|
Iterator i = l.iterator();
|
401
|
|
|
402
|
109
|
while (i.hasNext())
|
403
|
|
{
|
404
|
432
|
ServiceModelContribution smc = (ServiceModelContribution) i.next();
|
405
|
|
|
406
|
432
|
String name = smc.getName();
|
407
|
|
|
408
|
432
|
_serviceModelFactories.put(name, smc.getFactory());
|
409
|
|
}
|
410
|
|
}
|
411
|
|
|
412
|
803
|
public synchronized void cleanupThread()
|
413
|
|
{
|
414
|
803
|
if (_threadEventNotifier == null)
|
415
|
7
|
_threadEventNotifier = (ThreadEventNotifier) getService(
|
416
|
|
"hivemind.ThreadEventNotifier",
|
417
|
|
ThreadEventNotifier.class,
|
418
|
|
null);
|
419
|
|
|
420
|
803
|
_threadEventNotifier.fireThreadCleanup();
|
421
|
|
}
|
422
|
|
|
423
|
2
|
public boolean containsConfiguration(String configurationId, Module module)
|
424
|
|
{
|
425
|
2
|
checkShutdown();
|
426
|
|
|
427
|
2
|
ConfigurationPoint result = (ConfigurationPoint) _configurationPoints.get(configurationId);
|
428
|
|
|
429
|
2
|
return result != null && result.visibleToModule(module);
|
430
|
|
}
|
431
|
|
|
432
|
2
|
public boolean containsService(Class serviceInterface, Module module)
|
433
|
|
{
|
434
|
2
|
checkShutdown();
|
435
|
|
|
436
|
2
|
String key = serviceInterface.getName();
|
437
|
|
|
438
|
2
|
List servicePoints = (List) _servicePointsByInterfaceClassName.get(key);
|
439
|
|
|
440
|
2
|
if (servicePoints == null)
|
441
|
0
|
return false;
|
442
|
|
|
443
|
2
|
int count = 0;
|
444
|
|
|
445
|
2
|
Iterator i = servicePoints.iterator();
|
446
|
2
|
while (i.hasNext())
|
447
|
|
{
|
448
|
3
|
ServicePoint point = (ServicePoint) i.next();
|
449
|
|
|
450
|
3
|
if (point.visibleToModule(module))
|
451
|
3
|
count++;
|
452
|
|
}
|
453
|
|
|
454
|
2
|
return count == 1;
|
455
|
|
}
|
456
|
|
|
457
|
3
|
public boolean containsService(String serviceId, Class serviceInterface, Module module)
|
458
|
|
{
|
459
|
3
|
checkShutdown();
|
460
|
|
|
461
|
3
|
ServicePoint point = (ServicePoint) _servicePoints.get(serviceId);
|
462
|
|
|
463
|
3
|
if (point == null)
|
464
|
1
|
return false;
|
465
|
|
|
466
|
2
|
return point.visibleToModule(module)
|
467
|
|
&& point.getServiceInterface().equals(serviceInterface);
|
468
|
|
}
|
469
|
|
|
470
|
921
|
public ErrorHandler getErrorHander()
|
471
|
|
{
|
472
|
921
|
return _errorHandler;
|
473
|
|
}
|
474
|
|
|
475
|
6683
|
public Translator getTranslator(String constructor)
|
476
|
|
{
|
477
|
6683
|
return _translatorManager.getTranslator(constructor);
|
478
|
|
}
|
479
|
|
|
480
|
1
|
public Object getServiceFromToken(ServiceToken token)
|
481
|
|
{
|
482
|
1
|
Defense.notNull(token, "token");
|
483
|
|
|
484
|
1
|
checkShutdown();
|
485
|
|
|
486
|
1
|
String serviceId = token.getServiceId();
|
487
|
|
|
488
|
1
|
ServicePoint sp = (ServicePoint) _servicePoints.get(serviceId);
|
489
|
|
|
490
|
1
|
return sp.getService(Object.class);
|
491
|
|
}
|
492
|
|
|
493
|
1
|
public synchronized ServiceToken getServiceTokenForService(String serviceId)
|
494
|
|
{
|
495
|
1
|
Defense.notNull(serviceId, "serviceId");
|
496
|
|
|
497
|
1
|
checkShutdown();
|
498
|
|
|
499
|
1
|
if (_serviceTokens == null)
|
500
|
1
|
_serviceTokens = new HashMap();
|
501
|
|
|
502
|
1
|
ServiceToken result = (ServiceToken) _serviceTokens.get(serviceId);
|
503
|
|
|
504
|
1
|
if (result == null)
|
505
|
|
{
|
506
|
1
|
result = new ServiceToken(serviceId);
|
507
|
1
|
_serviceTokens.put(serviceId, result);
|
508
|
|
}
|
509
|
|
|
510
|
1
|
return result;
|
511
|
|
}
|
512
|
|
}
|