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