View Javadoc
1   package org.codehaus.mojo.webstart.dependency.task;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Created on 1/4/14.
34   *
35   * @author Tony Chemit <chemit@codelutin.com>
36   * @since 1.0-beta-5
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       * {@inheritDoc}
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       * {@inheritDoc}
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 }