View Javadoc

1   package com.atlassian.plugin;
2   
3   import java.io.File;
4   import java.net.URI;
5   
6   import static com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode;
7   
8   /**
9    * Creates plugin artifacts by handling URI's that are files and looking at the file's extension
10   *
11   * @since 2.1.0
12   */
13  public class DefaultPluginArtifactFactory implements PluginArtifactFactory
14  {
15      /**
16       * The {@link ReferenceMode} passed to {@link JarPluginArtifact} instances created by this
17       * factory.
18       */
19      final ReferenceMode referenceMode;
20  
21      /**
22       * Create a factory which produces artifacts that do not allow reference installation.
23       */
24      public DefaultPluginArtifactFactory()
25      {
26          this(ReferenceMode.FORBID_REFERENCE);
27      }
28  
29      /**
30       * Create a factory which produces artifacts that optionally allow reference installation.
31       *
32       * @param referenceMode the {@link ReferenceMode} passed to {@link JarPluginArtifact} instances
33       * created by this factory.
34       */
35      public DefaultPluginArtifactFactory(ReferenceMode referenceMode)
36      {
37          this.referenceMode = referenceMode;
38      }
39  
40      /**
41       * Creates the artifact by looking at the file extension
42       *
43       * @param artifactUri The artifact URI
44       * @return The created artifact
45       * @throws IllegalArgumentException If an artifact cannot be created from the URL
46       */
47      public PluginArtifact create(URI artifactUri)
48      {
49          PluginArtifact artifact = null;
50  
51          String protocol = artifactUri.getScheme();
52  
53          if ("file".equalsIgnoreCase(protocol))
54          {
55              File artifactFile = new File(artifactUri);
56             
57              String file = artifactFile.getName();
58              if (file.endsWith(".jar"))
59              {
60                  artifact = new JarPluginArtifact(artifactFile, referenceMode);
61              }
62              else if (file.endsWith(".xml"))
63              {
64                  artifact = new XmlPluginArtifact(artifactFile);
65              }
66          }
67  
68          if (artifact == null)
69          {
70              throw new IllegalArgumentException("The artifact URI " + artifactUri + " is not a valid plugin artifact");
71          }
72  
73          return artifact;
74      }
75  }