View Javadoc

1   package com.atlassian.plugin.osgi.loader;
2   
3   import com.atlassian.plugin.loaders.PluginFactory;
4   import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
5   import com.atlassian.plugin.osgi.container.OsgiContainerManager;
6   import com.atlassian.plugin.osgi.hostcomponents.HostComponentProvider;
7   import com.atlassian.plugin.Plugin;
8   import com.atlassian.plugin.ModuleDescriptorFactory;
9   import com.atlassian.plugin.PluginParseException;
10  import com.atlassian.plugin.util.FileUtils;
11  import com.atlassian.plugin.util.ClassLoaderUtils;
12  import com.atlassian.plugin.util.zip.UrlUnzipper;
13  import com.atlassian.plugin.impl.DynamicPlugin;
14  
15  import java.io.File;
16  import java.io.IOException;
17  import java.io.FilenameFilter;
18  import java.net.URL;
19  import java.util.List;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.zip.ZipEntry;
23  
24  import org.apache.log4j.Logger;
25  
26  /**
27   * Version of the osgi plugin loader that ensures plugins loaded will not be deletable.  Also provides an alternative
28   * constructor that will handle unzipping the bundled plugins.
29   */
30  public class BundledOsgiPluginLoader extends OsgiPluginLoader
31  {
32      private static final Logger log = Logger.getLogger(BundledOsgiPluginLoader.class);
33  
34      /**
35       * Constructs the loader, but also unzips the bundled plugins to the specified plugin directory
36       *
37       * @param zipUrl The url to the zip of bundled plugins
38       * @param pluginPath The directory that should contain the unzipped bundled plugins
39       * @param pluginDescriptorFileName The plugin descriptor name, i.e. atlassian-plugins.xml
40       * @param pluginFactory The factory to create the plugins
41       * @param osgi The osgi container manager
42       * @param provider The initial host component provider to use when starting the osgi container (if not already started)
43       */
44      public BundledOsgiPluginLoader(URL zipUrl, File pluginPath, String pluginDescriptorFileName, PluginFactory pluginFactory, OsgiContainerManager osgi, HostComponentProvider provider)
45      {
46          super(pluginPath, pluginDescriptorFileName, pluginFactory, osgi, provider);
47          if (zipUrl == null) 
48              throw new IllegalArgumentException("Bundled zip url cannot be null");
49          FileUtils.conditionallyExtractZipFile(zipUrl, pluginPath);
50      }
51  
52      public BundledOsgiPluginLoader(File pluginPath, String pluginDescriptorFileName, PluginFactory pluginFactory, OsgiContainerManager osgi, HostComponentProvider provider)
53      {
54          super(pluginPath, pluginDescriptorFileName, pluginFactory, osgi, provider);
55      }
56  
57      /**
58       * Override the standard plugin loading in order to set isDeleteable = false because we don't ever want to delete
59       * a bundled plugin, evern though we do want to dynamically replace them.
60       *
61       * @param deploymentUnit
62       * @param moduleDescriptorFactory
63       * @return The new DynamicPlugin with isDeleteable set to false;
64       * @throws com.atlassian.plugin.PluginParseException
65       */
66      protected Plugin deployPluginFromUnit(DeploymentUnit deploymentUnit, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
67      {
68          Plugin plugin = super.deployPluginFromUnit(deploymentUnit, moduleDescriptorFactory);
69          if (plugin instanceof DynamicPlugin)
70          {
71              DynamicPlugin dplugin = (DynamicPlugin) plugin; 
72              dplugin.setDeletable(false);
73              dplugin.setBundled(true);
74          }
75  
76          log.debug("Deploy bundled plugin: "+plugin.getName());
77  
78          return plugin;
79      }
80  }