View Javadoc
1   package com.atlassian.plugin.osgi.bridge;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.event.PluginEventManager;
5   import com.atlassian.plugin.osgi.bridge.external.PluginRetrievalService;
6   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
7   import org.osgi.framework.BundleActivator;
8   import org.osgi.framework.BundleContext;
9   import org.osgi.framework.ServiceReference;
10  
11  import java.util.Dictionary;
12  import java.util.Hashtable;
13  
14  /**
15   * Registers services for bridging Spring events with the plugin event system
16   *
17   * @since 2.2.0
18   */
19  public class BridgeActivator implements BundleActivator {
20      public void start(BundleContext bundleContext) {
21          // We can do this because the plugin event manager is a host component
22          PluginEventManager pluginEventManager = getHostComponent(bundleContext, PluginEventManager.class);
23  
24          // Register the listener for context refreshed and failed events
25          bundleContext.registerService(
26                  OsgiBundleApplicationContextListener.class.getName(),
27                  new SpringOsgiEventBridge(pluginEventManager),
28                  null);
29  
30          // Register the listener for internal application context events like waiting for dependencies
31          Dictionary<String, String> dict = new Hashtable<>();
32          dict.put("plugin-bridge", "true");
33          bundleContext.registerService(
34                  OsgiBundleApplicationContextListener.class.getName(),
35                  new SpringContextEventBridge(pluginEventManager),
36                  dict);
37  
38          // Register the {@link PluginRetrievalService} service
39          PluginAccessor pluginAccessor = getHostComponent(bundleContext, PluginAccessor.class);
40          bundleContext.registerService(
41                  PluginRetrievalService.class.getName(),
42                  new PluginRetrievalServiceFactory(pluginAccessor),
43                  null);
44      }
45  
46      private <T> T getHostComponent(BundleContext bundleContext, Class<T> componentClass) {
47          ServiceReference ref = bundleContext.getServiceReference(componentClass.getName());
48          if (ref == null) {
49              throw new IllegalStateException("The " + componentClass.getName() + " service must be exported from the application");
50          }
51          //noinspection unchecked
52          return (T) bundleContext.getService(ref);
53      }
54  
55      public void stop(BundleContext bundleContext) {
56      }
57  }