View Javadoc

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