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.Iterator ; 17 import java.util.List ; 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 27 public class SiteFileContentConsumer extends SiteContentConsumer { 28 29 private IFeature feature; 30 private boolean closed = false; 31 32 private String oldPath; 34 private String newPath; 35 36 private List 38 contentConsumers; 39 private List 40 installedFiles; 41 42 private SiteFileFactory archiveFactory = new SiteFileFactory(); 44 45 48 public SiteFileContentConsumer(IFeature feature) { 49 this.feature = feature; 50 installedFiles = new ArrayList (); 51 } 52 53 56 private String getFeaturePath() throws CoreException { 57 String featurePath = null; 58 try { 59 VersionedIdentifier featureIdentifier = feature.getVersionedIdentifier(); 60 String 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 72 public IContentConsumer open(INonPluginEntry nonPluginEntry) throws CoreException { 73 return new SiteFileNonPluginContentConsumer(getFeaturePath()); 74 } 75 76 79 public IContentConsumer open(IPluginEntry pluginEntry) throws CoreException { 80 ContentConsumer cons; 81 if(pluginEntry instanceof PluginEntryModel && !((PluginEntryModel)pluginEntry).isUnpack()){ 82 cons = new SiteFilePackedPluginContentConsumer(pluginEntry, getSite()); 84 } else{ 85 cons = new SiteFilePluginContentConsumer(pluginEntry, getSite()); 87 } 88 addContentConsumers(cons); 89 return cons; 90 } 91 92 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 ()); return; 100 } 101 102 InputStream inStream = null; 103 String featurePath = getFeaturePath(); 104 String contentKey = contentReference.getIdentifier(); 105 featurePath += contentKey; 106 107 if (featurePath.endsWith("\\"+Feature.FEATURE_XML) || featurePath.endsWith("/"+Feature.FEATURE_XML)) { 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 [] { 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); installedFiles.add(featurePath); 124 } catch (IOException e) { 125 throw Utilities.newCoreException(NLS.bind(Messages.GlobalConsumer_ErrorCreatingFile, (new String [] { featurePath })), e); 126 } finally { 127 if (inStream != null) { 128 try { 129 inStream.close(); 131 } catch (IOException e) { 132 } 133 } 134 } 135 136 } 137 138 141 public IFeatureReference close() throws CoreException { 142 143 if (closed) 144 UpdateCore.warn("Attempt to close a closed SiteFileContentConsumer", new Exception ()); 146 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 [] { file.getAbsolutePath() })), e); 156 } 157 158 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); } 169 sucess = fileToRename.renameTo(renamedFile); 170 } 171 if (!sucess) { 172 String msg = NLS.bind(Messages.ContentConsumer_UnableToRename, (new String [] { newPath, oldPath })); 173 throw Utilities.newCoreException(msg, new Exception (msg)); 174 } 175 } 176 177 if (contentConsumers != null) { 179 Iterator 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 commitPlugins(ref); 191 ref.markReadOnly(); 192 } 193 194 closed = true; 195 return ref; 196 } 197 198 201 public void abort() throws CoreException { 202 203 if (closed) { 204 UpdateCore.warn("Attempt to abort a closed SiteFileContentConsumer", new Exception ()); return; 206 } 207 208 if (contentConsumers != null) { 210 Iterator iter = contentConsumers.iterator(); 211 while (iter.hasNext()) { 212 Object 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 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 msg = NLS.bind(Messages.SiteFileContentConsumer_unableToDelete, (new String [] { oldPath })); 235 UpdateCore.log(msg, null); 236 } else { 237 Iterator iter = installedFiles.iterator(); 239 File featureFile = null; 240 while (iter.hasNext()) { 241 String path = (String ) iter.next(); 242 featureFile = new File(path); 243 UpdateManagerUtils.removeFromFileSystem(featureFile); 244 } 245 246 String featurePath = getFeaturePath(); 248 UpdateManagerUtils.removeEmptyDirectoriesFromFileSystem(new File(featurePath)); 249 } 250 closed = true; 251 return; 252 } 253 254 258 private void commitPlugins(IFeatureReference localFeatureReference) throws CoreException { 259 260 ((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 ArchiveReferenceModel archive = null; 275 IPluginEntry[] pluginEntries = localFeature.getPluginEntries(); 276 for (int i = 0; i < pluginEntries.length; i++) { 277 String versionId = pluginEntries[i].getVersionedIdentifier().toString(); 278 String 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"); } 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 urlString = (getSite().getURL() != null) ? getSite().getURL().toExternalForm() : ""; urlString += Site.DEFAULT_PLUGIN_PATH + pluginEntries[i].toString(); 296 throw Utilities.newCoreException(NLS.bind(Messages.SiteFile_UnableToCreateURL, (new String [] { urlString })), e); 297 } 298 } 299 return; 300 } 301 302 305 private void addContentConsumers(ContentConsumer cons) { 306 if (contentConsumers == null) 307 contentConsumers = new ArrayList (); 308 contentConsumers.add(cons); 309 } 310 311 } 312 | Popular Tags |