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