Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

DownloadRetrievalStrategy.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.updater.retrieving;
  23. import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
  24. import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
  25. import com.dmdirc.updater.UpdateComponent;
  26. import com.dmdirc.updater.checking.DownloadableUpdate;
  27. import com.dmdirc.util.collections.ListenerList;
  28. import com.dmdirc.util.io.DownloadListener;
  29. import com.dmdirc.util.io.Downloader;
  30. import org.slf4j.LoggerFactory;
  31. import javax.inject.Inject;
  32. import java.io.File;
  33. import java.io.IOException;
  34. import java.nio.file.Paths;
  35. /**
  36. * An {@link UpdateRetrievalStrategy} that downloads a file specified in a
  37. * {@link DownloadableUpdate}.
  38. */
  39. public class DownloadRetrievalStrategy extends TypeSensitiveRetrievalStrategy<DownloadableUpdate> {
  40. private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(
  41. DownloadRetrievalStrategy.class);
  42. /** List of registered listeners. */
  43. private final ListenerList listenerList = new ListenerList();
  44. /** The directory to put temporary update files in. */
  45. private final String directory;
  46. /** Downloader to download files. */
  47. private final Downloader downloader;
  48. /**
  49. * Creates a new {@link DownloadRetrievalStrategy} which will place its temporary files in the
  50. * given directory.
  51. *
  52. * @param directory The directory to use to download files to#
  53. * @param downloader Used to download files
  54. */
  55. @Inject
  56. public DownloadRetrievalStrategy(@Directory(DirectoryType.BASE) final String directory,
  57. final Downloader downloader) {
  58. super(DownloadableUpdate.class);
  59. this.directory = directory;
  60. this.downloader = downloader;
  61. }
  62. @Override
  63. protected UpdateRetrievalResult retrieveImpl(final DownloadableUpdate checkResult) {
  64. try {
  65. final String file = getFileName();
  66. listenerList.getCallable(UpdateRetrievalListener.class)
  67. .retrievalProgressChanged(checkResult.getComponent(), 0);
  68. LOG.debug("Downloading file from {} to {}", checkResult.getUrl(), file);
  69. downloader.downloadPage(checkResult.getUrl().toString(), Paths.get(file),
  70. new DownloadProgressListener(checkResult.getComponent()));
  71. listenerList.getCallable(UpdateRetrievalListener.class)
  72. .retrievalCompleted(checkResult.getComponent());
  73. return new BaseSingleFileResult(checkResult, new File(file));
  74. } catch (IOException ex) {
  75. LOG.warn("I/O exception downloading update from {}", checkResult.getUrl(), ex);
  76. listenerList.getCallable(UpdateRetrievalListener.class)
  77. .retrievalFailed(checkResult.getComponent());
  78. }
  79. return new BaseRetrievalResult(checkResult, false);
  80. }
  81. /**
  82. * Creates a random local file name to download the remote file to.
  83. *
  84. * @return The full, local path to download the remote file to
  85. */
  86. private String getFileName() {
  87. return directory + File.separator + "update."
  88. + Math.round(10000 * Math.random()) + ".tmp";
  89. }
  90. @Override
  91. public void addUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  92. listenerList.add(UpdateRetrievalListener.class, listener);
  93. }
  94. @Override
  95. public void removeUpdateRetrievalListener(final UpdateRetrievalListener listener) {
  96. listenerList.remove(UpdateRetrievalListener.class, listener);
  97. }
  98. /**
  99. * A {@link DownloadListener} which proxies progress updates on to this strategy's
  100. * {@link UpdateRetrievalListener}s.
  101. */
  102. private class DownloadProgressListener implements DownloadListener {
  103. /** The component to fire updates for. */
  104. private final UpdateComponent component;
  105. public DownloadProgressListener(final UpdateComponent component) {
  106. this.component = component;
  107. }
  108. @Override
  109. public void downloadProgress(final float percent) {
  110. listenerList.getCallable(UpdateRetrievalListener.class)
  111. .retrievalProgressChanged(component, percent);
  112. }
  113. @Override
  114. public void setIndeterminate(final boolean indeterminate) {
  115. // Do nothing
  116. }
  117. }
  118. }