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.util.Map;
23  import java.util.Objects;
24  
25  import org.eclipse.aether.artifact.AbstractArtifact;
26  import org.eclipse.aether.artifact.Artifact;
27  
28  /**
29   * @author Benjamin Bentmann
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     @Override
120     public Artifact setFile(File file) {
121         File current = getFile();
122         if (Objects.equals(current, file)) {
123             return this;
124         }
125         return new RelocatedArtifact(
126                 artifact.setFile(file), groupId, artifactId, classifier, extension, version, message);
127     }
128 
129     @Override
130     public Artifact setProperties(Map<String, String> properties) {
131         Map<String, String> current = getProperties();
132         if (current.equals(properties) || (properties == null && current.isEmpty())) {
133             return this;
134         }
135         return new RelocatedArtifact(
136                 artifact.setProperties(properties), groupId, artifactId, classifier, extension, version, message);
137     }
138 
139     @Override
140     public File getFile() {
141         return artifact.getFile();
142     }
143 
144     @Override
145     public String getProperty(String key, String defaultValue) {
146         return artifact.getProperty(key, defaultValue);
147     }
148 
149     @Override
150     public Map<String, String> getProperties() {
151         return artifact.getProperties();
152     }
153 
154     public String getMessage() {
155         return message;
156     }
157 }