View Javadoc

1   package com.atlassian.plugin.osgi.bridge;
2   
3   import com.atlassian.plugin.event.PluginEventManager;
4   import com.atlassian.plugin.event.events.PluginContainerFailedEvent;
5   import com.atlassian.plugin.event.events.PluginContainerRefreshedEvent;
6   import org.springframework.osgi.context.event.OsgiBundleApplicationContextEvent;
7   import org.springframework.osgi.context.event.OsgiBundleApplicationContextListener;
8   import org.springframework.osgi.context.event.OsgiBundleContextFailedEvent;
9   import org.springframework.osgi.context.event.OsgiBundleContextRefreshedEvent;
10  import org.osgi.framework.Bundle;
11  import org.osgi.framework.Constants;
12  
13  import java.util.jar.Manifest;
14  
15  /**
16   * Bridges key Spring DM extender events with the plugin system
17   *
18   * @since 2.2.0
19   */
20  public class SpringOsgiEventBridge implements OsgiBundleApplicationContextListener
21  {
22      private final PluginEventManager pluginEventManager;
23  
24      public SpringOsgiEventBridge(PluginEventManager pluginEventManager)
25      {
26          this.pluginEventManager = pluginEventManager;
27      }
28  
29      public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent evt)
30      {
31          if (evt instanceof OsgiBundleContextFailedEvent)
32          {
33              OsgiBundleContextFailedEvent e = (OsgiBundleContextFailedEvent)evt;
34              pluginEventManager.broadcast(new PluginContainerFailedEvent(
35                      e.getApplicationContext(),
36                      getPluginKey(e.getBundle()),
37                      e.getFailureCause()));
38          }
39          else if (evt instanceof OsgiBundleContextRefreshedEvent)
40          {
41              OsgiBundleContextRefreshedEvent e = (OsgiBundleContextRefreshedEvent)evt;
42              pluginEventManager.broadcast(new PluginContainerRefreshedEvent(
43                      e.getApplicationContext(),
44                      getPluginKey(e.getBundle())));
45          }
46      }
47  
48      /**
49       * Gets the plugin key from the bundle
50       *
51       * WARNING: shamelessly copied from {@link com.atlassian.plugin.osgi.util.OsgiHeaderUtil}, which can't be imported
52       * due to creating a cyclic build dependency.  Ensure these two implementations are in sync.
53       *
54       * @param bundle The plugin bundle
55       * @return The plugin key, cannot be null
56       * @since 2.2.0
57       */
58      private static String getPluginKey(Bundle bundle)
59      {
60          return getPluginKey(
61                  bundle.getSymbolicName(),
62                  bundle.getHeaders().get("Atlassian-Plugin-Key"),
63                  bundle.getHeaders().get(Constants.BUNDLE_VERSION)
64          );
65      }
66  
67      private static String getPluginKey(Object bundleName, Object atlKey, Object version)
68      {
69          Object key = atlKey;
70          if (key == null)
71          {
72              key = bundleName + "-" + version;
73          }
74          return key.toString();
75  
76      }
77  }