This project has retired. For details please refer to its Attic page.
ArtifactMetadata xref
View Javadoc
1   package org.apache.archiva.metadata.model;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.archiva.checksum.ChecksumAlgorithm;
23  import org.apache.commons.collections4.bidimap.DualHashBidiMap;
24  
25  import javax.xml.bind.annotation.XmlRootElement;
26  import java.time.Instant;
27  import java.time.ZonedDateTime;
28  import java.time.temporal.ChronoUnit;
29  import java.time.temporal.TemporalUnit;
30  import java.util.Map;
31  import java.util.Set;
32  import java.util.stream.Collectors;
33  
34  /**
35   * Metadata stored in the content repository for a particular artifact. Information that is shared between different
36   * artifacts of a given project version can be found in the
37   * {@link org.apache.archiva.metadata.model.ProjectVersionMetadata} class. The metadata is faceted to store information
38   * about particular types of artifacts, for example Maven 2.x artifact specific information.
39   * For more information, see the
40   * <a href="{@docRoot}/../metadata-content-model.html" target="_top">Metadata Content Model</a>.
41   */
42  @XmlRootElement(name = "artifactMetadata")
43  public class ArtifactMetadata
44          extends FacetedMetadata {
45  
46  
47      /**
48       * The artifact ID uniquely identifies an artifact within a given namespace, project and project version. For
49       * example, <tt>archiva-1.4-20100201.345612-2.jar</tt>
50       */
51      private String id;
52  
53      /**
54       * The repository that the artifact is stored in within the content repository.
55       */
56      private String repositoryId;
57  
58      /**
59       * The namespace of the project within the repository.
60       *
61       * @see ProjectMetadata#getNamespace()
62       */
63      private String namespace;
64  
65      /**
66       * The identifier of the project within the repository and namespace.
67       *
68       * @see ProjectMetadata#getId()
69       */
70      private String project;
71  
72      /**
73       * The version of the project. This may be more generalised than @{link #version}.
74       *
75       * @see ProjectVersionMetadata#getId()
76       */
77      private String projectVersion;
78  
79      /**
80       * The artifact version, if different from the project version. Note that the metadata does not do any calculation
81       * of this based on the project version - the calling code must be sure to set and check it appropriately if
82       * <tt>null</tt>.
83       */
84      private String version;
85  
86      /**
87       * The last modified date of the artifact file, if known.
88       */
89      private ZonedDateTime fileLastModified;
90  
91      /**
92       * The file size of the artifact, if known.
93       */
94      private long size;
95  
96      /**
97       * The list of checksums.
98       */
99      private Map<ChecksumAlgorithm, String> checksums = new DualHashBidiMap<>( );
100 
101     private String toStringValue = "";
102     private int lastHash = 0;
103 
104     /**
105      * When the artifact was found in the repository storage and added to the metadata content repository.
106      */
107     private ZonedDateTime whenGathered;
108 
109     public String getId() {
110         return id;
111     }
112 
113     public void setId(String id) {
114         this.id = id;
115     }
116 
117     public long getSize() {
118         return size;
119     }
120 
121     public void setSize(long size) {
122         this.size = size;
123     }
124 
125     public String getVersion() {
126         return version;
127     }
128 
129     public void setVersion(String version) {
130         this.version = version;
131     }
132 
133     public String getProjectVersion() {
134         return projectVersion;
135     }
136 
137     public void setProjectVersion(String projectVersion) {
138         this.projectVersion = projectVersion;
139     }
140 
141     public void setFileLastModified(long fileLastModified) {
142         this.fileLastModified = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fileLastModified), ModelInfo.STORAGE_TZ);
143     }
144 
145     public void setWhenGathered(ZonedDateTime whenGathered) {
146         // We set the resolution to milliseconds, because it's the resolution that all current backends support
147         this.whenGathered = whenGathered.withZoneSameInstant(ModelInfo.STORAGE_TZ).truncatedTo(ChronoUnit.MILLIS);
148     }
149 
150     public void setMd5(String md5) {
151         this.checksums.put(ChecksumAlgorithm.MD5, md5);
152     }
153 
154     public void setSha1(String sha1) {
155         this.checksums.put(ChecksumAlgorithm.SHA1, sha1);
156     }
157 
158     public ZonedDateTime getWhenGathered() {
159         return whenGathered;
160     }
161 
162     public String getChecksum(ChecksumAlgorithm checksumAlgorithm) {
163         return checksums.get(checksumAlgorithm);
164     }
165 
166     public void setChecksum(ChecksumAlgorithm algorithm, String checksumValue) {
167         this.checksums.put(algorithm, checksumValue);
168     }
169 
170     public Set<ChecksumAlgorithm> getChecksumTypes() {
171         return checksums.keySet();
172     }
173 
174     public Map<ChecksumAlgorithm,String> getChecksums() {
175         return this.checksums;
176     }
177 
178     public boolean hasChecksum(String checksum) {
179         return this.checksums.containsValue( checksum );
180     }
181 
182     public void setChecksums(Map<ChecksumAlgorithm,String> checksums) {
183         this.checksums = checksums;
184     }
185 
186     public String getMd5() {
187         return checksums.get(ChecksumAlgorithm.MD5);
188     }
189 
190     public String getSha1() {
191         return checksums.get(ChecksumAlgorithm.SHA1);
192     }
193 
194     public ZonedDateTime getFileLastModified() {
195 
196         return fileLastModified;
197     }
198 
199     public String getNamespace() {
200         return namespace;
201     }
202 
203     public void setNamespace(String namespace) {
204         this.namespace = namespace;
205     }
206 
207     public void setProject(String project) {
208         this.project = project;
209     }
210 
211     public String getProject() {
212         return project;
213     }
214 
215     public String getRepositoryId() {
216         return repositoryId;
217     }
218 
219     public void setRepositoryId(String repositoryId) {
220         this.repositoryId = repositoryId;
221     }
222 
223     @Override
224     public boolean equals(Object o) {
225         if (this == o) {
226             return true;
227         }
228         if (o == null || getClass() != o.getClass()) {
229             return false;
230         }
231 
232         ArtifactMetadata/../../org/apache/archiva/metadata/model/ArtifactMetadata.html#ArtifactMetadata">ArtifactMetadata that = (ArtifactMetadata) o;
233 
234         if (size != that.size) {
235             return false;
236         }
237         // Time equality by instant that means the point in time must match, but not the time zone
238         if (fileLastModified != null
239                 ? !fileLastModified.toInstant().equals(that.fileLastModified.toInstant())
240                 : that.fileLastModified != null) {
241             return false;
242         }
243         if (!id.equals(that.id)) {
244             return false;
245         }
246         for ( Map.Entry<ChecksumAlgorithm, String> entry : this.checksums.entrySet()) {
247             String thatChecksum = that.checksums.get(entry.getKey());
248             if (entry.getValue()!=null ? !entry.getValue().equals(thatChecksum) : thatChecksum!=null) {
249                 return false;
250             }
251         }
252         if (namespace != null ? !namespace.equals(that.namespace) : that.namespace != null) {
253             return false;
254         }
255         if (project != null ? !project.equals(that.project) : that.project != null) {
256             return false;
257         }
258         if (projectVersion != null ? !projectVersion.equals(that.projectVersion) : that.projectVersion != null) {
259             return false;
260         }
261         /**
262          * We cannot compare in different repositories, if this is in here
263          if ( !repositoryId.equals( that.repositoryId ) )
264          {
265          return false;
266          }
267          **/
268         if (version != null ? !version.equals(that.version) : that.version != null) {
269             return false;
270         }
271         if (whenGathered != null ? !whenGathered.toInstant().equals(that.whenGathered.toInstant()) : that.whenGathered != null) {
272             return false;
273         }
274 
275         return true;
276     }
277 
278     @Override
279     public int hashCode() {
280         int result = id != null ? id.hashCode() : 0;
281         result = 31 * result + (repositoryId != null ? repositoryId.hashCode() : 0);
282         result = 31 * result + (namespace != null ? namespace.hashCode() : 0);
283         result = 31 * result + (project != null ? project.hashCode() : 0);
284         result = 31 * result + (projectVersion != null ? projectVersion.hashCode() : 0);
285         result = 31 * result + (version != null ? version.hashCode() : 0);
286         result = 31 * result + (fileLastModified != null ? fileLastModified.hashCode() : 0);
287         result = 31 * result + (int) (size ^ (size >>> 32));
288         for (String checksum : checksums.values()) {
289             result = 31 * result + (checksum != null ? checksum.hashCode() : 0);
290         }
291         result = 31 * result + (whenGathered != null ? whenGathered.hashCode() : 0);
292         return result;
293     }
294 
295     /**
296      * Doing some hashing to avoid the expensive string concatenation.
297      */
298     @Override
299     public String toString() {
300         final int hashCode=hashCode();
301         if (hashCode!=lastHash) {
302             toStringValue = "ArtifactMetadata{" + "id='" + id + '\'' + ", size=" + size + ", version='" + version + '\'' +
303                     ", fileLastModified=" + fileLastModified + ", whenGathered=" + whenGathered +
304                     ", namespace='" + namespace + '\'' + ", project='" + project + '\'' +
305                     ", projectVersion='" + projectVersion + '\'' + ", repositoryId='" + repositoryId + '\'' +
306                     ", checksums=" + checksums.entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.joining(",")) +
307                     '}';
308             lastHash=hashCode;
309         }
310         return toStringValue;
311     }
312 }