-
Notifications
You must be signed in to change notification settings - Fork 283
/
WeldOSGiExtension.java
691 lines (623 loc) · 28.4 KB
/
WeldOSGiExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*
* JBoss, Home of Professional Open Source
* Copyright 2011, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.environment.osgi.impl.extension.service;
import java.io.File;
import java.io.InputStream;
import java.util.Dictionary;
import javax.naming.NamingException;
import org.jboss.weld.environment.osgi.api.Service;
import org.jboss.weld.environment.osgi.api.annotation.Filter;
import org.jboss.weld.environment.osgi.api.annotation.OSGiService;
import org.jboss.weld.environment.osgi.api.annotation.Required;
import org.jboss.weld.environment.osgi.impl.extension.beans.OSGiUtilitiesProducer;
import org.jboss.weld.environment.osgi.impl.extension.OSGiServiceAnnotatedType;
import org.jboss.weld.environment.osgi.impl.extension.FilterGenerator;
import org.jboss.weld.environment.osgi.impl.extension.OSGiServiceBean;
import org.jboss.weld.environment.osgi.impl.extension.OSGiServiceProducerBean;
import org.jboss.weld.environment.osgi.impl.extension.beans.BundleHolder;
import org.jboss.weld.environment.osgi.impl.extension.beans.ContainerObserver;
import org.jboss.weld.environment.osgi.impl.extension.beans.RegistrationsHolderImpl;
import org.jboss.weld.environment.osgi.impl.extension.beans.ServiceRegistryImpl;
import org.jboss.weld.environment.osgi.impl.integration.InstanceHolder;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.BundleListener;
import org.osgi.framework.BundleReference;
import org.osgi.framework.FrameworkListener;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessInjectionTarget;
import javax.enterprise.inject.spi.ProcessObserverMethod;
import javax.enterprise.inject.spi.ProcessProducer;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.jboss.weld.environment.osgi.api.events.AbstractBundleEvent;
import org.jboss.weld.environment.osgi.api.events.AbstractServiceEvent;
import org.jboss.weld.environment.osgi.api.events.BundleEvents;
import org.jboss.weld.environment.osgi.api.events.ServiceEvents;
import org.jboss.weld.environment.osgi.impl.Activator;
import org.jboss.weld.environment.osgi.impl.annotation.BundleNameAnnotation;
import org.jboss.weld.environment.osgi.impl.annotation.BundleVersionAnnotation;
import org.jboss.weld.environment.osgi.impl.annotation.FilterAnnotation;
import org.jboss.weld.environment.osgi.impl.annotation.SpecificationAnnotation;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.SynchronousBundleListener;
/**
* Weld OSGi {@link Extension}. Contains copy/paste parts from the GlassFish
* OSGI-CDI extension.
* <p/>
* It registers utility beans for Weld-OSGi framework, process OSGi service
* injection points and registers OSGi specific observers.
* <b/>
* @author Mathieu ANCELIN - SERLI (mathieu.ancelin@serli.com)
* @author Matthieu CLOCHARD - SERLI (matthieu.clochard@serli.com)
*/
@ApplicationScoped
public class WeldOSGiExtension implements Extension {
private static Logger logger = LoggerFactory.getLogger(
WeldOSGiExtension.class);
private static boolean autoRunInHybridMode = true;
// hack for weld integration
public static ThreadLocal<Long> currentBundle = new ThreadLocal<Long>();
public static ThreadLocal<BundleContext> currentContext = new ThreadLocal<BundleContext>();
private HashMap<Type, Set<InjectionPoint>> servicesToBeInjected =
new HashMap<Type, Set<InjectionPoint>>();
private HashMap<Type, Set<InjectionPoint>> serviceProducerToBeInjected =
new HashMap<Type, Set<InjectionPoint>>();
private List<Annotation> observers = new ArrayList<Annotation>();
private Map<Class, Set<Filter>> requiredOsgiServiceDependencies =
new HashMap<Class, Set<Filter>>();
private List<Exception> exceptions = new ArrayList<Exception>();
private BeanManager beanManager;
private HybridListener listener;
private BundleContextDelegate delegate;
void registerCDIOSGiBeans(@Observes BeforeBeanDiscovery event,
BeanManager manager) {
logger.debug("Observe a BeforeBeanDiscovery event");
event.addAnnotatedType(
manager.createAnnotatedType(OSGiUtilitiesProducer.class));
event.addAnnotatedType(
manager.createAnnotatedType(BundleHolder.class));
event.addAnnotatedType(
manager.createAnnotatedType(RegistrationsHolderImpl.class));
event.addAnnotatedType(
manager.createAnnotatedType(ServiceRegistryImpl.class));
event.addAnnotatedType(
manager.createAnnotatedType(ContainerObserver.class));
event.addAnnotatedType(
manager.createAnnotatedType(InstanceHolder.class));
this.beanManager = manager;
if (!Activator.osgiStarted() && WeldOSGiExtension.autoRunInHybridMode) {
delegate = new BundleContextDelegate();
currentContext.set(delegate);
}
}
/**
* Processes all bean class for the current bundle in order to wrap them.
* <p/>
* @param event the bean class to be processed event.
*
* @see OSGiServiceAnnotatedType
*/
void discoverCDIOSGiClass(@Observes ProcessAnnotatedType<?> event) {
logger.debug("Observe a ProcessAnnotatedType event");
AnnotatedType annotatedType = event.getAnnotatedType();
annotatedType = discoverAndProcessCDIOSGiClass(annotatedType);
if (annotatedType != null) {
event.setAnnotatedType(annotatedType);
} else {
logger.warn("The annotated type {} is ignored", annotatedType);
event.veto();
}
}
/**
* Processes all OSGi service injection points ({@link OSGiService}
* annotated, {@link Service} typed and {@link Required}).
* <p/>
* @param event the injection point to be processed event.
*/
void discoverCDIOSGiServices(@Observes ProcessInjectionTarget<?> event) {
logger.debug("Observe a ProcessInjectionTarget event");
Set<InjectionPoint> injectionPoints = event.getInjectionTarget().getInjectionPoints();
discoverServiceInjectionPoints(injectionPoints);
}
void afterProcessProducer(@Observes ProcessProducer<?, ?> event) {
//Only using ProcessInjectionTarget for now.
//TODO do we need to scan these events
}
void afterProcessBean(@Observes ProcessBean<?> event) {
//ProcessInjectionTarget and ProcessProducer take care of all relevant injection points.
//TODO verify that :)
}
void registerObservers(@Observes ProcessObserverMethod<?, ?> event) {
logger.debug("Observe a ProcessObserverMethod event");
Set<Annotation> qualifiers = event.getObserverMethod().getObservedQualifiers();
for (Annotation qualifier : qualifiers) {
if (qualifier.annotationType().equals(Filter.class)) {
observers.add(qualifier);
}
}
}
/**
* Creates the beans that match the discovered OSGi service injection points
* ({@link OSGiService} annotated and {@link Service} typed).
* <p/>
* @param event the AfterBeanDiscovery event.
*/
void registerCDIOSGiServices(@Observes AfterBeanDiscovery event) {
logger.debug("Observe an AfterBeanDiscovery event");
for (Exception exception : exceptions) {
logger.error("Registering a Weld-OSGi deployment error {}", exception);
event.addDefinitionError(exception);
}
for (Iterator<Type> iterator = this.servicesToBeInjected.keySet().iterator();
iterator.hasNext();) {
Type type = iterator.next();
if (!(type instanceof Class)) {
//TODO: need to handle Instance<Class>. This fails currently
logger.error("Unknown type: {}", type);
event.addDefinitionError(
new UnsupportedOperationException("Injection target type "
+ type + "not supported"));
break;
}
addService(event, this.servicesToBeInjected.get(type));
}
for (Iterator<Type> iterator =
this.serviceProducerToBeInjected.keySet().iterator();
iterator.hasNext();) {
Type type = iterator.next();
addServiceProducer(event, this.serviceProducerToBeInjected.get(type));
}
}
public void startHybridMode() {
if (!Activator.osgiStarted() && WeldOSGiExtension.autoRunInHybridMode) {
logger.warn("Starting Weld-OSGi extension in hybrid mode.");
runExtensionInHybridMode();
}
}
public void startHybridMode(BundleContext ctx) {
if (!Activator.osgiStarted() && WeldOSGiExtension.autoRunInHybridMode) {
logger.warn("Starting Weld-OSGi extension in hybrid mode.");
listener = runInHybridMode(ctx);
}
}
private void runExtensionInHybridMode() {
BundleContext bc = null;
try {
Context ctx = new InitialContext();
bc = (BundleContext) ctx.lookup("java:comp/BundleContext");
logger.info("JNDI lookup succeed :-)");
} catch (NamingException ex) {
logger.warn("Cannot lookup JNDI BundleContext.");
}
if (bc == null) {
try {
if (!servicesToBeInjected.isEmpty()) {
Set<InjectionPoint> injections =
servicesToBeInjected.values().iterator().next();
if (!injections.isEmpty()) {
InjectionPoint ip = injections.iterator().next();
Class annotatedElt = ip.getMember().getDeclaringClass();
bc = BundleReference.class.cast(annotatedElt.getClassLoader()).getBundle().getBundleContext();
listener = runInHybridMode(bc);
}
} else if (!serviceProducerToBeInjected.isEmpty()) {
Set<InjectionPoint> injections =
serviceProducerToBeInjected.values().iterator().next();
if (!injections.isEmpty()) {
InjectionPoint ip = injections.iterator().next();
Class annotatedElt = ip.getMember().getDeclaringClass();
bc = BundleReference.class.cast(annotatedElt.getClassLoader()).getBundle().getBundleContext();
listener = runInHybridMode(bc);
}
} else {
bc = BundleReference.class.cast(getClass().getClassLoader()).getBundle().getBundleContext();
logger.warn("Starting the extension assuming the bundle is {}",
bc.getBundle().getSymbolicName());
listener = runInHybridMode(bc);
}
} catch (Exception e) {
logger.error("Unable to start Weld-OSGi in Hybrid mode.");
}
} else {
listener = runInHybridMode(bc);
}
}
/**
* Method used to bootstrap the the OSGi part of the extension in an hybrid environment.
* Provided for server integration purposes.
*
* @param bc
* @param activator
*/
private HybridListener runInHybridMode(BundleContext bc) {
HybridListener list = new HybridListener(bc, this);
delegate.setContext(bc);
bc.addBundleListener(list);
bc.addServiceListener(list);
currentContext.set(bc);
currentBundle.set(bc.getBundle().getBundleId());
return list;
}
void afterDeployment(@Observes AfterDeploymentValidation event) {
if (listener != null) {
currentContext.remove();
currentBundle.remove();
}
}
private AnnotatedType discoverAndProcessCDIOSGiClass(
AnnotatedType annotatedType) {
try {
return new OSGiServiceAnnotatedType(annotatedType);
} catch (Exception e) {
exceptions.add(e);
}
return null;
}
private void discoverServiceInjectionPoints(
Set<InjectionPoint> injectionPoints) {
for (Iterator<InjectionPoint> iterator = injectionPoints.iterator();
iterator.hasNext();) {
InjectionPoint injectionPoint = iterator.next();
boolean service = false;
try {
if (((ParameterizedType) injectionPoint.getType()).getRawType().equals(Service.class)) {
service = true;
}
} catch (Exception e) {//Not a ParameterizedType, skip
}
if (service) {
addServiceProducerInjectionInfo(injectionPoint);
} else if (contains(injectionPoint.getQualifiers(), OSGiService.class)) {
addServiceInjectionInfo(injectionPoint);
}
if (contains(injectionPoint.getQualifiers(), Required.class)) {
Class key;
if (service) {
key = (Class) ((ParameterizedType) injectionPoint.getType()).getActualTypeArguments()[0];
} else {
key = (Class) injectionPoint.getType();
}
Filter value = FilterGenerator.makeFilter(injectionPoint);
if (!requiredOsgiServiceDependencies.containsKey(key)) {
requiredOsgiServiceDependencies.put(key, new HashSet<Filter>());
}
requiredOsgiServiceDependencies.get(key).add(value);
}
}
}
private void addServiceInjectionInfo(InjectionPoint injectionPoint) {
Type key = injectionPoint.getType();
if (!servicesToBeInjected.containsKey(key)) {
servicesToBeInjected.put(key, new HashSet<InjectionPoint>());
}
servicesToBeInjected.get(key).add(injectionPoint);
}
private void addServiceProducerInjectionInfo(InjectionPoint injectionPoint) {
Type key = injectionPoint.getType();
if (!serviceProducerToBeInjected.containsKey(key)) {
serviceProducerToBeInjected.put(key, new HashSet<InjectionPoint>());
}
serviceProducerToBeInjected.get(key).add(injectionPoint);
}
private void addService(AfterBeanDiscovery event,
final Set<InjectionPoint> injectionPoints) {
Set<OSGiServiceBean> beans = new HashSet<OSGiServiceBean>();
for (Iterator<InjectionPoint> iterator = injectionPoints.iterator();
iterator.hasNext();) {
final InjectionPoint injectionPoint = iterator.next();
beans.add(new OSGiServiceBean(injectionPoint, currentContext.get()));
}
for (OSGiServiceBean bean : beans) {
event.addBean(bean);
}
}
private void addServiceProducer(AfterBeanDiscovery event,
final Set<InjectionPoint> injectionPoints) {
Set<OSGiServiceProducerBean> beans = new HashSet<OSGiServiceProducerBean>();
for (Iterator<InjectionPoint> iterator = injectionPoints.iterator();
iterator.hasNext();) {
final InjectionPoint injectionPoint = iterator.next();
beans.add(new OSGiServiceProducerBean(injectionPoint, currentContext.get()));
}
for (OSGiServiceProducerBean bean : beans) {
event.addBean(bean);
}
}
private boolean contains(Set<Annotation> qualifiers,
Class<? extends Annotation> qualifier) {
for (Iterator<Annotation> iterator = qualifiers.iterator();
iterator.hasNext();) {
if (iterator.next().annotationType().equals(qualifier)) {
return true;
}
}
return false;
}
public List<Annotation> getObservers() {
return observers;
}
public Map<Class, Set<Filter>> getRequiredOsgiServiceDependencies() {
return requiredOsgiServiceDependencies;
}
public static void setAutoRunInHybridMode(boolean autoRunInHybridMode) {
WeldOSGiExtension.autoRunInHybridMode = autoRunInHybridMode;
}
public static boolean isAutoRunInHybridMode() {
return autoRunInHybridMode;
}
private static class HybridListener implements SynchronousBundleListener,
ServiceListener {
private static Logger logger = LoggerFactory.getLogger(HybridListener.class);
private final BundleContext context;
private final WeldOSGiExtension extension;
public HybridListener(BundleContext context, WeldOSGiExtension extension) {
this.context = context;
this.extension = extension;
}
@Override
public void bundleChanged(BundleEvent event) {
Bundle bundle = event.getBundle();
AbstractBundleEvent bundleEvent = null;
switch(event.getType()) {
case BundleEvent.INSTALLED:
logger.debug("Receiving a new OSGi bundle event INSTALLED");
bundleEvent = new BundleEvents.BundleInstalled(bundle);
break;
case BundleEvent.LAZY_ACTIVATION:
logger.debug("Receiving a new OSGi bundle event LAZY_ACTIVATION");
bundleEvent = new BundleEvents.BundleLazyActivation(bundle);
break;
case BundleEvent.RESOLVED:
logger.debug("Receiving a new OSGi bundle event RESOLVED");
bundleEvent = new BundleEvents.BundleResolved(bundle);
break;
case BundleEvent.STARTED:
logger.debug("Receiving a new OSGi bundle event STARTED");
bundleEvent = new BundleEvents.BundleStarted(bundle);
break;
case BundleEvent.STARTING:
logger.debug("Receiving a new OSGi bundle event STARTING");
bundleEvent = new BundleEvents.BundleStarting(bundle);
break;
case BundleEvent.STOPPED:
logger.debug("Receiving a new OSGi bundle event STOPPED");
bundleEvent = new BundleEvents.BundleStopped(bundle);
break;
case BundleEvent.STOPPING:
logger.debug("Receiving a new OSGi bundle event STOPPING");
bundleEvent = new BundleEvents.BundleStopping(bundle);
break;
case BundleEvent.UNINSTALLED:
logger.debug("Receiving a new OSGi bundle event UNINSTALLED");
bundleEvent = new BundleEvents.BundleUninstalled(bundle);
break;
case BundleEvent.UNRESOLVED:
logger.debug("Receiving a new OSGi bundle event UNRESOLVED");
bundleEvent = new BundleEvents.BundleUnresolved(bundle);
break;
case BundleEvent.UPDATED:
logger.debug("Receiving a new OSGi bundle event UPDATED");
bundleEvent = new BundleEvents.BundleUpdated(bundle);
break;
}
boolean set = WeldOSGiExtension.currentBundle.get() != null;
WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId());
try {
//broadcast the OSGi event through CDI event system
extension.beanManager.fireEvent(event);
}
catch(Throwable t) {
t.printStackTrace();
}
if (bundleEvent != null) {
//broadcast the corresponding Weld-OSGi event
fireAllBundleEvent(bundleEvent);
}
if (!set) {
WeldOSGiExtension.currentBundle.remove();
}
}
@Override
public void serviceChanged(ServiceEvent event) {
ServiceReference ref = event.getServiceReference();
AbstractServiceEvent serviceEvent = null;
switch(event.getType()) {
case ServiceEvent.MODIFIED:
logger.debug("Receiving a new OSGi service event MODIFIED");
serviceEvent = new ServiceEvents.ServiceChanged(ref, context);
break;
case ServiceEvent.REGISTERED:
logger.debug("Receiving a new OSGi service event REGISTERED");
serviceEvent = new ServiceEvents.ServiceArrival(ref, context);
break;
case ServiceEvent.UNREGISTERING:
logger.debug("Receiving a new OSGi service event UNREGISTERING");
serviceEvent = new ServiceEvents.ServiceDeparture(ref, context);
break;
}
boolean set = WeldOSGiExtension.currentBundle.get() != null;
WeldOSGiExtension.currentBundle.set(context.getBundle().getBundleId());
try {
//broadcast the OSGi event through CDI event system
extension.beanManager.fireEvent(event);
}
catch(Throwable t) {
t.printStackTrace();
}
if (serviceEvent != null) {
//broadcast the corresponding Weld-OSGi event
fireAllServiceEvent(serviceEvent);
}
if (!set) {
WeldOSGiExtension.currentBundle.remove();
}
}
private void fireAllServiceEvent(AbstractServiceEvent event) {
List<Class<?>> classes = event.getServiceClasses(getClass());
Class eventClass = event.getClass();
for (Class<?> clazz : classes) {
try {
Annotation[] qualifs = filteredServicesQualifiers(event,
new SpecificationAnnotation(clazz));
extension.beanManager.fireEvent(event, qualifs);
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
private Annotation[] filteredServicesQualifiers(AbstractServiceEvent event,
SpecificationAnnotation specific) {
Set<Annotation> eventQualifiers = new HashSet<Annotation>();
eventQualifiers.add(specific);
for (Annotation annotation : extension.getObservers()) {
String value = ((Filter) annotation).value();
try {
org.osgi.framework.Filter filter = context.createFilter(value);
if (filter.match(event.getReference())) {
eventQualifiers.add(new FilterAnnotation(value));
}
}
catch(InvalidSyntaxException ex) {
//ex.printStackTrace();
}
}
return eventQualifiers.toArray(new Annotation[eventQualifiers.size()]);
}
private void fireAllBundleEvent(AbstractBundleEvent event) {
try {
extension.beanManager.fireEvent(event,
new BundleNameAnnotation(event.getSymbolicName()),
new BundleVersionAnnotation(event.getVersion().toString()));
}
catch(Throwable t) {
t.printStackTrace();
}
}
}
private static class BundleContextDelegate implements BundleContext {
private BundleContext context;
public void setContext(BundleContext context) {
System.out.println("setup delegate context : " + context);
this.context = context;
}
public String toString() {
return context.toString();
}
public int hashCode() {
return context.hashCode();
}
public boolean equals(Object obj) {
return context.equals(obj);
}
public boolean ungetService(ServiceReference reference) {
return context.ungetService(reference);
}
public void removeServiceListener(ServiceListener listener) {
context.removeServiceListener(listener);
}
public void removeFrameworkListener(FrameworkListener listener) {
context.removeFrameworkListener(listener);
}
public void removeBundleListener(BundleListener listener) {
context.removeBundleListener(listener);
}
public ServiceRegistration registerService(String clazz, Object service, Dictionary properties) {
return context.registerService(clazz, service, properties);
}
public ServiceRegistration registerService(String[] clazzes, Object service, Dictionary properties) {
return context.registerService(clazzes, service, properties);
}
public Bundle installBundle(String location) throws BundleException {
return context.installBundle(location);
}
public Bundle installBundle(String location, InputStream input) throws BundleException {
return context.installBundle(location, input);
}
public ServiceReference[] getServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
return context.getServiceReferences(clazz, filter);
}
public ServiceReference getServiceReference(String clazz) {
return context.getServiceReference(clazz);
}
public Object getService(ServiceReference reference) {
return context.getService(reference);
}
public String getProperty(String key) {
return context.getProperty(key);
}
public File getDataFile(String filename) {
return context.getDataFile(filename);
}
public Bundle[] getBundles() {
return context.getBundles();
}
public Bundle getBundle(long id) {
return context.getBundle(id);
}
public Bundle getBundle() {
return context.getBundle();
}
public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException {
return context.getAllServiceReferences(clazz, filter);
}
public org.osgi.framework.Filter createFilter(String filter) throws InvalidSyntaxException {
return context.createFilter(filter);
}
public void addServiceListener(ServiceListener listener) {
context.addServiceListener(listener);
}
public void addServiceListener(ServiceListener listener, String filter) throws InvalidSyntaxException {
context.addServiceListener(listener, filter);
}
public void addFrameworkListener(FrameworkListener listener) {
context.addFrameworkListener(listener);
}
public void addBundleListener(BundleListener listener) {
context.addBundleListener(listener);
}
}
}