KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > core > JarDeltaInstallHandler


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.update.internal.core;
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Date JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.jar.JarFile JavaDoc;
27 import java.util.jar.JarOutputStream JavaDoc;
28 import java.util.zip.ZipEntry JavaDoc;
29 import java.util.zip.ZipException JavaDoc;
30
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.update.core.IFeatureContentConsumer;
33 import org.eclipse.update.core.IPluginEntry;
34 import org.eclipse.update.core.PluginEntry;
35 import org.eclipse.update.core.Site;
36
37 public class JarDeltaInstallHandler extends DeltaInstallHandler {
38
39     
40     protected void overlayPlugin(
41             IPluginEntry oldPlugin,
42             IPluginEntry newPlugin,
43             IFeatureContentConsumer consumer)
44             throws CoreException, IOException JavaDoc {
45         
46             if(newPlugin instanceof PluginEntry && ((PluginEntry)newPlugin).isUnpack()){
47                 // partial plug-ins (in patches) must always be unpacked
48
super.overlayPlugin(oldPlugin, newPlugin, consumer);
49             }
50             
51             URL JavaDoc oldURI = null;
52             try {
53                 oldURI = new URL JavaDoc(consumer.getFeature().getSite().getURL().getPath() +
54                                      Site.DEFAULT_PLUGIN_PATH +
55                                      oldPlugin.getVersionedIdentifier().toString());
56             } catch (MalformedURLException JavaDoc e) {
57                 throw new IOException JavaDoc(e.getMessage());
58             }
59             File JavaDoc oldJarFile = new File JavaDoc(oldURI.toExternalForm());
60             JarFile JavaDoc oldJar = new JarFile JavaDoc(oldJarFile);
61             
62             URL JavaDoc newURI = null;
63             try {
64                 newURI = new URL JavaDoc(consumer.getFeature().getSite().getURL().getPath() +
65                                  Site.DEFAULT_PLUGIN_PATH +
66                                  newPlugin.getVersionedIdentifier().toString());
67             } catch (MalformedURLException JavaDoc e) {
68                 throw new IOException JavaDoc(e.getMessage());
69             }
70             File JavaDoc newJarFile = new File JavaDoc(newURI.toExternalForm());
71             JarFile JavaDoc newJar = new JarFile JavaDoc(newJarFile);
72
73             String JavaDoc tempFileName = oldURI + "-" + (new Date JavaDoc()).getTime(); //$NON-NLS-1$
74
File JavaDoc tempFile = new File JavaDoc(tempFileName);
75             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(tempFile);
76             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc( fos);
77             
78             addToJar(jos, newJar);
79             addToJar(jos, oldJar);
80             
81             jos.closeEntry();
82             jos.finish();
83             fos.close();
84             newJar.close();
85             oldJar.close();
86             
87             newJarFile = new File JavaDoc(newURI.toExternalForm());
88             newJarFile.delete();
89             
90             newJarFile.createNewFile();
91
92             copyFile(tempFile, newJarFile);
93     }
94     
95     public static void copyFile(File JavaDoc src, File JavaDoc dst) throws IOException JavaDoc {
96         InputStream JavaDoc in=null;
97         OutputStream JavaDoc out=null;
98         try {
99             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(src));
100             out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dst));
101             byte[] buffer = new byte[4096];
102             int len;
103             while ((len=in.read(buffer)) != -1) {
104                 out.write(buffer, 0, len);
105             }
106         } finally {
107             if (in != null)
108                 try {
109                     in.close();
110                 } catch (IOException JavaDoc e) {
111                 }
112             if (out != null)
113                 try {
114                     out.close();
115                 } catch (IOException JavaDoc e) {
116                 }
117         }
118     }
119
120     public static void addToJar(JarOutputStream JavaDoc jos, JarFile JavaDoc jf) throws IOException JavaDoc {
121         Enumeration JavaDoc e = jf.entries();
122         
123         while(e.hasMoreElements()) {
124             
125             ZipEntry JavaDoc je = (ZipEntry JavaDoc)e.nextElement();
126             InputStream JavaDoc io = jf.getInputStream(je);
127             
128             byte b[] = new byte[4096];
129             int read = 0;
130             try {
131                 jos.putNextEntry(je);
132                 while( ( read = io.read(b, 0, 4096)) != -1) {
133                     jos.write(b, 0, read);
134                 }
135             } catch (ZipException JavaDoc ze) {
136                 //ze.printStackTrace();
137
throw ze;
138             }
139         }
140     }
141 }
142
Popular Tags