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