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 * The {@link ReferenceMode} passed to {@link JarPluginArtifact} instances created by this
14 * factory.
15 */
16 final ReferenceMode referenceMode;
17
18 /**
19 * Create a factory which produces artifacts that do not allow reference installation.
20 */
21 public DefaultPluginArtifactFactory() {
22 this(ReferenceMode.FORBID_REFERENCE);
23 }
24
25 /**
26 * Create a factory which produces artifacts that optionally allow reference installation.
27 *
28 * @param referenceMode the {@link ReferenceMode} passed to {@link JarPluginArtifact} instances
29 * created by this factory.
30 */
31 public DefaultPluginArtifactFactory(final ReferenceMode referenceMode) {
32 this.referenceMode = referenceMode;
33 }
34
35 /**
36 * Creates the artifact by looking at the file extension
37 *
38 * @param artifactUri The artifact URI
39 * @return The created artifact
40 * @throws IllegalArgumentException If an artifact cannot be created from the URL
41 */
42 public PluginArtifact create(URI artifactUri) {
43 PluginArtifact artifact = null;
44
45 String protocol = artifactUri.getScheme();
46
47 if ("file".equalsIgnoreCase(protocol)) {
48 File artifactFile = new File(artifactUri);
49
50 String file = artifactFile.getName();
51 if (file.endsWith(".jar")) {
52 artifact = new JarPluginArtifact(artifactFile, referenceMode);
53 } else if (file.endsWith(".xml")) {
54 artifact = new XmlPluginArtifact(artifactFile);
55 }
56 }
57
58 if (artifact == null) {
59 throw new IllegalArgumentException("The artifact URI " + artifactUri + " is not a valid plugin artifact");
60 }
61
62 return artifact;
63 }
64 }