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