View Javadoc

1   package com.atlassian.plugin.factories;
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.impl.XmlDynamicPlugin;
9   import com.atlassian.plugin.parsers.DescriptorParser;
10  import com.atlassian.plugin.parsers.XmlDescriptorParserFactory;
11  import com.google.common.base.Predicate;
12  import com.google.common.base.Predicates;
13  import com.google.common.collect.Sets;
14  import org.apache.commons.io.IOUtils;
15  import org.apache.commons.lang.Validate;
16  
17  import java.io.FileInputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Set;
21  
22  /**
23   * Deploys plugins that consist of an XML descriptor file.
24   *
25   * @since 2.1.0
26   */
27  public final class XmlDynamicPluginFactory extends AbstractPluginFactory
28  {
29      /**
30       * @param application The application key to use to choose modules
31       * @since 2.14
32       */
33      public XmlDynamicPluginFactory(final Application application)
34      {
35          this(Sets.newHashSet(application));
36      }
37  
38      /**
39       * @param applications The application key to use to choose modules
40       * @since 2.14
41       */
42      public XmlDynamicPluginFactory(final Set<Application> applications)
43      {
44          super(new XmlDescriptorParserFactory(), applications);
45      }
46  
47      @Override
48      protected InputStream getDescriptorInputStream(PluginArtifact pluginArtifact)
49      {
50          return pluginArtifact.getInputStream();
51      }
52  
53      @Override
54      protected Predicate<Integer> isValidPluginsVersion()
55      {
56          return Predicates.alwaysTrue();
57      }
58  
59      /**
60       * Deploys the plugin artifact
61       *
62       * @param pluginArtifact the plugin artifact to deploy
63       * @param moduleDescriptorFactory The factory for plugin modules
64       * @return The instantiated and populated plugin
65       * @throws PluginParseException If the descriptor cannot be parsed
66       * @since 2.2.0
67       */
68      public Plugin create(final PluginArtifact pluginArtifact, final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
69      {
70          Validate.notNull(pluginArtifact, "The plugin artifact must not be null");
71          Validate.notNull(moduleDescriptorFactory, "The module descriptor factory must not be null");
72  
73          InputStream pluginDescriptor = null;
74          try
75          {
76              pluginDescriptor = new FileInputStream(pluginArtifact.toFile());
77              // The plugin we get back may not be the same (in the case of an UnloadablePlugin), so add what gets returned, rather than the original
78              final DescriptorParser parser = descriptorParserFactory.getInstance(pluginDescriptor, applicationsAsArray());
79              return parser.configurePlugin(moduleDescriptorFactory, new XmlDynamicPlugin(pluginArtifact));
80          }
81          catch (final RuntimeException e)
82          {
83              throw new PluginParseException(e);
84          }
85          catch (final IOException e)
86          {
87              throw new PluginParseException();
88          }
89          finally
90          {
91              IOUtils.closeQuietly(pluginDescriptor);
92          }
93      }
94  
95      private Application[] applicationsAsArray()
96      {
97          return applications.toArray(new Application[applications.size()]);
98      }
99  }