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.osgi.framework.Bundle;
8   import org.eclipse.gemini.blueprint.context.ConfigurableOsgiBundleApplicationContext;
9   import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextEvent;
10  import org.eclipse.gemini.blueprint.context.event.OsgiBundleApplicationContextListener;
11  import org.eclipse.gemini.blueprint.extender.event.BootstrappingDependencyEvent;
12  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyEvent;
13  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitEndedEvent;
14  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitStartingEvent;
15  import org.eclipse.gemini.blueprint.service.importer.event.OsgiServiceDependencyWaitTimedOutEvent;
16  import org.eclipse.gemini.blueprint.service.importer.support.AbstractOsgiServiceImportFactoryBean;
17  import org.slf4j.LoggerFactory;
18  import org.slf4j.Logger;
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  {
28      private static final Logger log = LoggerFactory.getLogger(SpringContextEventBridge.class);
29  
30      private final PluginEventManager pluginEventManager;
31  
32      public SpringContextEventBridge(PluginEventManager pluginEventManager)
33      {
34          this.pluginEventManager = pluginEventManager;
35      }
36  
37      public void onOsgiApplicationEvent(OsgiBundleApplicationContextEvent osgiEvent)
38      {
39          // catch events where a manditory service waiting period is beginning
40          if (osgiEvent instanceof BootstrappingDependencyEvent)
41          {
42              OsgiServiceDependencyEvent event = ((BootstrappingDependencyEvent) osgiEvent).getDependencyEvent();
43              if (log.isDebugEnabled())
44              {
45                  log.debug("Handling osgi application context event: " + event);
46              }
47  
48              String beanName = event.getServiceDependency()
49                      .getBeanName();
50              String pluginKey = null;
51  
52              // Unfortunately, the source could really be anything, so let's try the instances that we know of
53              if (event.getSource() != null)
54              {
55                  // maybe the source is an application context
56                  if (event.getSource() instanceof ConfigurableOsgiBundleApplicationContext)
57                  {
58                      Bundle bundle = ((ConfigurableOsgiBundleApplicationContext) event.getSource()).getBundle();
59                      pluginKey = PluginBundleUtils.getPluginKey(bundle);
60                  }
61  
62                  // or maybe the source is a factory bean
63                  else
64                  {
65                      if (event.getSource() instanceof AbstractOsgiServiceImportFactoryBean)
66                      {
67                          AbstractOsgiServiceImportFactoryBean bean = ((AbstractOsgiServiceImportFactoryBean) event.getSource());
68                          if (beanName == null)
69                          {
70                              beanName = bean.getBeanName();
71                          }
72                          if (bean.getBundleContext() != null)
73                          {
74                              pluginKey = PluginBundleUtils.getPluginKey(bean.getBundleContext().getBundle());
75                          }
76                      }
77                  }
78              }
79  
80              // If the plugin key isn't found, it won't be used to provide useful messages to the plugin framework, so
81              // log this so that we can fix this as we find them.
82              if (pluginKey == null && log.isDebugEnabled())
83              {
84                  log.debug("Cannot determine the plugin key for event: " + event + " and source: " + event.getSource());
85              }
86              if (event instanceof OsgiServiceDependencyWaitStartingEvent)
87              {
88                  pluginEventManager.broadcast(new PluginServiceDependencyWaitStartingEvent(
89                          pluginKey,
90                          beanName,
91                          event.getServiceDependency().getServiceFilter(),
92                          ((OsgiServiceDependencyWaitStartingEvent) event).getTimeToWait()));
93              }
94              else
95              {
96                  if (event instanceof OsgiServiceDependencyWaitEndedEvent)
97                  {
98                      pluginEventManager.broadcast(new PluginServiceDependencyWaitEndedEvent(
99                              pluginKey,
100                             beanName,
101                             event.getServiceDependency().getServiceFilter(),
102                             ((OsgiServiceDependencyWaitEndedEvent) event).getElapsedTime()));
103                 }
104                 else
105                 {
106                     if (event instanceof OsgiServiceDependencyWaitTimedOutEvent)
107                     {
108                         pluginEventManager.broadcast(new PluginServiceDependencyWaitTimedOutEvent(
109                                 pluginKey,
110                                 beanName,
111                                 event.getServiceDependency().getServiceFilter(),
112                                 ((OsgiServiceDependencyWaitTimedOutEvent) event).getElapsedTime()));
113                     }
114                 }
115             }
116         }
117     }
118 }