1 | |
package org.apache.maven.shared.jarsigner; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
23 | |
import org.codehaus.plexus.util.Os; |
24 | |
import org.codehaus.plexus.util.StringUtils; |
25 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
26 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
27 | |
import org.codehaus.plexus.util.cli.Commandline; |
28 | |
import org.codehaus.plexus.util.cli.StreamConsumer; |
29 | |
|
30 | |
import java.io.File; |
31 | |
import java.io.IOException; |
32 | |
import java.io.InputStream; |
33 | |
import java.util.Map; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 18 | public class DefaultJarSigner |
44 | |
extends AbstractLogEnabled |
45 | |
implements JarSigner |
46 | |
{ |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
protected String jarSignerFile; |
52 | |
|
53 | |
public JarSignerResult execute( JarSignerRequest request ) |
54 | |
throws JarSignerException |
55 | |
{ |
56 | |
|
57 | 3 | if ( jarSignerFile == null ) |
58 | |
{ |
59 | |
|
60 | |
|
61 | |
try |
62 | |
{ |
63 | 2 | jarSignerFile = findJarSignerExecutable(); |
64 | |
} |
65 | 0 | catch ( IOException e ) |
66 | |
{ |
67 | 0 | throw new JarSignerException( "Error finding jar signer executable. Reason: " + e.getMessage(), e ); |
68 | 2 | } |
69 | |
} |
70 | |
|
71 | |
|
72 | 3 | Commandline cli = createCommandLine( request ); |
73 | |
|
74 | |
|
75 | 3 | return executeCommandLine( cli, request ); |
76 | |
} |
77 | |
|
78 | |
protected Commandline createCommandLine( JarSignerRequest request ) |
79 | |
throws JarSignerException |
80 | |
{ |
81 | 3 | JarSignerCommandLineBuilder cliBuilder = new JarSignerCommandLineBuilder(); |
82 | 3 | cliBuilder.setLogger( getLogger() ); |
83 | 3 | cliBuilder.setJarSignerFile( jarSignerFile ); |
84 | |
try |
85 | |
{ |
86 | 3 | return cliBuilder.build( request ); |
87 | |
} |
88 | 0 | catch ( CommandLineConfigurationException e ) |
89 | |
{ |
90 | 0 | throw new JarSignerException( "Error configuring command-line. Reason: " + e.getMessage(), e ); |
91 | |
} |
92 | |
} |
93 | |
|
94 | |
protected JarSignerResult executeCommandLine( Commandline cli, JarSignerRequest request ) |
95 | |
{ |
96 | 3 | if ( getLogger().isDebugEnabled() ) |
97 | |
{ |
98 | 0 | getLogger().debug( "Executing: " + cli ); |
99 | |
} |
100 | |
|
101 | 3 | final boolean verbose = request.isVerbose(); |
102 | |
|
103 | 3 | InputStream systemIn = new InputStream() |
104 | 3 | { |
105 | |
|
106 | |
public int read() |
107 | |
{ |
108 | 3 | return -1; |
109 | |
} |
110 | |
|
111 | |
}; |
112 | 3 | StreamConsumer systemOut = request.getSystemOutStreamConsumer(); |
113 | |
|
114 | 3 | if ( systemOut == null ) |
115 | |
{ |
116 | 3 | systemOut = new StreamConsumer() |
117 | 3 | { |
118 | |
|
119 | |
public void consumeLine( final String line ) |
120 | |
{ |
121 | 16 | if ( verbose ) |
122 | |
{ |
123 | 16 | getLogger().info( line ); |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | getLogger().debug( line ); |
128 | |
} |
129 | 16 | } |
130 | |
|
131 | |
}; |
132 | |
} |
133 | |
|
134 | 3 | StreamConsumer systemErr = request.getSystemErrorStreamConsumer(); |
135 | |
|
136 | 3 | if ( systemErr == null ) |
137 | |
{ |
138 | 3 | systemErr = new StreamConsumer() |
139 | 3 | { |
140 | |
|
141 | |
public void consumeLine( final String line ) |
142 | |
{ |
143 | 0 | getLogger().warn( line ); |
144 | 0 | } |
145 | |
|
146 | |
}; |
147 | |
} |
148 | |
|
149 | 3 | DefaultJarSignerResult result = new DefaultJarSignerResult(); |
150 | 3 | result.setCommandline( cli ); |
151 | |
|
152 | 3 | if ( verbose ) |
153 | |
{ |
154 | 3 | getLogger().info( cli.toString() ); |
155 | |
} |
156 | |
else |
157 | |
{ |
158 | 0 | getLogger().debug( cli.toString() ); |
159 | |
} |
160 | |
|
161 | |
try |
162 | |
{ |
163 | 3 | int resultCode = CommandLineUtils.executeCommandLine( cli, systemIn, systemOut, systemErr ); |
164 | |
|
165 | 3 | result.setExitCode( resultCode ); |
166 | |
} |
167 | 0 | catch ( CommandLineException e ) |
168 | |
{ |
169 | 0 | result.setExecutionException( e ); |
170 | 3 | } |
171 | |
|
172 | 3 | return result; |
173 | |
} |
174 | |
|
175 | |
protected String findJarSignerExecutable() |
176 | |
throws IOException |
177 | |
{ |
178 | 2 | String command = "jarsigner" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ); |
179 | |
|
180 | 2 | String executable = |
181 | |
findExecutable( command, System.getProperty( "java.home" ), new String[]{ "../bin", "bin", "../sh" } ); |
182 | |
|
183 | 2 | if ( executable == null ) |
184 | |
{ |
185 | |
|
186 | 0 | Map<String, String> env = System.getenv(); |
187 | |
|
188 | 0 | String[] variables = { "JDK_HOME", "JAVA_HOME" }; |
189 | |
|
190 | 0 | for ( int i = 0; i < variables.length && executable == null; i++ ) |
191 | |
{ |
192 | 0 | executable = findExecutable( command, env.get( variables[i] ), new String[]{ "bin", "sh" } ); |
193 | |
} |
194 | |
|
195 | |
} |
196 | |
|
197 | 2 | if ( executable == null ) |
198 | |
{ |
199 | 0 | executable = command; |
200 | |
} |
201 | |
|
202 | 2 | return executable; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
protected String findExecutable( String command, String homeDir, String[] subDirs ) |
214 | |
{ |
215 | 2 | if ( StringUtils.isNotEmpty( homeDir ) ) |
216 | |
{ |
217 | 4 | for ( int i = 0; i < subDirs.length; i++ ) |
218 | |
{ |
219 | 4 | File file = new File( new File( homeDir, subDirs[i] ), command ); |
220 | |
|
221 | 4 | if ( file.isFile() ) |
222 | |
{ |
223 | 2 | return file.getAbsolutePath(); |
224 | |
} |
225 | |
} |
226 | |
} |
227 | |
|
228 | 0 | return null; |
229 | |
} |
230 | |
} |