View Javadoc

1   package org.apache.maven.jnlp;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.File;
21  import java.io.FileInputStream;
22  import java.io.FileOutputStream;
23  import java.io.InputStream;
24  import java.util.Enumeration;
25  import java.util.jar.JarEntry;
26  import java.util.jar.JarFile;
27  import java.util.jar.JarOutputStream;
28  
29  /**
30   * Updates manifest for correcting java webstart bug
31   *
32   * @author <a href="mailto:evenisse@ifrance.com">Emmanuel Venisse</a>
33   * @version $Id: UpdateManifest.java 170200 2005-05-15 06:24:19Z brett $
34   */
35  public class UpdateManifest
36  {
37      private File inputJar;
38      private File manifest;
39      private File outputDir;
40  
41      public void process() throws Exception
42      {
43          System.out.println("Update Manifest in " + inputJar.getName());
44          JarFile jarFile = new JarFile(inputJar);
45          Enumeration entries = jarFile.entries();
46          File outputJar = new File(outputDir, inputJar.getName());
47          FileOutputStream fos = new FileOutputStream(outputJar);
48          JarOutputStream jos = new JarOutputStream(fos);
49          while (entries.hasMoreElements()) {
50              process(jarFile, jos, (JarEntry)entries.nextElement());
51          }
52          jos.close();
53          jarFile.close();
54          outputJar.setLastModified(inputJar.lastModified());
55      }
56      
57      private void process(JarFile jarFile, JarOutputStream jos, JarEntry entry)
58          throws Exception
59      {
60          InputStream is = jarFile.getInputStream(entry);
61          JarEntry je = entry;
62          if (entry.getName().equalsIgnoreCase("META-INF/MANIFEST.MF"))
63          {
64              je = new JarEntry("META-INF/MANIFEST.MF");
65              is = new FileInputStream(manifest);
66          }
67          JarEntry destEntry = new JarEntry (je.getName()); 
68          jos.putNextEntry(destEntry); 
69          
70          byte[] buffer = new byte[2048];
71          int read = 0;
72          while((read = is.read(buffer)) > 0)
73          {
74              jos.write(buffer, 0, read);
75          }
76          jos.closeEntry();
77      }
78      
79      public void setInputJar(File input)
80      {
81          this.inputJar = input;
82      }
83      
84      public File getInputJar()
85      {
86          return inputJar;
87      }
88      
89      public void setManifest(File manifest)
90      {
91          this.manifest = manifest;
92      }
93      
94      public File getManifest()
95      {
96          return manifest;
97      }
98      
99      public void setOutputDir(File outputDir)
100     {
101         this.outputDir = outputDir;
102     }
103     
104     public File getOutputDir()
105     {
106         return outputDir;
107     }
108 }