View Javadoc

1   package com.atlassian.plugin.factories;
2   
3   import com.atlassian.plugin.*;
4   import com.atlassian.plugin.impl.XmlDynamicPlugin;
5   import com.atlassian.plugin.loaders.classloading.DeploymentUnit;
6   import com.atlassian.plugin.parsers.DescriptorParser;
7   import com.atlassian.plugin.parsers.DescriptorParserFactory;
8   import com.atlassian.plugin.parsers.XmlDescriptorParserFactory;
9   import org.apache.commons.io.IOUtils;
10  import org.apache.commons.lang.Validate;
11  import org.dom4j.DocumentException;
12  
13  import java.io.FileInputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  
17  /**
18   * Deploys plugins that consist of an XML descriptor file.
19   *
20   * @since 2.1.0
21   */
22  public class XmlDynamicPluginFactory implements PluginFactory
23  {
24      private DescriptorParserFactory descriptorParserFactory;
25  
26      public XmlDynamicPluginFactory()
27      {
28          this.descriptorParserFactory = new XmlDescriptorParserFactory();
29      }
30  
31      /**
32       * @deprecated Since 2.2.0, use {@link #create(PluginArtifact,ModuleDescriptorFactory)} instead
33       */
34      public Plugin create(DeploymentUnit deploymentUnit, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
35      {
36          return create(new XmlPluginArtifact(deploymentUnit.getPath()), moduleDescriptorFactory);
37      }
38  
39      /**
40       * Deploys the plugin artifact
41       * @param pluginArtifact the plugin artifact to deploy
42       * @param moduleDescriptorFactory The factory for plugin modules
43       * @return The instantiated and populated plugin
44       * @throws PluginParseException If the descriptor cannot be parsed
45       * @since 2.2.0
46       */
47      public Plugin create(PluginArtifact pluginArtifact, ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
48      {
49          Validate.notNull(pluginArtifact, "The deployment unit must not be null");
50          Validate.notNull(moduleDescriptorFactory, "The module descriptor factory must not be null");
51  
52          Plugin plugin = null;
53          InputStream pluginDescriptor = null;
54          try
55          {
56              pluginDescriptor = new FileInputStream(pluginArtifact.toFile());
57              // 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
58              DescriptorParser parser = descriptorParserFactory.getInstance(pluginDescriptor);
59              plugin = parser.configurePlugin(moduleDescriptorFactory, new XmlDynamicPlugin());
60          }
61          catch (RuntimeException e)
62          {
63              throw new PluginParseException(e);
64          }
65          catch (IOException e)
66          {
67              throw new PluginParseException();
68          } finally
69          {
70              IOUtils.closeQuietly(pluginDescriptor);
71          }
72          return plugin;
73      }
74  
75      /**
76       * Determines if this deployer can handle this artifact by looking for the plugin descriptor
77       *
78       * @param pluginArtifact The artifact to test
79       * @return The plugin key, null if it cannot load the plugin
80       * @throws com.atlassian.plugin.PluginParseException If there are exceptions parsing the plugin configuration
81       */
82      public String canCreate(PluginArtifact pluginArtifact) throws PluginParseException
83      {
84          Validate.notNull(pluginArtifact, "The plugin artifact must not be null");
85          String pluginKey = null;
86          final InputStream descriptorStream = pluginArtifact.getInputStream();
87          if (descriptorStream != null)
88          {
89              try
90              {
91                  final DescriptorParser descriptorParser = descriptorParserFactory.getInstance(descriptorStream);
92                  pluginKey = descriptorParser.getKey();
93              } catch (PluginParseException ex)
94              {
95                  if (!(ex.getCause() instanceof DocumentException))
96                      throw ex;
97              } finally
98              {
99                  IOUtils.closeQuietly(descriptorStream);
100             }
101         }
102         return pluginKey;
103     }
104 }