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.dom4j.DocumentException;
16  
17  import java.io.FileInputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.util.Set;
21  
22  import static com.google.common.base.Preconditions.checkNotNull;
23  
24  /**
25   * Deploys plugins that consist of an XML descriptor file.
26   *
27   * @since 2.1.0
28   */
29  public final class XmlDynamicPluginFactory extends AbstractPluginFactory
30  {
31      /**
32       * @param application The application key to use to choose modules
33       * @since 3.0
34       */
35      public XmlDynamicPluginFactory(final Application application)
36      {
37          this(Sets.newHashSet(application));
38      }
39  
40      /**
41       * @param applications The application key to use to choose modules
42       * @since 3.0
43       */
44      public XmlDynamicPluginFactory(final Set<Application> applications)
45      {
46          super(new XmlDescriptorParserFactory(), applications);
47      }
48  
49      @Override
50      protected InputStream getDescriptorInputStream(PluginArtifact pluginArtifact)
51      {
52          return pluginArtifact.getInputStream();
53      }
54  
55      @Override
56      protected Predicate<Integer> isValidPluginsVersion()
57      {
58          return Predicates.alwaysTrue();
59      }
60  
61      @Override
62      public String canCreate(PluginArtifact pluginArtifact) throws PluginParseException
63      {
64          try
65          {
66              return super.canCreate(pluginArtifact);
67          }
68          catch (PluginParseException e)
69          {
70              if (e.getCause() instanceof DocumentException)
71              {
72                  log.debug("There was an error parsing the plugin descriptor for '{}'", pluginArtifact);
73                  log.debug("This is most probably because we parsed a jar, and the plugin is not an XML dynamic plugin. See the exception below for confirmation:", e);
74                  return null;
75              }
76              throw e;
77          }
78      }
79  
80      /**
81       * Deploys the plugin artifact
82       *
83       * @param pluginArtifact the plugin artifact to deploy
84       * @param moduleDescriptorFactory The factory for plugin modules
85       * @return The instantiated and populated plugin
86       * @throws PluginParseException If the descriptor cannot be parsed
87       * @since 2.2.0
88       */
89      public Plugin create(final PluginArtifact pluginArtifact, final ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
90      {
91          checkNotNull(pluginArtifact, "The plugin artifact must not be null");
92          checkNotNull(moduleDescriptorFactory, "The module descriptor factory must not be null");
93  
94          InputStream pluginDescriptor = null;
95          try
96          {
97              pluginDescriptor = new FileInputStream(pluginArtifact.toFile());
98              // 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
99              final DescriptorParser parser = descriptorParserFactory.getInstance(pluginDescriptor, applications);
100             return parser.configurePlugin(moduleDescriptorFactory, new XmlDynamicPlugin(pluginArtifact));
101         }
102         catch (final RuntimeException e)
103         {
104             throw new PluginParseException(e);
105         }
106         catch (final IOException e)
107         {
108             throw new PluginParseException(e);
109         }
110         finally
111         {
112             IOUtils.closeQuietly(pluginDescriptor);
113         }
114     }
115 }