View Javadoc
1   package com.atlassian.plugin.osgi.spring;
2   
3   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEvent;
4   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEventMulticasterAdapter;
5   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
6   import org.osgi.framework.BundleContext;
7   import org.osgi.framework.InvalidSyntaxException;
8   import org.osgi.framework.ServiceEvent;
9   import org.osgi.framework.ServiceListener;
10  import org.osgi.framework.ServiceReference;
11  import org.springframework.context.ApplicationListener;
12  import org.springframework.context.event.SimpleApplicationEventMulticaster;
13  
14  /**
15   * Finds ApplicationListener bridge and uses it to CC all event broadcasts
16   *
17   * @since 2.2.1
18   */
19  public class PluginBridgeEventMulticaster extends OsgiBundleApplicationContextEventMulticasterAdapter {
20      private volatile OsgiBundleApplicationContextListener bridgeListener;
21  
22      /**
23       * Look for the application listener bridge from atlassian-plugins-osgi-bridge.  Can't use Spring DM stuff as it
24       * creates a circular dependency.
25       */
26      public PluginBridgeEventMulticaster(final BundleContext bundleContext) {
27          super(new SimpleApplicationEventMulticaster());
28  
29          String filter = "(&(objectClass=" + OsgiBundleApplicationContextListener.class.getName() + ")(plugin-bridge=true))";
30  
31          ServiceReference[] refs;
32          try {
33              refs = bundleContext.getAllServiceReferences(ApplicationListener.class.getName(), filter);
34              if (refs != null && refs.length == 1) {
35                  bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(refs[0]);
36              }
37  
38              // Add listener to catch the extremely rare case of a late deployment or upgrade
39              bundleContext.addServiceListener(new ServiceListener() {
40  
41                  public void serviceChanged(ServiceEvent serviceEvent) {
42                      switch (serviceEvent.getType()) {
43                          case ServiceEvent.REGISTERED:
44                              bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(serviceEvent.getServiceReference());
45                              break;
46                          case ServiceEvent.UNREGISTERING:
47                              bridgeListener = null;
48                              break;
49                          case ServiceEvent.MODIFIED:
50                              bridgeListener = (OsgiBundleApplicationContextListener) bundleContext.getService(serviceEvent.getServiceReference());
51                              break;
52                      }
53                  }
54              }, filter);
55          } catch (InvalidSyntaxException e) {
56              // Should never happen
57              throw new RuntimeException("Invalid LDAP filter", e);
58          }
59      }
60  
61      @Override
62      public void multicastEvent(OsgiBundleApplicationContextEvent applicationEvent) {
63          super.multicastEvent(applicationEvent);
64          if (bridgeListener != null)
65              bridgeListener.onOsgiApplicationEvent(applicationEvent);
66      }
67  }