View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.event.PluginEventManager;
5   import com.atlassian.plugin.event.events.PluginModuleAvailableEvent;
6   import com.atlassian.plugin.event.events.PluginModuleUnavailableEvent;
7   import org.apache.commons.lang.Validate;
8   import org.osgi.framework.Bundle;
9   import org.osgi.framework.ServiceReference;
10  import org.osgi.util.tracker.ServiceTrackerCustomizer;
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  /**
15   * Tracks module descriptors registered as services, then updates the descriptors map accordingly
16   *
17   * @since 2.2.0
18   */
19  class ModuleDescriptorServiceTrackerCustomizer implements ServiceTrackerCustomizer
20  {
21      private static final Logger log = LoggerFactory.getLogger(ModuleDescriptorServiceTrackerCustomizer.class);
22  
23      private final Bundle bundle;
24      private final OsgiPlugin plugin;
25      private final PluginEventManager pluginEventManager;
26  
27      public ModuleDescriptorServiceTrackerCustomizer(OsgiPlugin plugin, PluginEventManager pluginEventManager)
28      {
29          this.pluginEventManager = pluginEventManager;
30          Validate.notNull(plugin);
31          this.bundle = plugin.getBundle();
32          Validate.notNull(bundle);
33          this.plugin = plugin;
34      }
35  
36      public Object addingService(final ServiceReference serviceReference)
37      {
38          ModuleDescriptor<?> descriptor = null;
39          if (serviceReference.getBundle() == bundle)
40          {
41              descriptor = (ModuleDescriptor<?>) bundle.getBundleContext().getService(serviceReference);
42              plugin.addModuleDescriptor(descriptor);
43              if (log.isInfoEnabled())
44              {
45                  log.info("Dynamically registered new module descriptor: " + descriptor.getCompleteKey());
46              }
47              pluginEventManager.broadcast(new PluginModuleAvailableEvent(descriptor));
48          }
49          return descriptor;
50      }
51  
52      public void modifiedService(final ServiceReference serviceReference, final Object o)
53      {
54          // Don't bother doing anything as it only represents a change in properties
55      }
56  
57      public void removedService(final ServiceReference serviceReference, final Object o)
58      {
59          if (serviceReference.getBundle() == bundle)
60          {
61              final ModuleDescriptor<?> descriptor = (ModuleDescriptor<?>) o;
62              plugin.clearModuleDescriptor(descriptor.getKey());
63              if (log.isInfoEnabled())
64              {
65                  log.info("Dynamically removed module descriptor: " + descriptor.getCompleteKey());
66              }
67              pluginEventManager.broadcast(new PluginModuleUnavailableEvent(descriptor));
68          }
69      }
70  }