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   */
30  public final class RelocatedArtifact extends AbstractArtifact {
31  
32      private final Artifact artifact;
33  
34      private final String groupId;
35  
36      private final String artifactId;
37  
38      private final String classifier;
39  
40      private final String extension;
41  
42      private final String version;
43  
44      private final String message;
45  
46      public RelocatedArtifact(
47              Artifact artifact,
48              String groupId,
49              String artifactId,
50              String classifier,
51              String extension,
52              String version,
53              String message) {
54          this.artifact = Objects.requireNonNull(artifact, "artifact cannot be null");
55          this.groupId = (groupId != null && !groupId.isEmpty()) ? groupId : null;
56          this.artifactId = (artifactId != null && !artifactId.isEmpty()) ? artifactId : null;
57          this.classifier = (classifier != null && !classifier.isEmpty()) ? classifier : null;
58          this.extension = (extension != null && !extension.isEmpty()) ? extension : null;
59          this.version = (version != null && !version.isEmpty()) ? version : null;
60          this.message = (message != null && !message.isEmpty()) ? message : null;
61      }
62  
63      @Override
64      public String getGroupId() {
65          if (groupId != null) {
66              return groupId;
67          } else {
68              return artifact.getGroupId();
69          }
70      }
71  
72      @Override
73      public String getArtifactId() {
74          if (artifactId != null) {
75              return artifactId;
76          } else {
77              return artifact.getArtifactId();
78          }
79      }
80  
81      @Override
82      public String getClassifier() {
83          if (classifier != null) {
84              return classifier;
85          } else {
86              return artifact.getClassifier();
87          }
88      }
89  
90      @Override
91      public String getExtension() {
92          if (extension != null) {
93              return extension;
94          } else {
95              return artifact.getExtension();
96          }
97      }
98  
99      @Override
100     public String getVersion() {
101         if (version != null) {
102             return version;
103         } else {
104             return artifact.getVersion();
105         }
106     }
107 
108     // Revise these three methods when MRESOLVER-233 is delivered
109     @Override
110     public Artifact setVersion(String version) {
111         String current = getVersion();
112         if (current.equals(version) || (version == null && current.length() <= 0)) {
113             return this;
114         }
115         return new RelocatedArtifact(artifact, groupId, artifactId, classifier, extension, version, message);
116     }
117 
118     @Override
119     public Artifact setFile(File file) {
120         File current = getFile();
121         if (Objects.equals(current, file)) {
122             return this;
123         }
124         return new RelocatedArtifact(
125                 artifact.setFile(file), groupId, artifactId, classifier, extension, version, message);
126     }
127 
128     @Override
129     public Artifact setProperties(Map<String, String> properties) {
130         Map<String, String> current = getProperties();
131         if (current.equals(properties) || (properties == null && current.isEmpty())) {
132             return this;
133         }
134         return new RelocatedArtifact(
135                 artifact.setProperties(properties), groupId, artifactId, classifier, extension, version, message);
136     }
137 
138     @Override
139     public File getFile() {
140         return artifact.getFile();
141     }
142 
143     @Override
144     public String getProperty(String key, String defaultValue) {
145         return artifact.getProperty(key, defaultValue);
146     }
147 
148     @Override
149     public Map<String, String> getProperties() {
150         return artifact.getProperties();
151     }
152 
153     public String getMessage() {
154         return message;
155     }
156 }