KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.eclipse.update.internal.core;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.update.core.*;
22 import org.eclipse.update.core.model.*;
23
24 /**
25  * ContentConsummer for a SiteFile
26  */

27 public class SiteFileContentConsumer extends SiteContentConsumer {
28
29     private IFeature feature;
30     private boolean closed = false;
31
32     // recovery
33
private String JavaDoc oldPath;
34     private String JavaDoc newPath;
35
36     // for abort
37
private List JavaDoc /* of SiteFilePluginContentConsumer */
38     contentConsumers;
39     private List JavaDoc /*of path as String */
40     installedFiles;
41     
42     // PERF: new instance variable
43
private SiteFileFactory archiveFactory = new SiteFileFactory();
44
45     /*
46      * Constructor
47      */

48     public SiteFileContentConsumer(IFeature feature) {
49         this.feature = feature;
50         installedFiles = new ArrayList JavaDoc();
51     }
52
53     /*
54      * Returns the path in which the Feature will be installed
55      */

56     private String JavaDoc getFeaturePath() throws CoreException {
57         String JavaDoc featurePath = null;
58         try {
59             VersionedIdentifier featureIdentifier = feature.getVersionedIdentifier();
60             String JavaDoc path = Site.DEFAULT_INSTALLED_FEATURE_PATH + featureIdentifier.toString() + File.separator;
61             URL newURL = new URL(getSite().getURL(), path);
62             featurePath = newURL.getFile();
63         } catch (MalformedURLException e) {
64             throw Utilities.newCoreException(Messages.SiteFileContentConsumer_UnableToCreateURL + e.getMessage(), e);
65         }
66         return featurePath;
67     }
68
69     /*
70      * @see ISiteContentConsumer#open(INonPluginEntry)
71      */

72     public IContentConsumer open(INonPluginEntry nonPluginEntry) throws CoreException {
73         return new SiteFileNonPluginContentConsumer(getFeaturePath());
74     }
75
76     /*
77      * @see ISiteContentConsumer#open(IPluginEntry)
78      */

79     public IContentConsumer open(IPluginEntry pluginEntry) throws CoreException {
80         ContentConsumer cons;
81         if(pluginEntry instanceof PluginEntryModel && !((PluginEntryModel)pluginEntry).isUnpack()){
82             // plugin can run from a jar
83
cons = new SiteFilePackedPluginContentConsumer(pluginEntry, getSite());
84         } else{
85             // plugin must be unpacked
86
cons = new SiteFilePluginContentConsumer(pluginEntry, getSite());
87         }
88         addContentConsumers(cons);
89         return cons;
90     }
91
92     /*
93      * @see ISiteContentConsumer#store(ContentReference, IProgressMonitor)
94      */

95     public void store(ContentReference contentReference, IProgressMonitor monitor) throws CoreException {
96
97         if (closed) {
98             UpdateCore.warn("Attempt to store in a closed SiteFileContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
99
return;
100         }
101
102         InputStream inStream = null;
103         String JavaDoc featurePath = getFeaturePath();
104         String JavaDoc contentKey = contentReference.getIdentifier();
105         featurePath += contentKey;
106
107         // error recovery
108
if (featurePath.endsWith("\\"+Feature.FEATURE_XML) || featurePath.endsWith("/"+Feature.FEATURE_XML)) { //$NON-NLS-1$ //$NON-NLS-2$
109
oldPath = featurePath.replace(File.separatorChar, '/');
110             File localFile = new File(oldPath);
111             if (localFile.exists()) {
112                 throw Utilities.newCoreException(NLS.bind(Messages.UpdateManagerUtils_FileAlreadyExists, (new Object JavaDoc[] { localFile })), null);
113             }
114             featurePath = ErrorRecoveryLog.getLocalRandomIdentifier(featurePath);
115             newPath = featurePath;
116             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.FEATURE_ENTRY, featurePath);
117         }
118
119         try {
120             inStream = contentReference.getInputStream();
121             UpdateManagerUtils.copyToLocal(inStream, featurePath, null);
122             UpdateManagerUtils.checkPermissions(contentReference, featurePath); // 20305
123
installedFiles.add(featurePath);
124         } catch (IOException e) {
125             throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String JavaDoc[] { featurePath })), e);
126         } finally {
127             if (inStream != null) {
128                 try {
129                     // close stream
130
inStream.close();
131                 } catch (IOException e) {
132                 }
133             }
134         }
135
136     }
137
138     /*
139      * @see ISiteContentConsumer#close()
140      */

141     public IFeatureReference close() throws CoreException {
142
143         if (closed)
144             UpdateCore.warn("Attempt to close a closed SiteFileContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
145

146         // create a new Feature reference to be added to the site
147
SiteFeatureReference ref = new SiteFeatureReference();
148         ref.setSite(getSite());
149         File file = null;
150
151         try {
152             file = new File(getFeaturePath());
153             ref.setURL(file.toURL());
154         } catch (MalformedURLException e) {
155             throw Utilities.newCoreException(NLS.bind(Messages.SiteFileContentConsumer_UnableToCreateURLForFile, (new String JavaDoc[] { file.getAbsolutePath() })), e);
156         }
157
158         //rename file back
159
if (newPath != null) {
160             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.RENAME_ENTRY, newPath);
161             boolean sucess = false;
162             File fileToRename = new File(newPath);
163             if (fileToRename.exists()) {
164                 File renamedFile = new File(oldPath);
165                 if (renamedFile.exists()) {
166                     UpdateManagerUtils.removeFromFileSystem(renamedFile);
167                     UpdateCore.warn("Removing already existing file:" + oldPath); //$NON-NLS-1$
168
}
169                 sucess = fileToRename.renameTo(renamedFile);
170             }
171             if (!sucess) {
172                 String JavaDoc msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String JavaDoc[] { newPath, oldPath }));
173                 throw Utilities.newCoreException(msg, new Exception JavaDoc(msg));
174             }
175         }
176
177         // close plugin and non plugin content consumer
178
if (contentConsumers != null) {
179             Iterator JavaDoc iter = contentConsumers.iterator();
180             while (iter.hasNext()) {
181                 ContentConsumer element = (ContentConsumer) iter.next();
182                 element.close();
183             }
184         }
185         contentConsumers = null;
186
187         if (ref != null) {
188             // the feature MUST have renamed the plugins at that point
189
// (by closing the PluginContentConsumer)
190
commitPlugins(ref);
191             ref.markReadOnly();
192         }
193
194         closed = true;
195         return ref;
196     }
197
198     /*
199      * @see ISiteContentConsumer#abort()
200      */

201     public void abort() throws CoreException {
202
203         if (closed) {
204             UpdateCore.warn("Attempt to abort a closed SiteFileContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
205
return;
206         }
207
208         //abort all plugins content consumer opened
209
if (contentConsumers != null) {
210             Iterator JavaDoc iter = contentConsumers.iterator();
211             while (iter.hasNext()) {
212                 Object JavaDoc element = iter.next();
213                 if (element instanceof SiteFilePluginContentConsumer) {
214                     ((SiteFilePluginContentConsumer)element).abort();
215                 } else if (element instanceof SiteFilePackedPluginContentConsumer){
216                     ((SiteFilePackedPluginContentConsumer)element).abort();
217                 }
218                 
219             }
220         }
221         contentConsumers = null;
222         boolean sucess = true;
223
224         //Remove feature.xml first if it exists
225
if (oldPath != null) {
226             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.DELETE_ENTRY, oldPath);
227             File fileToDelete = new File(oldPath);
228             if (fileToDelete.exists()) {
229                 sucess = fileToDelete.delete();
230             }
231         }
232
233         if (!sucess) {
234             String JavaDoc msg = NLS.bind(Messages.SiteFileContentConsumer_unableToDelete, (new String JavaDoc[] { oldPath }));
235             UpdateCore.log(msg, null);
236         } else {
237             // remove the feature files;
238
Iterator JavaDoc iter = installedFiles.iterator();
239             File featureFile = null;
240             while (iter.hasNext()) {
241                 String JavaDoc path = (String JavaDoc) iter.next();
242                 featureFile = new File(path);
243                 UpdateManagerUtils.removeFromFileSystem(featureFile);
244             }
245
246             // remove the feature directory if empty
247
String JavaDoc featurePath = getFeaturePath();
248             UpdateManagerUtils.removeEmptyDirectoriesFromFileSystem(new File(featurePath));
249         }
250         closed = true;
251         return;
252     }
253
254     /*
255      * commit the plugins installed as archive on the site
256      * (creates the map between the plugin id and the location of the plugin)
257      */

258     private void commitPlugins(IFeatureReference localFeatureReference) throws CoreException {
259     
260         // get the feature
261
((SiteFile) getSite()).addFeatureReferenceModel((SiteFeatureReferenceModel) localFeatureReference);
262         IFeature localFeature = null;
263         try {
264             localFeature = localFeatureReference.getFeature(null);
265         } catch (CoreException e) {
266             UpdateCore.warn(null, e);
267             return;
268         }
269     
270         if (localFeature == null)
271             return;
272     
273         // add the installed plugins directories as archives entry
274
ArchiveReferenceModel archive = null;
275         IPluginEntry[] pluginEntries = localFeature.getPluginEntries();
276         for (int i = 0; i < pluginEntries.length; i++) {
277             String JavaDoc versionId = pluginEntries[i].getVersionedIdentifier().toString();
278             String JavaDoc pluginID = Site.DEFAULT_PLUGIN_PATH + versionId + FeaturePackagedContentProvider.JAR_EXTENSION;
279             archive = archiveFactory.createArchiveReferenceModel();
280             archive.setPath(pluginID);
281             try {
282                 URL url = null;
283                 if (pluginEntries[i] instanceof PluginEntryModel
284                         && !((PluginEntryModel) pluginEntries[i]).isUnpack()) {
285                     url = new URL(getSite().getURL(), Site.DEFAULT_PLUGIN_PATH + versionId + ".jar"); //$NON-NLS-1$
286
} else {
287                     url = new URL(getSite().getURL(), Site.DEFAULT_PLUGIN_PATH + versionId + File.separator);
288                 }
289                 archive.setURLString(url.toExternalForm());
290                 archive.resolve(url, null);
291                 ((SiteFile) getSite()).addArchiveReferenceModel(archive);
292             } catch (MalformedURLException e) {
293     
294                 String JavaDoc urlString = (getSite().getURL() != null) ? getSite().getURL().toExternalForm() : ""; //$NON-NLS-1$
295
urlString += Site.DEFAULT_PLUGIN_PATH + pluginEntries[i].toString();
296                 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_UnableToCreateURL, (new String JavaDoc[] { urlString })), e);
297             }
298         }
299         return;
300     }
301
302     /*
303      * Adds a SiteFilePluginContentConsumer to the list
304      */

305     private void addContentConsumers(ContentConsumer cons) {
306         if (contentConsumers == null)
307             contentConsumers = new ArrayList JavaDoc();
308         contentConsumers.add(cons);
309     }
310
311 }
312
Popular Tags