KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.File JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.net.URL JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.update.core.BaseInstallHandler;
22 import org.eclipse.update.core.ContentReference;
23 import org.eclipse.update.core.IFeature;
24 import org.eclipse.update.core.IFeatureContentConsumer;
25 import org.eclipse.update.core.IPluginEntry;
26 import org.eclipse.update.core.PluginEntry;
27 import org.eclipse.update.core.Site;
28 import org.eclipse.update.internal.operations.UpdateUtils;
29
30 /**
31  * Install handler for partial plugin delivery: copy delta content from old plugin into the new one.
32  * The new plugin should only contain files that have changed.
33  */

34 public class DeltaInstallHandler extends BaseInstallHandler {
35     private final static String JavaDoc PLUGIN_XML = "plugin.xml"; //$NON-NLS-1$
36
private final static String JavaDoc FRAGMENT_XML = "fragment.xml"; //$NON-NLS-1$
37
private final static String JavaDoc META_MANIFEST = "META-INF/MANIFEST.MF"; //$NON-NLS-1$
38

39     protected IFeature oldFeature;
40
41     /* (non-Javadoc)
42      * @see org.eclipse.update.core.IInstallHandler#completeInstall(org.eclipse.update.core.IFeatureContentConsumer)
43      */

44     public void completeInstall(IFeatureContentConsumer consumer)
45         throws CoreException {
46         try {
47             if (pluginEntries == null)
48                 return;
49
50             if (!feature.isPatch()) {
51                 // Get the old feature
52
IFeature[] oldFeatures = UpdateUtils
53                         .getInstalledFeatures(feature);
54                 if (oldFeatures.length == 0)
55                     return;
56                 oldFeature = oldFeatures[0];
57             } else {
58                 oldFeature = UpdateUtils.getPatchedFeature(feature);
59                 if (oldFeature == null) {
60                     return;
61                 }
62             }
63             
64             IPluginEntry[] oldPlugins = oldFeature.getPluginEntries();
65             for (int i = 0; i < pluginEntries.length; i++) {
66                 IPluginEntry newPlugin = pluginEntries[i];
67                 IPluginEntry oldPlugin =
68                     getPluginEntry(
69                         oldPlugins,
70                         newPlugin.getVersionedIdentifier().getIdentifier());
71                 if (oldPlugin == null)
72                     continue;
73                 try {
74                     overlayPlugin(oldPlugin, newPlugin, consumer);
75                 } catch (IOException JavaDoc e) {
76                     throw new CoreException(
77                         new Status(
78                             Status.ERROR,
79                             UpdateUtils.getPluginId(),
80                             1,
81                             "", //$NON-NLS-1$
82
e));
83                 }
84             }
85         } finally {
86             //if (contentConsumer != null)
87
// contentConsumer.close();
88
}
89     }
90
91     protected IPluginEntry getPluginEntry(IPluginEntry[] plugins, String JavaDoc id) {
92         for (int i = 0; i < plugins.length; i++)
93             if (plugins[i].getVersionedIdentifier().getIdentifier().equals(id))
94                 return plugins[i];
95         return null;
96     }
97
98     protected boolean referenceExists(
99         ContentReference[] references,
100         ContentReference ref) {
101         String JavaDoc id = ref.getIdentifier();
102         if (id == null)
103             return false;
104
105         for (int i = 0; i < references.length; i++)
106             if (id.equals(references[i].getIdentifier()))
107                 return true;
108         return false;
109     }
110
111     protected void overlayPlugin(
112         IPluginEntry oldPlugin,
113         IPluginEntry newPlugin,
114         IFeatureContentConsumer consumer)
115         throws CoreException, IOException JavaDoc {
116         if(newPlugin instanceof PluginEntry && !((PluginEntry)newPlugin).isUnpack()){
117             // partial plug-ins (in patches) must always be unpacked
118
return;
119         }
120         
121         // copy the content of the old plugin over the new one, but only
122
// those files that do not exist on the target
123
ContentReference[] oldReferences =
124             oldFeature
125                 .getFeatureContentProvider()
126                 .getPluginEntryContentReferences(
127                 oldPlugin,
128                 null);
129         ContentReference[] newReferences =
130             feature
131                 .getFeatureContentProvider()
132                 .getPluginEntryContentReferences(
133                 newPlugin,
134                 null);
135
136         URL JavaDoc newURL = new URL JavaDoc(consumer.getFeature().getSite().getURL(),
137                 Site.DEFAULT_PLUGIN_PATH
138                         + newPlugin.getVersionedIdentifier().toString());
139         String JavaDoc pluginPath = newURL.getFile();
140         for (int i = 0; i < oldReferences.length; i++) {
141             if (isPluginManifest(oldReferences[i])
142                 || referenceExists(newReferences, oldReferences[i]))
143                 continue;
144
145             InputStream JavaDoc input = null;
146             try {
147                 input = oldReferences[i].getInputStream();
148                 File JavaDoc targetFile =
149                     new File JavaDoc(pluginPath, oldReferences[i].getIdentifier());
150                 UpdateManagerUtils.copyToLocal(
151                     input,
152                     targetFile.getAbsolutePath(),
153                     null);
154                 UpdateManagerUtils.checkPermissions(
155                     oldReferences[i],
156                     pluginPath);
157                 // 20305
158
} catch (IOException JavaDoc e) {
159                 continue;
160             } finally {
161                 if(input != null){
162                     try{
163                         input.close();
164                     } catch (IOException JavaDoc ioe) {
165                     }
166                 }
167             }
168         }
169     }
170
171     protected boolean isPluginManifest(ContentReference ref) {
172         String JavaDoc id = ref.getIdentifier();
173         return PLUGIN_XML.equals(id) || FRAGMENT_XML.equals(id) || META_MANIFEST.equals(id);
174     }
175 }
176
Popular Tags