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.osgi.framework.BundleActivator;
7   import org.osgi.framework.BundleContext;
8   import org.osgi.framework.ServiceReference;
9   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
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  {
21      public void start(BundleContext bundleContext) throws Exception
22      {
23          // We can do this because the plugin event manager is a host component
24          PluginEventManager pluginEventManager = getHostComponent(bundleContext, PluginEventManager.class);
25  
26          // Register the listener for context refreshed and failed events
27          bundleContext.registerService(
28                  OsgiBundleApplicationContextListener.class.getName(),
29                  new SpringOsgiEventBridge(pluginEventManager),
30                  null);
31  
32          // Register the listener for internal application context events like waiting for dependencies
33          Dictionary<String, String> dict = new Hashtable<String, String>();
34          dict.put("plugin-bridge", "true");
35          bundleContext.registerService(
36                  OsgiBundleApplicationContextListener.class.getName(),
37                  new SpringContextEventBridge(pluginEventManager),
38                  dict);
39  
40          // Register the {@link PluginRetrievalService} service
41          PluginAccessor pluginAccessor = getHostComponent(bundleContext, PluginAccessor.class);
42          bundleContext.registerService(
43                  PluginRetrievalService.class.getName(),
44                  new PluginRetrievalServiceFactory(pluginAccessor),
45                  null);
46      }
47  
48      private <T> T getHostComponent(BundleContext bundleContext, Class<T> componentClass)
49      {
50          ServiceReference ref = bundleContext.getServiceReference(componentClass.getName());
51          if (ref == null)
52          {
53              throw new IllegalStateException("The " + componentClass.getName() + " service must be exported from the application");
54          }
55          return (T) bundleContext.getService(ref);
56      }
57  
58      public void stop(BundleContext bundleContext) throws Exception
59      {
60      }
61  }