1 package org.codehaus.mojo.webstart.dependency.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.shared.jarsigner.JarSignerUtil;
24 import org.codehaus.mojo.webstart.dependency.JnlpDependencyConfig;
25 import org.codehaus.mojo.webstart.sign.SignTool;
26 import org.codehaus.plexus.component.annotations.Component;
27 import org.codehaus.plexus.component.annotations.Requirement;
28
29 import java.io.File;
30 import java.io.IOException;
31
32
33
34
35
36
37
38
39 @Component( role = JnlpDependencyTask.class, hint = UnsignTask.ROLE_HINT, instantiationStrategy = "per-lookup" )
40 public class UnsignTask
41 extends AbstractJnlpTask
42 {
43 public static final String ROLE_HINT = "UnsignTask";
44
45
46
47
48 @Requirement
49 private SignTool signTool;
50
51
52
53
54 public void check( JnlpDependencyConfig config )
55 {
56 if ( config == null )
57 {
58 throw new NullPointerException( "config can't be null" );
59 }
60 if ( config.getArtifact() == null )
61 {
62 throw new NullPointerException( "config.artifact can't be null" );
63 }
64 if ( config.getArtifact().getFile() == null )
65 {
66 throw new NullPointerException( "config.artifact.file can't be null" );
67 }
68 if ( !config.isSign() )
69 {
70 throw new IllegalStateException( "Can't sign if config.isSign is false" );
71 }
72
73 File file = config.getArtifact().getFile();
74
75 boolean jarSigned;
76 try
77 {
78 jarSigned = signTool.isJarSigned( file );
79 }
80 catch ( MojoExecutionException e )
81 {
82 throw new RuntimeException( e.getMessage(), e.getCause() );
83 }
84
85 if ( jarSigned && !config.isCanUnsign() )
86 {
87 throw new IllegalStateException( "Can't unsign the config.artifact.file if config.isCanUsign is false" );
88 }
89 }
90
91
92
93
94 public File execute( JnlpDependencyConfig config, File jarFile )
95 throws JnlpDependencyTaskException
96 {
97
98 boolean jarSigned;
99 try
100 {
101 jarSigned = JarSignerUtil.isArchiveSigned( jarFile );
102 }
103 catch ( IOException e )
104 {
105 throw new JnlpDependencyTaskException( "Could not detect if jar signed: " + jarFile, e );
106 }
107
108 if ( jarSigned )
109 {
110
111
112
113 verboseLog( config, "Unsign jar " + jarFile );
114 try
115 {
116 JarSignerUtil.unsignArchive( jarFile );
117 }
118 catch ( IOException e )
119 {
120
121 throw new JnlpDependencyTaskException( "Could not find unsign jar " + jarFile, e );
122 }
123 }
124 else
125 {
126
127
128 verboseLog( config, "Jar " + jarFile + " is not signed." );
129 }
130 return jarFile;
131 }
132 }