1 11 package org.eclipse.update.internal.core; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.update.core.*; 24 25 28 public class SiteFilePluginContentConsumer extends ContentConsumer { 29 30 private IPluginEntry pluginEntry; 31 private ISite site; 32 private boolean closed = false; 33 34 private Map renames = new HashMap (2); 37 38 private List 40 installedFiles; 41 42 45 public SiteFilePluginContentConsumer(IPluginEntry pluginEntry, ISite site) { 46 this.pluginEntry = pluginEntry; 47 this.site = site; 48 installedFiles = new ArrayList (); 49 } 50 51 54 public void store(ContentReference contentReference, IProgressMonitor monitor) throws CoreException { 55 InputStream inStream = null; 56 String pluginPath = null; 57 58 if (closed) { 59 UpdateCore.warn("Attempt to store in a closed SiteFilePluginContentConsumer", new Exception ()); 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 contentKey = contentReference.getIdentifier(); 67 inStream = contentReference.getInputStream(); 68 pluginPath += pluginPath.endsWith(File.separator) ? contentKey : File.separator + contentKey; 69 70 String logEntry=null; 72 if ("plugin.xml".equals(contentKey)) { logEntry=ErrorRecoveryLog.PLUGIN_ENTRY; 74 } else if ("fragment.xml".equals(contentKey)) { logEntry=ErrorRecoveryLog.FRAGMENT_ENTRY; 76 } else if ("META-INF/MANIFEST.MF".equals(contentKey)) { logEntry=ErrorRecoveryLog.BUNDLE_MANIFEST_ENTRY; 78 } 79 if (logEntry!=null) { 80 String 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 [] { localFile })), null); 84 } 85 pluginPath = ErrorRecoveryLog.getLocalRandomIdentifier(pluginPath); 86 renames.put(pluginPath, originalName); 87 ErrorRecoveryLog.getLog().appendPath(logEntry, pluginPath); 88 } 89 UpdateManagerUtils.copyToLocal(inStream, pluginPath, null); 91 UpdateManagerUtils.checkPermissions(contentReference, pluginPath); installedFiles.add(pluginPath); 93 } catch (IOException e) { 94 throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String [] { pluginPath })), e); 95 } finally { 96 if (inStream != null) { 97 try { 98 inStream.close(); 100 } catch (IOException e) { 101 } 102 } 103 } 104 } 105 106 109 public void close() throws CoreException { 110 111 if (closed) { 112 UpdateCore.warn("Attempt to close a closed SiteFilePluginContentConsumer", new Exception ()); return; 114 } 115 116 for(Iterator it = renames.entrySet().iterator(); it.hasNext();){ 117 Map.Entry entry = (Map.Entry )it.next(); 119 String temporary = (String ) entry.getKey(); 120 String original = (String ) 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 msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String [] { temporary, original })); 130 throw Utilities.newCoreException(msg, new Exception (msg)); 131 } 132 } 133 134 if (site instanceof SiteFile) 135 ((SiteFile) site).addPluginEntry(pluginEntry); 136 closed = true; 137 } 138 139 142 public void abort() throws CoreException { 143 144 if (closed) { 145 UpdateCore.warn("Attempt to abort a closed SiteFilePluginContentConsumer", new Exception ()); return; 147 } 148 149 boolean success = true; 150 InstallRegistry.unregisterPlugin(pluginEntry); 151 152 for(Iterator it = renames.values().iterator(); it.hasNext();){ 154 String originalName = (String ) 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 msg = NLS.bind(Messages.SiteFilePluginContentConsumer_unableToDelete, (new String [] { originalName })); 161 UpdateCore.log(msg, null); 162 success = false; 163 } 164 } 165 } 166 167 if (success) { 168 Iterator iter = installedFiles.iterator(); 170 File featureFile = null; 171 while (iter.hasNext()) { 172 String path = (String ) iter.next(); 173 featureFile = new File(path); 174 UpdateManagerUtils.removeFromFileSystem(featureFile); 175 } 176 177 try { 179 URL newURL = new URL(site.getURL(), Site.DEFAULT_PLUGIN_PATH + pluginEntry.getVersionedIdentifier().toString()); 180 String 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 |