1 package com.atlassian.plugin.osgi.spring;
2
3 import org.osgi.framework.BundleContext;
4 import org.osgi.framework.InvalidSyntaxException;
5 import org.osgi.framework.ServiceEvent;
6 import org.osgi.framework.ServiceListener;
7 import org.osgi.framework.ServiceReference;
8 import org.springframework.context.ApplicationListener;
9 import org.springframework.context.event.SimpleApplicationEventMulticaster;
10 import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEventMulticasterAdapter;
11 import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEvent;
12 import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
13
14
15
16
17
18
19 public class PluginBridgeEventMulticaster extends OsgiBundleApplicationContextEventMulticasterAdapter
20 {
21 private volatile OsgiBundleApplicationContextListener bridgeListener;
22
23
24
25
26
27 public PluginBridgeEventMulticaster(final BundleContext bundleContext)
28 {
29 super(new SimpleApplicationEventMulticaster());
30
31 String filter = "(&(objectClass=" + OsgiBundleApplicationContextListener.class.getName() + ")(plugin-bridge=true))";
32
33 ServiceReference[] refs;
34 try
35 {
36 refs = bundleContext.getAllServiceReferences(ApplicationListener.class.getName(), filter);
37 if (refs != null && refs.length == 1)
38 {
39 bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(refs[0]);
40 }
41
42
43 bundleContext.addServiceListener(new ServiceListener()
44 {
45
46 public void serviceChanged(ServiceEvent serviceEvent)
47 {
48 switch (serviceEvent.getType())
49 {
50 case ServiceEvent.REGISTERED :
51 bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(serviceEvent.getServiceReference());
52 break;
53 case ServiceEvent.UNREGISTERING :
54 bridgeListener = null;
55 break;
56 case ServiceEvent.MODIFIED :
57 bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(serviceEvent.getServiceReference());
58 break;
59 }
60 }
61 }, filter);
62 }
63 catch (InvalidSyntaxException e)
64 {
65
66 throw new RuntimeException("Invalid LDAP filter", e);
67 }
68 }
69
70 @Override
71 public void multicastEvent(OsgiBundleApplicationContextEvent applicationEvent)
72 {
73 super.multicastEvent(applicationEvent);
74 if (bridgeListener != null)
75 bridgeListener.onOsgiApplicationEvent(applicationEvent);
76 }
77 }