View Javadoc
1   package com.atlassian.plugin.osgi.bridge;
2   
3   import com.atlassian.plugin.event.PluginEventManager;
4   import com.atlassian.plugin.osgi.event.PluginServiceDependencyWaitEndedEvent;
5   import com.atlassian.plugin.osgi.event.PluginServiceDependencyWaitStartingEvent;
6   import com.atlassian.plugin.osgi.event.PluginServiceDependencyWaitTimedOutEvent;
7   import org.eclipse.gemini.blueprint.context.ConfigurableOsgiBundleApplicationContext;
8   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEvent;
9   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
10  import org.eclipse.gemini.blueprint.extender.event.BootstrappingDependencyEvent;
11  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyEvent;
12  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitEndedEvent;
13  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitStartingEvent;
14  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitTimedOutEvent;
15  import org.eclipse.gemini.blueprint.service.importer.support.AbstractOsgiServiceImportFactoryBean;
16  import org.osgi.framework.Bundle;
17  import org.slf4j.Logger;
18  import org.slf4j.LoggerFactory;
19  
20  /**
21   * Bridge for internal spring context events and the plugin framework event system, specifically when the internal
22   * spring context is waiting for OSGi service dependencies.
23   *
24   * @since 2.2.1
25   */
26  public class SpringContextEventBridge implements OsgiBundleApplicationContextListener {
27      private static final Logger log = LoggerFactory.getLogger(SpringContextEventBridge.class);
28  
29      private final PluginEventManager pluginEventManager;
30  
31      public SpringContextEventBridge(PluginEventManager pluginEventManager) {
32          this.pluginEventManager = pluginEventManager;
33      }
34  
35      public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent osgiEvent) {
36          // catch events where a mandatory service waiting period is beginning
37          if (osgiEvent instanceof BootstrappingDependencyEvent) {
38              OsgiServiceDependencyEvent event = ((BootstrappingDependencyEvent) osgiEvent).getDependencyEvent();
39              if (log.isDebugEnabled()) {
40                  log.debug("Handling osgi application context event: " + event);
41              }
42  
43              String beanName = event.getServiceDependency()
44                      .getBeanName();
45              String pluginKey = null;
46  
47              // Unfortunately, the source could really be anything, so let's try the instances that we know of
48              if (event.getSource() != null) {
49                  // maybe the source is an application context
50                  if (event.getSource() instanceof ConfigurableOsgiBundleApplicationContext) {
51                      Bundle bundle = ((ConfigurableOsgiBundleApplicationContext) event.getSource()).getBundle();
52                      pluginKey = PluginBundleUtils.getPluginKey(bundle);
53                  }
54  
55                  // or maybe the source is a factory bean
56                  else {
57                      if (event.getSource() instanceof AbstractOsgiServiceImportFactoryBean) {
58                          AbstractOsgiServiceImportFactoryBean bean = ((AbstractOsgiServiceImportFactoryBean) event.getSource());
59                          if (beanName == null) {
60                              beanName = bean.getBeanName();
61                          }
62                          if (bean.getBundleContext() != null) {
63                              pluginKey = PluginBundleUtils.getPluginKey(bean.getBundleContext().getBundle());
64                          }
65                      }
66                  }
67              }
68  
69              // If the plugin key isn't found, it won't be used to provide useful messages to the plugin framework, so
70              // log this so that we can fix this as we find them.
71              if (pluginKey == null && log.isDebugEnabled()) {
72                  log.debug("Cannot determine the plugin key for event: " + event + " and source: " + event.getSource());
73              }
74              if (event instanceof OsgiServiceDependencyWaitStartingEvent) {
75                  pluginEventManager.broadcast(new PluginServiceDependencyWaitStartingEvent(
76                          pluginKey,
77                          beanName,
78                          event.getServiceDependency().getServiceFilter(),
79                          ((OsgiServiceDependencyWaitStartingEvent) event).getTimeToWait()));
80              } else {
81                  if (event instanceof OsgiServiceDependencyWaitEndedEvent) {
82                      pluginEventManager.broadcast(new PluginServiceDependencyWaitEndedEvent(
83                              pluginKey,
84                              beanName,
85                              event.getServiceDependency().getServiceFilter(),
86                              ((OsgiServiceDependencyWaitEndedEvent) event).getElapsedTime()));
87                  } else {
88                      if (event instanceof OsgiServiceDependencyWaitTimedOutEvent) {
89                          pluginEventManager.broadcast(new PluginServiceDependencyWaitTimedOutEvent(
90                                  pluginKey,
91                                  beanName,
92                                  event.getServiceDependency().getServiceFilter(),
93                                  ((OsgiServiceDependencyWaitTimedOutEvent) event).getElapsedTime()));
94                      }
95                  }
96              }
97          }
98      }
99  }