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.util.IOUtil;
25  import org.codehaus.mojo.webstart.util.JarUtil;
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.FileOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.Enumeration;
34  import java.util.Map;
35  import java.util.Set;
36  import java.util.jar.Attributes;
37  import java.util.jar.JarFile;
38  import java.util.jar.JarOutputStream;
39  import java.util.jar.Manifest;
40  import java.util.zip.ZipEntry;
41  import java.util.zip.ZipFile;
42  
43  /**
44   * Created on 1/4/14.
45   *
46   * @author Tony Chemit <chemit@codelutin.com>
47   * @since 1.0-beta-5
48   */
49  @Component(role = JnlpDependencyTask.class, hint = UpdateManifestTask.ROLE_HINT, instantiationStrategy = "per-lookup")
50  public class UpdateManifestTask
51      extends AbstractJnlpTask
52  {
53      public static final String ROLE_HINT = "UpdateManifestTask";
54  
55      @Requirement
56      private IOUtil ioUtil;
57  
58      /**
59       * {@inheritDoc}
60       */
61      public void check( JnlpDependencyConfig config )
62      {
63          if ( config == null )
64          {
65              throw new NullPointerException( "config can't be null" );
66          }
67          if ( config.getArtifact() == null )
68          {
69              throw new NullPointerException( "config.artifact can't be null" );
70          }
71          if ( config.getArtifact().getFile() == null )
72          {
73              throw new NullPointerException( "config.artifact.file can't be null" );
74          }
75          if ( !config.isUpdateManifest() )
76          {
77              throw new IllegalStateException( "Can't update manifest if config.isUpdateManifest is false" );
78          }
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      public File execute( JnlpDependencyConfig config, File file )
85          throws JnlpDependencyTaskException
86      {
87  
88          verboseLog( config, "Update manifest " + file.getName() );
89  
90          try
91          {
92              File updatedFile = updateManifestEntries( file, config.getUpdateManifestEntries() );
93              return updatedFile;
94          }
95          catch ( MojoExecutionException e )
96          {
97              throw new JnlpDependencyTaskException( "Could not update manifest of file: " + file, e );
98          }
99      }
100 
101     private File updateManifestEntries( File jar, Map<String, String> manifestentries )
102         throws MojoExecutionException
103     {
104 
105         Manifest manifest = createManifest( jar, manifestentries );
106 
107         File updatedUnprocessedJarFile = new File( jar.getParent(), jar.getName() + "_updateManifestEntriesJar" );
108 
109         ZipFile originalJar = null;
110         JarOutputStream targetJar = null;
111 
112         try
113         {
114             originalJar = new ZipFile( jar );
115             targetJar = new JarOutputStream( new FileOutputStream( updatedUnprocessedJarFile ), manifest );
116 
117             // add all other entries from the original jar file
118             Enumeration<? extends ZipEntry> entries = originalJar.entries();
119             while ( entries.hasMoreElements() )
120             {
121                 ZipEntry entry = entries.nextElement();
122 
123                 // skip the original manifest
124                 if ( JarFile.MANIFEST_NAME.equals( entry.getName() ) )
125                 {
126                     continue;
127                 }
128 
129                 ZipEntry newEntry = new ZipEntry( entry.getName() );
130                 targetJar.putNextEntry( newEntry );
131 
132                 // write content to stream if it is a file
133                 if ( !entry.isDirectory() )
134                 {
135                     InputStream inputStream = null;
136                     try
137                     {
138                         inputStream = originalJar.getInputStream( entry );
139                         org.codehaus.plexus.util.IOUtil.copy( inputStream, targetJar );
140                         inputStream.close();
141                     }
142                     finally
143                     {
144                         org.apache.maven.shared.utils.io.IOUtil.close( inputStream );
145                     }
146                 }
147                 targetJar.closeEntry();
148             }
149             targetJar.close();
150             originalJar.close();
151         }
152         catch ( IOException e )
153         {
154             throw new MojoExecutionException( "Error while updating manifest of " + jar.getName(), e );
155         }
156         finally
157         {
158             org.apache.maven.shared.utils.io.IOUtil.close( targetJar );
159             ioUtil.close( originalJar );
160         }
161 
162         return updatedUnprocessedJarFile;
163     }
164 
165     /**
166      * Create the new manifest from the existing jar file and the new entries.
167      *
168      * @param jar
169      * @param manifestentries
170      * @return Manifest
171      * @throws MojoExecutionException
172      */
173     private Manifest createManifest( File jar, Map<String, String> manifestentries )
174         throws MojoExecutionException
175     {
176         JarFile jarFile = null;
177         try
178         {
179             jarFile = new JarFile( jar );
180 
181             // read manifest from jar
182             Manifest manifest = jarFile.getManifest();
183 
184             if ( manifest == null || manifest.getMainAttributes().isEmpty() )
185             {
186                 manifest = new Manifest();
187                 manifest.getMainAttributes().putValue( Attributes.Name.MANIFEST_VERSION.toString(), "1.0" );
188             }
189 
190             // add or overwrite entries
191             Set<Map.Entry<String, String>> entrySet = manifestentries.entrySet();
192             for ( Map.Entry<String, String> entry : entrySet )
193             {
194                 manifest.getMainAttributes().putValue( entry.getKey(), entry.getValue() );
195             }
196 
197             return manifest;
198         }
199         catch ( IOException e )
200         {
201             throw new MojoExecutionException( "Error while reading manifest from " + jar.getAbsolutePath(), e );
202         }
203         finally
204         {
205             ioUtil.close( jarFile );
206         }
207     }
208 }