1 11 package org.eclipse.update.internal.core; 12 import java.io.*; 13 import java.net.*; 14 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.update.core.*; 18 19 23 public class SiteFilePackedPluginContentConsumer extends ContentConsumer { 24 25 private IPluginEntry pluginEntry; 26 private ISite site; 27 private boolean closed = false; 28 private String jarPath; 29 private String tempPath; 30 31 34 public SiteFilePackedPluginContentConsumer(IPluginEntry pluginEntry, ISite site) { 35 this.pluginEntry = pluginEntry; 36 this.site = site; 37 } 38 39 42 public void store(ContentReference contentReference, IProgressMonitor monitor) throws CoreException { 43 InputStream inStream = null; 44 45 if (closed) { 46 UpdateCore.warn("Attempt to store in a closed SiteFilePluginContentConsumer", new Exception ()); return; 48 } 49 50 try { 51 URL newURL = new URL(site.getURL(), Site.DEFAULT_PLUGIN_PATH + pluginEntry.getVersionedIdentifier().toString() + ".jar"); inStream = contentReference.getInputStream(); 53 jarPath = newURL.getFile().replace(File.separatorChar, '/'); 54 File jarFile = new File(jarPath); 55 if (jarFile.exists()) { 56 throw Utilities.newCoreException(NLS.bind(Messages.UpdateManagerUtils_FileAlreadyExists, (new Object [] { jarFile })), null); 57 } 58 tempPath= ErrorRecoveryLog.getLocalRandomIdentifier(jarPath+".tmp"); ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.BUNDLE_JAR_ENTRY, tempPath); 61 UpdateManagerUtils.copyToLocal(inStream, tempPath, null); 63 } catch (IOException e) { 64 throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String [] { tempPath })), e); 65 } finally { 66 if (inStream != null) { 67 try { 68 inStream.close(); 70 } catch (IOException e) { 71 } 72 } 73 } 74 } 75 76 79 public void close() throws CoreException { 80 81 if (closed) { 82 UpdateCore.warn("Attempt to close a closed SiteFilePluginContentConsumer", new Exception ()); return; 84 } 85 86 if (tempPath != null) { 87 ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.RENAME_ENTRY, tempPath); 89 File fileToRename = new File(tempPath); 90 boolean sucess = false; 91 if (fileToRename.exists()) { 92 File renamedFile = new File(jarPath); 93 sucess = fileToRename.renameTo(renamedFile); 94 } 95 if (!sucess) { 96 String msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String [] { tempPath, jarPath })); 97 throw Utilities.newCoreException(msg, new Exception (msg)); 98 } 99 } 100 101 if (site instanceof SiteFile) 102 ((SiteFile) site).addPluginEntry(pluginEntry); 103 closed = true; 104 } 105 106 109 public void abort() throws CoreException { 110 111 if (closed) { 112 UpdateCore.warn("Attempt to abort a closed SiteFilePluginContentConsumer", new Exception ()); return; 114 } 115 116 boolean sucess = true; 117 118 if (jarPath != null) { 120 ErrorRecoveryLog.getLog().appendPath(ErrorRecoveryLog.DELETE_ENTRY, jarPath); 121 File fileToRemove = new File(jarPath); 122 123 if (fileToRemove.exists()) { 124 sucess = fileToRemove.delete(); 125 } 126 } 127 128 if (!sucess) { 129 String msg = NLS.bind(Messages.SiteFilePackedPluginContentConsumer_unableToDelete, (new String [] { jarPath })); 130 UpdateCore.log(msg, null); 131 } 132 closed = true; 133 } 134 135 } 136 | Popular Tags |