View Javadoc

1   package com.atlassian.plugin.loaders;
2   
3   import com.atlassian.plugin.ModuleDescriptorFactory;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginParseException;
6   import com.atlassian.plugin.event.PluginEventManager;
7   import com.atlassian.plugin.factories.PluginFactory;
8   import com.atlassian.plugin.impl.DynamicPlugin;
9   import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
10  import com.atlassian.plugin.util.FileUtils;
11  
12  import org.apache.commons.logging.Log;
13  import org.apache.commons.logging.LogFactory;
14  
15  import java.io.File;
16  import java.net.URL;
17  import java.util.List;
18  
19  /**
20   * Plugin loader that unzips plugins from a zip file into a local directory, and ensures that directory only contains
21   * plugins from that zip file.  It also treats all plugins loaded from the directory as bundled plugins, meaning they
22   * can can be upgraded, but not deleted.
23   */
24  public class BundledPluginLoader extends DirectoryPluginLoader
25  {
26  
27      private static final Log log = LogFactory.getLog(BundledPluginLoader.class);
28  
29      public BundledPluginLoader(final URL zipUrl, final File pluginPath, final List<PluginFactory> pluginFactories, final PluginEventManager eventManager)
30      {
31          super(pluginPath, pluginFactories, eventManager);
32          if (zipUrl == null)
33          {
34              throw new IllegalArgumentException("Bundled zip url cannot be null");
35          }
36          FileUtils.conditionallyExtractZipFile(zipUrl, pluginPath);
37      }
38  
39      /**
40       * Just like the {@link DirectoryPluginLoader#deployPluginFromUnit(DeploymentUnit,ModuleDescriptorFactory)} method
41       * but changes the plugin to not be deletable
42       * @param deploymentUnit The plugin to deploy
43       * @param moduleDescriptorFactory The descriptor factory
44       * @return The created plugin
45       * @throws PluginParseException If there is a problem parsing the configuration
46       */
47      @Override
48      protected Plugin deployPluginFromUnit(final DeploymentUnit deploymentUnit, final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
49      {
50          final Plugin plugin = super.deployPluginFromUnit(deploymentUnit, moduleDescriptorFactory);
51          if (plugin instanceof DynamicPlugin)
52          {
53              final DynamicPlugin dplugin = (DynamicPlugin) plugin;
54              dplugin.setDeletable(false);
55              dplugin.setBundled(true);
56          }
57  
58          if (log.isDebugEnabled())
59          {
60              log.debug("Deployed bundled plugin: " + plugin.getName());
61          }
62          return plugin;
63      }
64  }