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.plugins.gpg;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
26  import org.apache.maven.artifact.metadata.ArtifactMetadata;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataStoreException;
29  import org.codehaus.plexus.util.FileUtils;
30  
31  /**
32   * @author dkulp
33   *
34   */
35  public class AscArtifactMetadata extends AbstractArtifactMetadata
36          implements org.apache.maven.shared.transfer.metadata.ArtifactMetadata {
37  
38      private final File file;
39  
40      private final boolean isPom;
41  
42      public AscArtifactMetadata(Artifact artifact, File file, boolean isPom) {
43          super(artifact);
44          this.file = file;
45          this.isPom = isPom;
46      }
47  
48      @Override
49      public String getBaseVersion() {
50          return artifact.getBaseVersion();
51      }
52  
53      @Override
54      public Object getKey() {
55          return "gpg signature " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":" + artifact.getType()
56                  + ":" + artifact.getClassifier() + (isPom ? ":pom" : "");
57      }
58  
59      private String getFilename() {
60          StringBuilder buf = new StringBuilder(128);
61          buf.append(getArtifactId());
62          buf.append("-").append(artifact.getVersion());
63          if (isPom) {
64              buf.append(".pom");
65          } else {
66              if (artifact.getClassifier() != null && !"".equals(artifact.getClassifier())) {
67                  buf.append("-").append(artifact.getClassifier());
68              }
69              buf.append(".").append(artifact.getArtifactHandler().getExtension());
70          }
71          buf.append(".asc");
72          return buf.toString();
73      }
74  
75      @Override
76      public String getLocalFilename(ArtifactRepository repository) {
77          return getFilename();
78      }
79  
80      @Override
81      public String getRemoteFilename() {
82          return getFilename();
83      }
84  
85      @Override
86      public void merge(ArtifactMetadata metadata) {
87          merge((AscArtifactMetadata) metadata);
88      }
89  
90      @Override
91      public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
92          merge((AscArtifactMetadata) metadata);
93      }
94  
95      private void merge(AscArtifactMetadata metadata) {
96          if (!metadata.file.equals(file)) {
97              throw new IllegalStateException("Cannot add two different pieces of metadata for: " + getKey());
98          }
99      }
100 
101     @Override
102     public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactRepository remoteRepository)
103             throws RepositoryMetadataStoreException {
104         File destination = new File(
105                 localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata(this, remoteRepository));
106 
107         try {
108             FileUtils.copyFile(file, destination);
109         } catch (IOException e) {
110             throw new RepositoryMetadataStoreException("Error copying ASC to the local repository.", e);
111         }
112     }
113 
114     @Override
115     public boolean storedInArtifactVersionDirectory() {
116         return true;
117     }
118 
119     @Override
120     public String toString() {
121         return getFilename();
122     }
123 
124     @Override
125     public File getFile() {
126         return file;
127     }
128 }