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