You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateChecker.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.updater;
  18. import com.dmdirc.config.provider.AggregateConfigProvider;
  19. import com.dmdirc.interfaces.config.IdentityController;
  20. import com.dmdirc.updater.manager.CachingUpdateManager;
  21. import com.dmdirc.updater.manager.UpdateStatus;
  22. import java.util.Date;
  23. import java.util.Timer;
  24. import java.util.TimerTask;
  25. import java.util.concurrent.Semaphore;
  26. import org.slf4j.Logger;
  27. import org.slf4j.LoggerFactory;
  28. import static com.dmdirc.util.LogUtils.USER_ERROR;
  29. /**
  30. * The update checker contacts the DMDirc website to check to see if there are any updates
  31. * available.
  32. */
  33. public final class UpdateChecker implements Runnable {
  34. private static final Logger LOG = LoggerFactory.getLogger(UpdateChecker.class);
  35. /** The domain to use for updater settings. */
  36. private static final String DOMAIN = "updater";
  37. /** Semaphore used to prevent multiple invocations. */
  38. private static final Semaphore MUTEX = new Semaphore(1);
  39. /** Our timer. */
  40. private static Timer timer = new Timer("Update Checker Timer");
  41. /** The update manager to use. */
  42. private final CachingUpdateManager updateManager;
  43. /** The controller to use to read and write settings. */
  44. private final IdentityController identityController;
  45. /**
  46. * Creates a new instance of {@link UpdateChecker}.
  47. *
  48. * @param updateManager The manager to use to perform updates.
  49. * @param identityController The controller to use to read and write settings.
  50. */
  51. public UpdateChecker(
  52. final CachingUpdateManager updateManager,
  53. final IdentityController identityController) {
  54. this.updateManager = updateManager;
  55. this.identityController = identityController;
  56. }
  57. @Override
  58. public void run() {
  59. if (!MUTEX.tryAcquire()) {
  60. // Duplicate invocation
  61. return;
  62. }
  63. final AggregateConfigProvider config = identityController.getGlobalConfiguration();
  64. if (!config.getOptionBool(DOMAIN, "enable")) {
  65. identityController.getUserSettings().setOption(DOMAIN,
  66. "lastcheck", String.valueOf((int) (new Date().getTime() / 1000)));
  67. MUTEX.release();
  68. init(updateManager, identityController);
  69. return;
  70. }
  71. updateManager.checkForUpdates();
  72. MUTEX.release();
  73. identityController.getUserSettings().setOption(DOMAIN,
  74. "lastcheck", String.valueOf((int) (new Date().getTime() / 1000)));
  75. init(updateManager, identityController);
  76. if (config.getOptionBool(DOMAIN, "autoupdate")) {
  77. updateManager.getComponents().stream()
  78. .filter(component -> updateManager.getStatus(component) ==
  79. UpdateStatus.UPDATE_PENDING).forEach(updateManager::install);
  80. } else if (config.getOptionBool(DOMAIN, "autodownload")) {
  81. updateManager.getComponents().stream()
  82. .filter(component -> updateManager.getStatus(component) ==
  83. UpdateStatus.UPDATE_PENDING).forEach(updateManager::retrieve);
  84. }
  85. }
  86. /**
  87. * Initialises the update checker. Sets a timer to check based on the frequency specified in the
  88. * config.
  89. *
  90. * @param manager Manager to monitor updates
  91. * @param controller The controller to use to retrieve and update settings.
  92. */
  93. public static void init(
  94. final CachingUpdateManager manager,
  95. final IdentityController controller) {
  96. final int last = controller.getGlobalConfiguration()
  97. .getOptionInt(DOMAIN, "lastcheck");
  98. final int freq = controller.getGlobalConfiguration()
  99. .getOptionInt(DOMAIN, "frequency");
  100. final int timestamp = (int) (new Date().getTime() / 1000);
  101. int time = 0;
  102. if (last + freq > timestamp) {
  103. time = last + freq - timestamp;
  104. }
  105. if (time > freq || time < 0) {
  106. LOG.info(USER_ERROR, "Attempted to schedule update check " + (time < 0
  107. ? "in the past" : "too far in the future") + ", rescheduling.");
  108. time = 1;
  109. }
  110. timer.cancel();
  111. timer = new Timer("Update Checker Timer");
  112. timer.schedule(new TimerTask() {
  113. @Override
  114. public void run() {
  115. checkNow(manager, controller);
  116. }
  117. }, time * 1000);
  118. }
  119. /**
  120. * Checks for updates now.
  121. *
  122. * @param updateManager The manager to use for checking.
  123. * @param identityController The controller to use to retrieve and update settings.
  124. */
  125. public static void checkNow(
  126. final CachingUpdateManager updateManager,
  127. final IdentityController identityController) {
  128. checkNow(updateManager, identityController, "Update Checker thread");
  129. }
  130. /**
  131. * Checks for updates now.
  132. *
  133. * @param updateManager The manager to use for checking.
  134. * @param identityController The controller to use to retrieve and update settings.
  135. * @param threadName The name of the thread to use to run the checker in.
  136. */
  137. public static void checkNow(
  138. final CachingUpdateManager updateManager,
  139. final IdentityController identityController,
  140. final String threadName) {
  141. new Thread(new UpdateChecker(updateManager, identityController), threadName)
  142. .start();
  143. }
  144. }