View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.util.Map;
24  import java.util.Objects;
25  
26  import org.eclipse.aether.artifact.AbstractArtifact;
27  import org.eclipse.aether.artifact.Artifact;
28  
29  /**
30   */
31  public final class RelocatedArtifact extends AbstractArtifact {
32  
33      private final Artifact artifact;
34  
35      private final String groupId;
36  
37      private final String artifactId;
38  
39      private final String classifier;
40  
41      private final String extension;
42  
43      private final String version;
44  
45      private final String message;
46  
47      public RelocatedArtifact(
48              Artifact artifact,
49              String groupId,
50              String artifactId,
51              String classifier,
52              String extension,
53              String version,
54              String message) {
55          this.artifact = Objects.requireNonNull(artifact, "artifact cannot be null");
56          this.groupId = (groupId != null && !groupId.isEmpty()) ? groupId : null;
57          this.artifactId = (artifactId != null && !artifactId.isEmpty()) ? artifactId : null;
58          this.classifier = (classifier != null && !classifier.isEmpty()) ? classifier : null;
59          this.extension = (extension != null && !extension.isEmpty()) ? extension : null;
60          this.version = (version != null && !version.isEmpty()) ? version : null;
61          this.message = (message != null && !message.isEmpty()) ? message : null;
62      }
63  
64      @Override
65      public String getGroupId() {
66          if (groupId != null) {
67              return groupId;
68          } else {
69              return artifact.getGroupId();
70          }
71      }
72  
73      @Override
74      public String getArtifactId() {
75          if (artifactId != null) {
76              return artifactId;
77          } else {
78              return artifact.getArtifactId();
79          }
80      }
81  
82      @Override
83      public String getClassifier() {
84          if (classifier != null) {
85              return classifier;
86          } else {
87              return artifact.getClassifier();
88          }
89      }
90  
91      @Override
92      public String getExtension() {
93          if (extension != null) {
94              return extension;
95          } else {
96              return artifact.getExtension();
97          }
98      }
99  
100     @Override
101     public String getVersion() {
102         if (version != null) {
103             return version;
104         } else {
105             return artifact.getVersion();
106         }
107     }
108 
109     // Revise these three methods when MRESOLVER-233 is delivered
110     @Override
111     public Artifact setVersion(String version) {
112         String current = getVersion();
113         if (current.equals(version) || (version == null && current.isEmpty())) {
114             return this;
115         }
116         return new RelocatedArtifact(artifact, groupId, artifactId, classifier, extension, version, message);
117     }
118 
119     @Deprecated
120     @Override
121     public Artifact setFile(File file) {
122         File current = getFile();
123         if (Objects.equals(current, file)) {
124             return this;
125         }
126         return new RelocatedArtifact(
127                 artifact.setFile(file), groupId, artifactId, classifier, extension, version, message);
128     }
129 
130     @Override
131     public Artifact setPath(Path path) {
132         Path current = getPath();
133         if (Objects.equals(current, path)) {
134             return this;
135         }
136         return new RelocatedArtifact(
137                 artifact.setPath(path), groupId, artifactId, classifier, extension, version, message);
138     }
139 
140     @Override
141     public Artifact setProperties(Map<String, String> properties) {
142         Map<String, String> current = getProperties();
143         if (current.equals(properties) || (properties == null && current.isEmpty())) {
144             return this;
145         }
146         return new RelocatedArtifact(
147                 artifact.setProperties(properties), groupId, artifactId, classifier, extension, version, message);
148     }
149 
150     @Deprecated
151     @Override
152     public File getFile() {
153         return artifact.getFile();
154     }
155 
156     @Override
157     public Path getPath() {
158         return artifact.getPath();
159     }
160 
161     @Override
162     public String getProperty(String key, String defaultValue) {
163         return artifact.getProperty(key, defaultValue);
164     }
165 
166     @Override
167     public Map<String, String> getProperties() {
168         return artifact.getProperties();
169     }
170 
171     public String getMessage() {
172         return message;
173     }
174 }