1 11 package org.eclipse.update.core; 12 13 import java.io.BufferedOutputStream ; 14 import java.io.File ; 15 import java.io.FileNotFoundException ; 16 import java.io.FileOutputStream ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.net.URL ; 21 import java.util.Date ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Properties ; 26 import java.util.Set ; 27 import java.util.StringTokenizer ; 28 29 import org.eclipse.core.runtime.CoreException; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.osgi.util.NLS; 33 import org.eclipse.update.core.model.ContentEntryModel; 34 import org.eclipse.update.core.model.InstallAbortedException; 35 import org.eclipse.update.core.model.NonPluginEntryModel; 36 import org.eclipse.update.core.model.PluginEntryModel; 37 import org.eclipse.update.internal.core.FatalIOException; 38 import org.eclipse.update.internal.core.FeatureDownloadException; 39 import org.eclipse.update.internal.core.FileFragment; 40 import org.eclipse.update.internal.core.InternalSiteManager; 41 import org.eclipse.update.internal.core.LockManager; 42 import org.eclipse.update.internal.core.Messages; 43 import org.eclipse.update.internal.core.UpdateCore; 44 import org.eclipse.update.internal.core.UpdateManagerUtils; 45 46 62 public abstract class FeatureContentProvider 63 implements IFeatureContentProvider { 64 65 private static final boolean SWITCH_COPY_LOCAL = true; 66 68 70 73 public class FileFilter { 74 75 private IPath filterPath = null; 76 77 80 public FileFilter(String filter) { 81 super(); 82 this.filterPath = new Path(filter); 83 } 84 85 88 public boolean accept(String name) { 89 90 if (name == null) 91 return false; 92 93 IPath namePath = new Path(name); 96 if (filterPath.lastSegment().indexOf('*') == -1) { 97 return filterPath.equals(namePath); 98 } 99 100 String extension = filterPath.getFileExtension(); 103 if (extension != null && !extension.equals("*")) { if (!extension.equalsIgnoreCase(namePath.getFileExtension())) 105 return false; 106 } else { 107 IPath noExtension = filterPath.removeFileExtension(); 108 String fileName = noExtension.lastSegment(); 109 if (!fileName.equals("*")) { if (!namePath.lastSegment().startsWith(fileName)) 111 return false; 112 } 113 } 114 115 IPath p1 = namePath.removeLastSegments(1); 117 IPath p2 = filterPath.removeLastSegments(1); 118 return p1.equals(p2); 119 } 120 121 } 122 123 private URL base; 124 private IFeature feature; 125 private File tmpDir; public static final String JAR_EXTENSION = ".jar"; 128 private static final String DOT_PERMISSIONS = "permissions.properties"; private static final String EXECUTABLES = "permissions.executable"; 131 139 public FeatureContentProvider(URL base) { 140 this.base = base; 141 this.feature = null; 142 } 143 144 149 public URL getURL() { 150 return base; 151 } 152 153 158 public IFeature getFeature() { 159 return feature; 160 } 161 162 167 public void setFeature(IFeature feature) { 168 this.feature = feature; 169 } 170 171 184 public ContentReference asLocalReference(ContentReference ref, InstallMonitor monitor) throws IOException , CoreException { 185 186 if (ref.isLocalReference()) 188 return ref; 189 190 String key = ref.toString(); 192 193 File localFile = null; 196 FileFragment localFileFragment = null; 197 Object keyLock = LockManager.getLock(key); 198 synchronized (keyLock) { 199 localFile = Utilities.lookupLocalFile(key); 200 if (localFile != null) { 201 try { 204 if (UpdateManagerUtils.isSameTimestamp(ref.asURL(), localFile.lastModified())) 205 return ref.createContentReference(ref.getIdentifier(), localFile); 206 } catch(FatalIOException e) { 207 throw e; 208 } catch(IOException e) { 209 throw new FeatureDownloadException(NLS.bind(Messages.FeatureContentProvider_ExceptionDownloading, (new Object [] {getURL().toExternalForm()})), e); 210 } 211 } 212 213 if (localFile == null) { 214 localFileFragment = UpdateManagerUtils.lookupLocalFileFragment(key); 215 } 216 InputStream is = null; 219 OutputStream os = null; 220 long bytesCopied = 0; 221 long inputLength = 0; 222 boolean success = false; 223 if (monitor != null) { 224 monitor.saveState(); 225 monitor.setTaskName(Messages.FeatureContentProvider_Downloading); 226 monitor.subTask(ref.getIdentifier() + " "); try { 228 monitor.setTotalCount(ref.getInputSize()); 229 } catch (FatalIOException e) { 230 throw e; 231 } catch (IOException e) { 232 throw new FeatureDownloadException(NLS.bind(Messages.FeatureContentProvider_ExceptionDownloading, (new Object [] {getURL().toExternalForm()})), e); 233 } 234 monitor.showCopyDetails(true); 235 } 236 237 try { 238 if (localFileFragment != null && "http".equals(ref.asURL().getProtocol())) { localFile = localFileFragment.getFile(); 241 try { 242 is = ref.getPartialInputStream(localFileFragment.getSize()); 244 inputLength = ref.getInputSize() - localFileFragment.getSize(); 245 os = new BufferedOutputStream ( 247 new FileOutputStream (localFile.getPath(), true)); 250 } catch (FatalIOException e) { 251 throw e; 252 } catch (IOException e) { 253 try { 254 if (is != null) 255 is.close(); 256 } catch (IOException ioe) { 257 } 258 is = null; 259 os = null; 260 localFileFragment = null; 261 throw new FeatureDownloadException(NLS.bind(Messages.FeatureContentProvider_ExceptionDownloading, (new Object [] {getURL().toExternalForm()})), e); 262 } 263 } 264 if (is == null) { 265 localFile = Utilities.createLocalFile(getWorkingDirectory(), null); 267 try { 268 is = ref.getInputStream(); 269 inputLength = ref.getInputSize(); 270 } catch (FatalIOException e) { 271 throw Utilities.newCoreException(NLS.bind(Messages.FeatureContentProvider_UnableToRetrieve, (new Object [] {ref})), e); 272 } catch (IOException e) { 273 throw new FeatureDownloadException(NLS.bind(Messages.FeatureContentProvider_ExceptionDownloading, (new Object [] {getURL().toExternalForm()})), e); 274 } 275 276 try { 277 os = new BufferedOutputStream (new FileOutputStream (localFile)); 278 } catch (FileNotFoundException e) { 279 throw Utilities.newCoreException(NLS.bind(Messages.FeatureContentProvider_UnableToCreate, (new Object [] {localFile})), e); 280 } 281 } 282 283 Date start = new Date (); 284 if (localFileFragment != null) { 285 bytesCopied = localFileFragment.getSize(); 286 if (monitor != null) { 287 monitor.setCopyCount(bytesCopied); 288 } 289 } 290 291 long offset = UpdateManagerUtils.copy(is, os, monitor, inputLength); 293 if (offset != -1) { 294 bytesCopied += offset; 295 if (bytesCopied > 0) { 296 UpdateManagerUtils.mapLocalFileFragment(key, new FileFragment(localFile, bytesCopied)); 298 } 299 if (monitor.isCanceled()) { 300 String msg = Messages.Feature_InstallationCancelled; 301 throw new InstallAbortedException(msg, null); 302 } else { 303 throw new FeatureDownloadException(NLS.bind(Messages.FeatureContentProvider_ExceptionDownloading, (new Object [] {getURL().toExternalForm()})), new IOException ()); 304 } 305 } else { 306 UpdateManagerUtils.unMapLocalFileFragment(key); 307 } 308 309 Date stop = new Date (); 310 long timeInseconds = (stop.getTime() - start.getTime()) / 1000; 311 InternalSiteManager.downloaded( 313 ref.getInputSize(), 314 (timeInseconds), 315 ref.asURL()); 316 317 success = true; 318 Utilities.mapLocalFile(key, localFile); 321 322 330 } catch (ClassCastException e) { 331 throw Utilities.newCoreException( 332 NLS.bind(Messages.FeatureContentProvider_UnableToCreate, (new Object [] { localFile })), 333 e); 334 } finally { 335 if (success && is != null) 338 try { 339 is.close(); 340 } catch (IOException e) { 341 } 342 if (os != null) 343 try { 344 os.close(); } catch (IOException e) { 346 } 347 348 if (success || bytesCopied > 0) { 349 localFile.setLastModified(ref.getLastModified()); 352 } 353 if (monitor != null) 354 monitor.restoreState(); 355 } 356 LockManager.returnLock(key); 357 } ContentReference reference = 359 ref.createContentReference(ref.getIdentifier(), localFile); 360 361 UpdateCore.getPlugin().getUpdateSession().markVisited(ref.asURL()); 362 363 return reference; 364 } 365 366 379 public File asLocalFile(ContentReference ref, InstallMonitor monitor) throws IOException , CoreException { 380 File file = ref.asFile(); 381 if (file != null && !SWITCH_COPY_LOCAL) 382 return file; 383 ContentReference localRef = asLocalReference(ref, monitor); 384 file = localRef.asFile(); 385 return file; 386 } 387 388 395 protected File getWorkingDirectory() throws IOException { 396 if (tmpDir == null) 397 tmpDir = Utilities.createWorkingDirectory(); 398 return tmpDir; 399 } 400 401 408 public long getDownloadSizeFor(IPluginEntry[] pluginEntries, INonPluginEntry[] nonPluginEntries) { 409 long result = 0; 410 411 if ((pluginEntries == null || pluginEntries.length == 0) && (nonPluginEntries == null || nonPluginEntries.length == 0)) { 413 return ContentEntryModel.UNKNOWN_SIZE; 414 } 415 416 long size = 0; 418 if (pluginEntries != null) 419 for (int i = 0; i < pluginEntries.length; i++) { 420 size = ((PluginEntryModel) pluginEntries[i]).getDownloadSize(); 421 if (size == ContentEntryModel.UNKNOWN_SIZE) { 422 return ContentEntryModel.UNKNOWN_SIZE; 423 } 424 result += size; 425 } 426 427 if (nonPluginEntries != null) 429 for (int i = 0; i < nonPluginEntries.length; i++) { 430 size = ((NonPluginEntryModel) nonPluginEntries[i]).getDownloadSize(); 431 if (size == ContentEntryModel.UNKNOWN_SIZE) { 432 return ContentEntryModel.UNKNOWN_SIZE; 433 } 434 result += size; 435 } 436 437 return result; 438 } 439 440 447 public long getInstallSizeFor(IPluginEntry[] pluginEntries, INonPluginEntry[] nonPluginEntries) { 448 long result = 0; 449 450 if ((pluginEntries == null || pluginEntries.length == 0) && (nonPluginEntries == null || nonPluginEntries.length == 0)) { 452 return ContentEntryModel.UNKNOWN_SIZE; 453 } 454 455 long size = 0; 457 if (pluginEntries != null) 458 for (int i = 0; i < pluginEntries.length; i++) { 459 size = ((PluginEntryModel) pluginEntries[i]).getInstallSize(); 460 if (size == ContentEntryModel.UNKNOWN_SIZE) { 461 return ContentEntryModel.UNKNOWN_SIZE; 462 } 463 result += size; 464 } 465 466 if (nonPluginEntries != null) 468 for (int i = 0; i < nonPluginEntries.length; i++) { 469 size = ((NonPluginEntryModel) nonPluginEntries[i]).getInstallSize(); 470 if (size == ContentEntryModel.UNKNOWN_SIZE) { 471 return ContentEntryModel.UNKNOWN_SIZE; 472 } 473 result += size; 474 } 475 476 return result; 477 } 478 479 484 protected String getPathID(IPluginEntry entry) { 485 return Site.DEFAULT_PLUGIN_PATH + entry.getVersionedIdentifier().toString() + JAR_EXTENSION; 486 } 487 488 493 protected String getPathID(INonPluginEntry entry) { 494 String nonPluginBaseID = Site.DEFAULT_FEATURE_PATH + feature.getVersionedIdentifier().toString() + "/"; return nonPluginBaseID + entry.getIdentifier(); 496 } 497 498 503 protected void validatePermissions(ContentReference[] references) { 504 505 if (references == null || references.length == 0) 506 return; 507 508 Map permissions = getPermissions(references); 509 if (permissions.isEmpty()) 510 return; 511 512 for (int i = 0; i < references.length; i++) { 513 ContentReference contentReference = references[i]; 514 String id = contentReference.getIdentifier(); 515 Object value = null; 516 if ((value = matchesOneRule(id, permissions)) != null) { 517 Integer permission = (Integer ) value; 518 contentReference.setPermission(permission.intValue()); 519 } 520 } 521 } 522 523 535 private Object matchesOneRule(String id, Map permissions) { 536 537 Set keySet = permissions.keySet(); 538 Iterator iter = keySet.iterator(); 539 while (iter.hasNext()) { 540 FileFilter rule = (FileFilter) iter.next(); 541 if (rule.accept(id)) { 542 return permissions.get(rule); 543 } 544 } 545 546 return null; 547 } 548 549 552 private Map getPermissions(ContentReference[] references) { 553 554 Map result = new HashMap (); 555 boolean notfound = true; 557 ContentReference permissionReference = null; 558 for (int i = 0; i < references.length && notfound; i++) { 559 ContentReference contentReference = references[i]; 560 if (DOT_PERMISSIONS.equals(contentReference.getIdentifier())) { 561 notfound = false; 562 permissionReference = contentReference; 563 } 564 } 565 if (notfound) 566 return result; 567 568 Properties prop = new Properties (); 569 InputStream propertyStream = null; 570 try { 571 try { 572 propertyStream = permissionReference.getInputStream(); 573 prop.load(propertyStream); 574 } finally { 575 if (propertyStream != null) 576 propertyStream.close(); 577 } 578 } catch (IOException e) { 579 UpdateCore.warn("", e); } 581 582 String executables = prop.getProperty(EXECUTABLES); 583 if (executables == null) 584 return result; 585 586 StringTokenizer tokenizer = new StringTokenizer (executables, ","); Integer defaultExecutablePermission = new Integer (ContentReference.DEFAULT_EXECUTABLE_PERMISSION); 588 while (tokenizer.hasMoreTokens()) { 589 FileFilter filter = new FileFilter(tokenizer.nextToken()); 590 result.put(filter, defaultExecutablePermission); 591 } 592 593 return result; 594 } 595 } 596 | Popular Tags |