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.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   * To unsign a already sign
34   * Created on 1/4/14.
35   *
36   * @author Tony Chemit <chemit@codelutin.com>
37   * @since 1.0-beta-5
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       * Sign tool.
47       */
48      @Requirement
49      private SignTool signTool;
50  
51      /**
52       * {@inheritDoc}
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       * {@inheritDoc}
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             // unsign jar
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             // not signed jar do nothing
128             verboseLog( config, "Jar " + jarFile + " is not signed." );
129         }
130         return jarFile;
131     }
132 }