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 * Create a factory which produces artifacts that optionally allow reference installation.
37 *
38 * @param referenceMode the legacy {@link com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode} whose
39 * modern equivalent {@link ReferenceMode} is passed to {@link JarPluginArtifact} instances created by this factory.
40 * @deprecated since 4.0.0, to be removed in 5.0.0: Use {@link DefaultPluginArtifactFactory(ReferenceMode)} which uses the non
41 * legacy {@link ReferenceMode}.
42 */
43 public DefaultPluginArtifactFactory(final com.atlassian.plugin.PluginArtifact.AllowsReference.ReferenceMode referenceMode) {
44 this(referenceMode.toModern());
45 }
46
47 /**
48 * Creates the artifact by looking at the file extension
49 *
50 * @param artifactUri The artifact URI
51 * @return The created artifact
52 * @throws IllegalArgumentException If an artifact cannot be created from the URL
53 */
54 public PluginArtifact create(URI artifactUri) {
55 PluginArtifact artifact = null;
56
57 String protocol = artifactUri.getScheme();
58
59 if ("file".equalsIgnoreCase(protocol)) {
60 File artifactFile = new File(artifactUri);
61
62 String file = artifactFile.getName();
63 if (file.endsWith(".jar")) {
64 artifact = new JarPluginArtifact(artifactFile, referenceMode);
65 } else if (file.endsWith(".xml")) {
66 artifact = new XmlPluginArtifact(artifactFile);
67 }
68 }
69
70 if (artifact == null) {
71 throw new IllegalArgumentException("The artifact URI " + artifactUri + " is not a valid plugin artifact");
72 }
73
74 return artifact;
75 }
76 }