View Javadoc

1   package com.atlassian.plugin;
2   
3   import java.io.File;
4   import java.net.URISyntaxException;
5   import java.net.URL;
6   
7   /**
8    * Creates plugin artifacts by handling URL's that are files and looking at the file's extension
9    *
10   * @since 2.1.0
11   */
12  public class DefaultPluginArtifactFactory implements PluginArtifactFactory
13  {
14      /**
15       * Creates the artifact by looking at the file extension
16       *
17       * @param artifactUrl The artifact URL
18       * @return The created artifact
19       * @throws IllegalArgumentException If an artifact cannot be created from the URL
20       */
21      public PluginArtifact create(URL artifactUrl)
22      {
23          PluginArtifact artifact = null;
24          String protocol = artifactUrl.getProtocol();
25          if ("file".equalsIgnoreCase(protocol))
26          {
27              File artifactFile;
28              try
29              {
30                  artifactFile = new File(artifactUrl.toURI());
31              } catch (URISyntaxException e)
32              {
33                  throw new IllegalArgumentException("Invalid artifact URI: "+artifactUrl, e);
34              }
35              String file = artifactUrl.getFile();
36              if (file.endsWith(".jar"))
37                  artifact = new JarPluginArtifact(artifactFile);
38              else if (file.endsWith(".xml"))
39                  artifact = new XmlPluginArtifact(artifactFile);
40          }
41  
42          if (artifact == null)
43              throw new IllegalArgumentException("The artifact URI "+artifactUrl+" is not a valid plugin artifact");
44          
45          return artifact;
46      }
47  }