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.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugin.logging.Log;
27  
28  /**
29   * A base class for all classes that implements signing of files.
30   *
31   * @author Dennis Lundberg
32   * @since 1.5
33   */
34  public abstract class AbstractGpgSigner {
35      public static final String SIGNATURE_EXTENSION = ".asc";
36  
37      protected boolean useAgent;
38  
39      protected boolean isInteractive = true;
40  
41      protected boolean defaultKeyring = true;
42  
43      protected String keyname;
44  
45      private Log log;
46  
47      protected String passphrase;
48  
49      private File outputDir;
50  
51      private File buildDir;
52  
53      private File baseDir;
54  
55      protected File homeDir;
56  
57      protected String secretKeyring;
58  
59      protected String publicKeyring;
60  
61      protected String lockMode;
62  
63      protected List<String> args;
64  
65      public Log getLog() {
66          return log;
67      }
68  
69      public void setArgs(List<String> args) {
70          this.args = args;
71      }
72  
73      public void setInteractive(boolean b) {
74          isInteractive = b;
75      }
76  
77      public void setLockMode(String lockMode) {
78          this.lockMode = lockMode;
79      }
80  
81      public void setUseAgent(boolean b) {
82          useAgent = b;
83      }
84  
85      public void setDefaultKeyring(boolean enabled) {
86          defaultKeyring = enabled;
87      }
88  
89      public void setKeyName(String s) {
90          keyname = s;
91      }
92  
93      public void setLog(Log log) {
94          this.log = log;
95      }
96  
97      public void setPassPhrase(String s) {
98          passphrase = s;
99      }
100 
101     public void setOutputDirectory(File out) {
102         outputDir = out;
103     }
104 
105     public void setBuildDirectory(File out) {
106         buildDir = out;
107     }
108 
109     public void setBaseDirectory(File out) {
110         baseDir = out;
111     }
112 
113     public void setHomeDirectory(File homeDirectory) {
114         homeDir = homeDirectory;
115     }
116 
117     public void setSecretKeyring(String path) {
118         secretKeyring = path;
119     }
120 
121     public void setPublicKeyring(String path) {
122         publicKeyring = path;
123     }
124 
125     public abstract String signerName();
126 
127     public void prepare() throws MojoFailureException {}
128 
129     /**
130      * Create a detached signature file for the provided file.
131      *
132      * @param file The file to sign
133      * @return A reference to the generated signature file
134      * @throws MojoExecutionException if signature generation fails
135      */
136     public File generateSignatureForArtifact(File file) throws MojoExecutionException {
137         // ----------------------------------------------------------------------------
138         // Set up the file and directory for the signature file
139         // ----------------------------------------------------------------------------
140 
141         File signature = new File(file + SIGNATURE_EXTENSION);
142 
143         boolean isInBuildDir = false;
144         if (buildDir != null) {
145             File parent = signature.getParentFile();
146             if (buildDir.equals(parent)) {
147                 isInBuildDir = true;
148             }
149         }
150         if (!isInBuildDir && outputDir != null) {
151             String fileDirectory = "";
152             File signatureDirectory = signature;
153 
154             while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
155                 if (isPossibleRootOfArtifact(signatureDirectory)) {
156                     break;
157                 }
158                 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
159             }
160             signatureDirectory = new File(outputDir, fileDirectory);
161             if (!signatureDirectory.exists()) {
162                 signatureDirectory.mkdirs();
163             }
164             signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
165         }
166 
167         if (signature.exists()) {
168             signature.delete();
169         }
170 
171         // ----------------------------------------------------------------------------
172         // Generate the signature file
173         // ----------------------------------------------------------------------------
174 
175         generateSignatureForFile(file, signature);
176 
177         return signature;
178     }
179 
180     /**
181      * Generate the detached signature file for the provided file.
182      *
183      * @param file The file to sign
184      * @param signature The file in which the generate signature will be put
185      * @throws MojoExecutionException if signature generation fails
186      */
187     protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
188 
189     private boolean isPossibleRootOfArtifact(File signatureDirectory) {
190         return signatureDirectory.equals(outputDir)
191                 || signatureDirectory.equals(buildDir)
192                 || signatureDirectory.equals(baseDir);
193     }
194 }