]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.repo/src/org/argeo/slc/repo/osgi/SubArtifact.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / org.argeo.slc.repo / src / org / argeo / slc / repo / osgi / SubArtifact.java
1 package org.argeo.slc.repo.osgi;
2
3 import java.io.File;
4 import java.util.Map;
5
6 import org.eclipse.aether.artifact.AbstractArtifact;
7 import org.eclipse.aether.artifact.Artifact;
8
9 /**
10 * An artifact whose identity is derived from another artifact. <em>Note:</em>
11 * Instances of this class are immutable and the exposed mutators return new
12 * objects rather than changing the current instance.
13 */
14 final class SubArtifact extends AbstractArtifact {
15
16 private final Artifact mainArtifact;
17
18 private final String classifier;
19
20 private final String extension;
21
22 private final File file;
23
24 private final Map<String, String> properties;
25
26 /**
27 * Creates a new sub artifact. The classifier and extension specified for this
28 * artifact may use the asterisk character "*" to refer to the corresponding
29 * property of the main artifact. For instance, the classifier "*-sources" can
30 * be used to refer to the source attachment of an artifact. Likewise, the
31 * extension "*.asc" can be used to refer to the GPG signature of an artifact.
32 *
33 * @param mainArtifact The artifact from which to derive the identity, must not
34 * be {@code null}.
35 * @param classifier The classifier for this artifact, may be {@code null} if
36 * none.
37 * @param extension The extension for this artifact, may be {@code null} if
38 * none.
39 */
40 public SubArtifact(Artifact mainArtifact, String classifier, String extension) {
41 this(mainArtifact, classifier, extension, (File) null);
42 }
43
44 /**
45 * Creates a new sub artifact. The classifier and extension specified for this
46 * artifact may use the asterisk character "*" to refer to the corresponding
47 * property of the main artifact. For instance, the classifier "*-sources" can
48 * be used to refer to the source attachment of an artifact. Likewise, the
49 * extension "*.asc" can be used to refer to the GPG signature of an artifact.
50 *
51 * @param mainArtifact The artifact from which to derive the identity, must not
52 * be {@code null}.
53 * @param classifier The classifier for this artifact, may be {@code null} if
54 * none.
55 * @param extension The extension for this artifact, may be {@code null} if
56 * none.
57 * @param file The file for this artifact, may be {@code null} if
58 * unresolved.
59 */
60 public SubArtifact(Artifact mainArtifact, String classifier, String extension, File file) {
61 this(mainArtifact, classifier, extension, null, file);
62 }
63
64 /**
65 * Creates a new sub artifact. The classifier and extension specified for this
66 * artifact may use the asterisk character "*" to refer to the corresponding
67 * property of the main artifact. For instance, the classifier "*-sources" can
68 * be used to refer to the source attachment of an artifact. Likewise, the
69 * extension "*.asc" can be used to refer to the GPG signature of an artifact.
70 *
71 * @param mainArtifact The artifact from which to derive the identity, must not
72 * be {@code null}.
73 * @param classifier The classifier for this artifact, may be {@code null} if
74 * none.
75 * @param extension The extension for this artifact, may be {@code null} if
76 * none.
77 * @param properties The properties of the artifact, may be {@code null}.
78 */
79 public SubArtifact(Artifact mainArtifact, String classifier, String extension, Map<String, String> properties) {
80 this(mainArtifact, classifier, extension, properties, null);
81 }
82
83 /**
84 * Creates a new sub artifact. The classifier and extension specified for this
85 * artifact may use the asterisk character "*" to refer to the corresponding
86 * property of the main artifact. For instance, the classifier "*-sources" can
87 * be used to refer to the source attachment of an artifact. Likewise, the
88 * extension "*.asc" can be used to refer to the GPG signature of an artifact.
89 *
90 * @param mainArtifact The artifact from which to derive the identity, must not
91 * be {@code null}.
92 * @param classifier The classifier for this artifact, may be {@code null} if
93 * none.
94 * @param extension The extension for this artifact, may be {@code null} if
95 * none.
96 * @param properties The properties of the artifact, may be {@code null}.
97 * @param file The file for this artifact, may be {@code null} if
98 * unresolved.
99 */
100 public SubArtifact(Artifact mainArtifact, String classifier, String extension, Map<String, String> properties,
101 File file) {
102 if (mainArtifact == null) {
103 throw new IllegalArgumentException("no artifact specified");
104 }
105 this.mainArtifact = mainArtifact;
106 this.classifier = classifier;
107 this.extension = extension;
108 this.file = file;
109 this.properties = copyProperties(properties);
110 }
111
112 private SubArtifact(Artifact mainArtifact, String classifier, String extension, File file,
113 Map<String, String> properties) {
114 // NOTE: This constructor assumes immutability of the provided properties, for
115 // internal use only
116 this.mainArtifact = mainArtifact;
117 this.classifier = classifier;
118 this.extension = extension;
119 this.file = file;
120 this.properties = properties;
121 }
122
123 public String getGroupId() {
124 return mainArtifact.getGroupId();
125 }
126
127 public String getArtifactId() {
128 return mainArtifact.getArtifactId();
129 }
130
131 public String getVersion() {
132 return mainArtifact.getVersion();
133 }
134
135 public String getBaseVersion() {
136 return mainArtifact.getBaseVersion();
137 }
138
139 public boolean isSnapshot() {
140 return mainArtifact.isSnapshot();
141 }
142
143 public String getClassifier() {
144 return expand(classifier, mainArtifact.getClassifier());
145 }
146
147 public String getExtension() {
148 return expand(extension, mainArtifact.getExtension());
149 }
150
151 public File getFile() {
152 return file;
153 }
154
155 public Artifact setFile(File file) {
156 if ((this.file == null) ? file == null : this.file.equals(file)) {
157 return this;
158 }
159 return new SubArtifact(mainArtifact, classifier, extension, file, properties);
160 }
161
162 public Map<String, String> getProperties() {
163 return properties;
164 }
165
166 public Artifact setProperties(Map<String, String> properties) {
167 if (this.properties.equals(properties) || (properties == null && this.properties.isEmpty())) {
168 return this;
169 }
170 return new SubArtifact(mainArtifact, classifier, extension, properties, file);
171 }
172
173 private static String expand(String pattern, String replacement) {
174 String result = "";
175 if (pattern != null) {
176 result = pattern.replace("*", replacement);
177
178 if (replacement.length() <= 0) {
179 if (pattern.startsWith("*")) {
180 int i = 0;
181 for (; i < result.length(); i++) {
182 char c = result.charAt(i);
183 if (c != '-' && c != '.') {
184 break;
185 }
186 }
187 result = result.substring(i);
188 }
189 if (pattern.endsWith("*")) {
190 int i = result.length() - 1;
191 for (; i >= 0; i--) {
192 char c = result.charAt(i);
193 if (c != '-' && c != '.') {
194 break;
195 }
196 }
197 result = result.substring(0, i + 1);
198 }
199 }
200 }
201 return result;
202 }
203
204 }