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.codehaus.mojo.webstart.dependency.JnlpDependencyConfig;
24 import org.codehaus.mojo.webstart.sign.SignConfig;
25 import org.codehaus.mojo.webstart.sign.SignTool;
26 import org.codehaus.mojo.webstart.util.IOUtil;
27 import org.codehaus.plexus.component.annotations.Component;
28 import org.codehaus.plexus.component.annotations.Requirement;
29
30 import java.io.File;
31
32
33
34
35
36
37
38 @Component( role = JnlpDependencyTask.class, hint = SignTask.ROLE_HINT, instantiationStrategy = "per-lookup" )
39 public class SignTask
40 extends AbstractJnlpTask
41 {
42 public static final String ROLE_HINT = "SignTask";
43
44 @Requirement
45 private SignTool signTool;
46
47 @Requirement
48 private IOUtil ioUtil;
49
50
51
52
53 public void check( JnlpDependencyConfig config )
54 {
55 if ( config == null )
56 {
57 throw new NullPointerException( "config can't be null" );
58 }
59 if ( config.getArtifact() == null )
60 {
61 throw new NullPointerException( "config.artifact can't be null" );
62 }
63 if ( config.getArtifact().getFile() == null )
64 {
65 throw new NullPointerException( "config.artifact.file can't be null" );
66 }
67 if ( !config.isSign() )
68 {
69 throw new IllegalStateException( "Can't sign if config.isSign is false" );
70 }
71
72 File file = config.getArtifact().getFile();
73
74 boolean jarSigned;
75 try
76 {
77 jarSigned = signTool.isJarSigned( file );
78 }
79 catch ( MojoExecutionException e )
80 {
81 throw new RuntimeException( e.getMessage(), e.getCause() );
82 }
83
84 if ( jarSigned && !config.isCanUnsign() )
85 {
86 throw new IllegalStateException( "Can't unsign the config.artifact.file if config.isCanUsign is false" );
87 }
88 }
89
90
91
92
93 public File execute( JnlpDependencyConfig config, File file )
94 throws JnlpDependencyTaskException
95 {
96
97 SignConfig sign = config.getSign();
98
99 boolean signVerify = sign.isVerify();
100
101 File signedJar = new File( file.getParentFile(), file.getName() + ".sign" );
102
103 try
104 {
105 ioUtil.deleteFile( signedJar );
106 }
107 catch ( MojoExecutionException e )
108 {
109 throw new JnlpDependencyTaskException( e.getMessage(), e.getCause() );
110 }
111
112 verboseLog( config, "Sign " + signedJar.getName() );
113 try
114 {
115 signTool.sign( sign, file, signedJar );
116 }
117 catch ( MojoExecutionException e )
118 {
119 throw new JnlpDependencyTaskException( e.getMessage(), e.getCause() );
120 }
121
122 getLogger().debug( "lastModified signedJar:" + signedJar.lastModified() + " not signed Jar:" +
123 file.lastModified() );
124
125 if ( signVerify )
126 {
127 verboseLog( config, "Verify signature of " + signedJar.getName() );
128 try
129 {
130 signTool.verify( sign, signedJar, config.isVerbose() );
131 }
132 catch ( MojoExecutionException e )
133 {
134 throw new JnlpDependencyTaskException( e.getMessage(), e.getCause() );
135 }
136 }
137 return signedJar;
138 }
139 }