KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.update.core.*;
24
25 /**
26  * Plugin Content Consumer on a Site
27  */

28 public class SiteFilePluginContentConsumer extends ContentConsumer {
29
30     private IPluginEntry pluginEntry;
31     private ISite site;
32     private boolean closed = false;
33
34     // recovery
35
// temporary name to original name map
36
private Map JavaDoc renames = new HashMap JavaDoc(2);
37
38     // for abort
39
private List JavaDoc /*of path as String */
40     installedFiles;
41
42     /*
43      * Constructor
44      */

45     public SiteFilePluginContentConsumer(IPluginEntry pluginEntry, ISite site) {
46         this.pluginEntry = pluginEntry;
47         this.site = site;
48         installedFiles = new ArrayList JavaDoc();
49     }
50
51     /*
52      * @see ISiteContentConsumer#store(ContentReference, IProgressMonitor)
53      */

54     public void store(ContentReference contentReference, IProgressMonitor monitor) throws CoreException {
55         InputStream inStream = null;
56         String JavaDoc pluginPath = null;
57
58         if (closed) {
59             UpdateCore.warn("Attempt to store in a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
60
return;
61         }
62
63         try {
64             URL newURL = new URL(site.getURL(), Site.DEFAULT_PLUGIN_PATH + pluginEntry.getVersionedIdentifier().toString());
65             pluginPath = newURL.getFile();
66             String JavaDoc contentKey = contentReference.getIdentifier();
67             inStream = contentReference.getInputStream();
68             pluginPath += pluginPath.endsWith(File.separator) ? contentKey : File.separator + contentKey;
69
70             // error recovery
71
String JavaDoc logEntry=null;
72             if ("plugin.xml".equals(contentKey)) { //$NON-NLS-1$
73
logEntry=ErrorRecoveryLog.PLUGIN_ENTRY;
74             } else if ("fragment.xml".equals(contentKey)) { //$NON-NLS-1$
75
logEntry=ErrorRecoveryLog.FRAGMENT_ENTRY;
76             } else if ("META-INF/MANIFEST.MF".equals(contentKey)) { //$NON-NLS-1$
77
logEntry=ErrorRecoveryLog.BUNDLE_MANIFEST_ENTRY;
78             }
79             if (logEntry!=null) {
80                 String JavaDoc originalName = pluginPath.replace(File.separatorChar, '/');
81                 File localFile = new File(originalName);
82                 if (localFile.exists()) {
83                     throw Utilities.newCoreException(NLS.bind(Messages.UpdateManagerUtils_FileAlreadyExists, (new Object JavaDoc[] { localFile })), null);
84                 }
85                 pluginPath = ErrorRecoveryLog.getLocalRandomIdentifier(pluginPath);
86                 renames.put(pluginPath, originalName);
87                 ErrorRecoveryLog.getLog().appendPath(logEntry, pluginPath);
88             }
89             //
90
UpdateManagerUtils.copyToLocal(inStream, pluginPath, null);
91             UpdateManagerUtils.checkPermissions(contentReference, pluginPath); // 20305
92
installedFiles.add(pluginPath);
93         } catch (IOException e) {
94             throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String JavaDoc[] { pluginPath })), e);
95         } finally {
96             if (inStream != null) {
97                 try {
98                     // close stream
99
inStream.close();
100                 } catch (IOException e) {
101                 }
102             }
103         }
104     }
105
106     /*
107      * @see ISiteContentConsumer#close()
108      */

109     public void close() throws CoreException {
110
111         if (closed) {
112             UpdateCore.warn("Attempt to close a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
113
return;
114         }
115
116         for(Iterator JavaDoc it = renames.entrySet().iterator(); it.hasNext();){
117             // rename file
118
Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
119             String JavaDoc temporary = (String JavaDoc) entry.getKey();
120             String JavaDoc original = (String JavaDoc) entry.getValue();
121             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.RENAME_ENTRY, temporary);
122             File fileToRename = new File(temporary);
123             boolean sucess = false;
124             if (fileToRename.exists()) {
125                 File renamedFile = new File(original);
126                 sucess = fileToRename.renameTo(renamedFile);
127             }
128             if (!sucess) {
129                 String JavaDoc msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String JavaDoc[] { temporary, original }));
130                 throw Utilities.newCoreException(msg, new Exception JavaDoc(msg));
131             }
132         }
133
134         if (site instanceof SiteFile)
135              ((SiteFile) site).addPluginEntry(pluginEntry);
136         closed = true;
137     }
138
139     /*
140      *
141      */

142     public void abort() throws CoreException {
143
144         if (closed) {
145             UpdateCore.warn("Attempt to abort a closed SiteFilePluginContentConsumer", new Exception JavaDoc()); //$NON-NLS-1$
146
return;
147         }
148
149         boolean success = true;
150         InstallRegistry.unregisterPlugin(pluginEntry);
151
152         // delete plugin manifests first
153
for(Iterator JavaDoc it = renames.values().iterator(); it.hasNext();){
154             String JavaDoc originalName = (String JavaDoc) it.next();
155
156             ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.DELETE_ENTRY, originalName);
157             File fileToRemove = new File(originalName);
158             if (fileToRemove.exists()) {
159                 if(!fileToRemove.delete()){
160                     String JavaDoc msg = NLS.bind(Messages.SiteFilePluginContentConsumer_unableToDelete, (new String JavaDoc[] { originalName }));
161                     UpdateCore.log(msg, null);
162                     success = false;
163                 }
164             }
165         }
166
167         if (success) {
168             // remove the plugin files;
169
Iterator JavaDoc iter = installedFiles.iterator();
170             File featureFile = null;
171             while (iter.hasNext()) {
172                 String JavaDoc path = (String JavaDoc) iter.next();
173                 featureFile = new File(path);
174                 UpdateManagerUtils.removeFromFileSystem(featureFile);
175             }
176
177             // remove the plugin directory if empty
178
try {
179                 URL newURL = new URL(site.getURL(), Site.DEFAULT_PLUGIN_PATH + pluginEntry.getVersionedIdentifier().toString());
180                 String JavaDoc pluginPath = newURL.getFile();
181                 UpdateManagerUtils.removeEmptyDirectoriesFromFileSystem(new File(pluginPath));
182             } catch (MalformedURLException e) {
183                 throw Utilities.newCoreException(e.getMessage(), e);
184             }
185         }
186         closed = true;
187     }
188
189 }
190
Popular Tags