View Javadoc

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