View Javadoc

1   package com.atlassian.plugin.osgi.factory;
2   
3   import com.atlassian.plugin.Application;
4   import com.atlassian.plugin.ModuleDescriptorFactory;
5   import com.atlassian.plugin.Plugin;
6   import com.atlassian.plugin.PluginArtifact;
7   import com.atlassian.plugin.PluginParseException;
8   import com.atlassian.plugin.factories.AbstractPluginFactory;
9   import com.atlassian.plugin.impl.UnloadablePlugin;
10  import com.atlassian.plugin.parsers.DescriptorParser;
11  import com.atlassian.plugin.parsers.XmlDescriptorParserFactory;
12  import com.google.common.base.Predicate;
13  import com.google.common.collect.ImmutableSet;
14  import com.google.common.collect.Ranges;
15  import org.apache.commons.io.IOUtils;
16  
17  import java.io.InputStream;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  /**
22   * Creates unloadable plugins from static plugins.  Used to handle when a static plugin (version 1) is deployed
23   * to a directory that only accepts OSGi plugins.  This should be placed last in the chain of plugin factories and
24   * only if {@link com.atlassian.plugin.factories.LegacyDynamicPluginFactory} is not used.
25   *
26   * @since 2.2.3
27   */
28  public final class UnloadableStaticPluginFactory extends AbstractPluginFactory
29  {
30      private final String pluginDescriptorFileName;
31  
32      public UnloadableStaticPluginFactory(String pluginDescriptorFileName)
33      {
34          super(new XmlDescriptorParserFactory(), ImmutableSet.<Application>of());
35          this.pluginDescriptorFileName = pluginDescriptorFileName;
36      }
37  
38      @Override
39      protected InputStream getDescriptorInputStream(PluginArtifact pluginArtifact)
40      {
41          return pluginArtifact.getResourceAsStream(pluginDescriptorFileName);
42      }
43  
44      @Override
45      protected Predicate<Integer> isValidPluginsVersion()
46      {
47          return Ranges.singleton(Plugin.VERSION_1);
48      }
49  
50      /**
51       * Creates an unloadable plugin
52       *
53       * @param pluginArtifact          the plugin artifact to deploy
54       * @param moduleDescriptorFactory The factory for plugin modules
55       * @return The instantiated and populated plugin
56       * @throws PluginParseException If the descriptor cannot be parsed
57       */
58      public Plugin create(PluginArtifact pluginArtifact, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
59      {
60          checkNotNull(pluginArtifact, "The plugin deployment unit is required");
61          checkNotNull(moduleDescriptorFactory, "The module descriptor factory is required");
62  
63          UnloadablePlugin plugin = null;
64          InputStream pluginDescriptor = null;
65          try
66          {
67              pluginDescriptor = pluginArtifact.getResourceAsStream(pluginDescriptorFileName);
68              if (pluginDescriptor == null)
69              {
70                  throw new PluginParseException("No descriptor found in classloader for : " + pluginArtifact);
71              }
72  
73              DescriptorParser parser = descriptorParserFactory.getInstance(pluginDescriptor, ImmutableSet.<Application>of());
74  
75              plugin = new UnloadablePlugin();
76              // This should be a valid plugin, it just got put in the wrong directory.
77              // We'll try to do a full configure because it looks more user-friendly.
78              try
79              {
80                  parser.configurePlugin(moduleDescriptorFactory, plugin);
81              }
82              catch (Exception ex)
83              {
84                  // Error on full configure - we'll just set the key as this is an UnloadablePlugin anyway.
85                  plugin.setKey(parser.getKey());   
86              }
87              plugin.setErrorText("Unable to load the static '" + pluginArtifact + "' plugin from the plugins directory.  Please " +
88                                  "copy this file into WEB-INF/lib and restart.");
89          }
90          finally
91          {
92              IOUtils.closeQuietly(pluginDescriptor);
93          }
94          return plugin;
95      }
96  }