1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
30  
31  
32  
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      protected boolean terminatePassphrase;
50  
51      private File outputDir;
52  
53      private File buildDir;
54  
55      private File baseDir;
56  
57      protected File homeDir;
58  
59      protected String secretKeyring;
60  
61      protected String publicKeyring;
62  
63      protected String lockMode;
64  
65      protected List<String> args;
66  
67      public Log getLog() {
68          return log;
69      }
70  
71      public void setArgs(List<String> args) {
72          this.args = args;
73      }
74  
75      public void setInteractive(boolean b) {
76          isInteractive = b;
77      }
78  
79      public void setLockMode(String lockMode) {
80          this.lockMode = lockMode;
81      }
82  
83      public void setUseAgent(boolean b) {
84          useAgent = b;
85      }
86  
87      public void setDefaultKeyring(boolean enabled) {
88          defaultKeyring = enabled;
89      }
90  
91      public void setKeyName(String s) {
92          keyname = s;
93      }
94  
95      public void setLog(Log log) {
96          this.log = log;
97      }
98  
99      public void setPassPhrase(String s) {
100         passphrase = s;
101     }
102 
103     public void setTerminatePassphrase(boolean b) {
104         this.terminatePassphrase = b;
105     }
106 
107     public void setOutputDirectory(File out) {
108         outputDir = out;
109     }
110 
111     public void setBuildDirectory(File out) {
112         buildDir = out;
113     }
114 
115     public void setBaseDirectory(File out) {
116         baseDir = out;
117     }
118 
119     public void setHomeDirectory(File homeDirectory) {
120         homeDir = homeDirectory;
121     }
122 
123     public void setSecretKeyring(String path) {
124         secretKeyring = path;
125     }
126 
127     public void setPublicKeyring(String path) {
128         publicKeyring = path;
129     }
130 
131     public abstract String signerName();
132 
133     
134 
135 
136 
137 
138     public void prepare() throws MojoFailureException {}
139 
140     
141 
142 
143 
144 
145 
146     public abstract String getKeyInfo();
147 
148     
149 
150 
151 
152 
153 
154 
155 
156     public File generateSignatureForArtifact(File file) throws MojoExecutionException {
157         
158         
159         
160 
161         File signature = new File(file + SIGNATURE_EXTENSION);
162 
163         boolean isInBuildDir = false;
164         if (buildDir != null) {
165             File parent = signature.getParentFile();
166             if (buildDir.equals(parent)) {
167                 isInBuildDir = true;
168             }
169         }
170         if (!isInBuildDir && outputDir != null) {
171             String fileDirectory = "";
172             File signatureDirectory = signature;
173 
174             while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
175                 if (isPossibleRootOfArtifact(signatureDirectory)) {
176                     break;
177                 }
178                 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
179             }
180             signatureDirectory = new File(outputDir, fileDirectory);
181             if (!signatureDirectory.exists()) {
182                 signatureDirectory.mkdirs();
183             }
184             signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
185         }
186 
187         if (signature.exists()) {
188             signature.delete();
189         }
190 
191         
192         
193         
194 
195         generateSignatureForFile(file, signature);
196 
197         return signature;
198     }
199 
200     
201 
202 
203 
204 
205 
206 
207     protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
208 
209     private boolean isPossibleRootOfArtifact(File signatureDirectory) {
210         return signatureDirectory.equals(outputDir)
211                 || signatureDirectory.equals(buildDir)
212                 || signatureDirectory.equals(baseDir);
213     }
214 }