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.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
45
46
47
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
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
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
118 Enumeration<? extends ZipEntry> entries = originalJar.entries();
119 while ( entries.hasMoreElements() )
120 {
121 ZipEntry entry = entries.nextElement();
122
123
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
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
167
168
169
170
171
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
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
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 }