Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

UpdateChecker.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2006-2013 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;
  23. import com.dmdirc.config.IdentityManager;
  24. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.updater.manager.CachingUpdateManager;
  28. import com.dmdirc.updater.manager.UpdateStatus;
  29. import java.util.Date;
  30. import java.util.Timer;
  31. import java.util.TimerTask;
  32. import java.util.concurrent.Semaphore;
  33. import lombok.Getter;
  34. /**
  35. * The update checker contacts the DMDirc website to check to see if there
  36. * are any updates available.
  37. */
  38. public final class UpdateChecker implements Runnable {
  39. /** The domain to use for updater settings. */
  40. private static final String DOMAIN = "updater";
  41. /** Semaphore used to prevent multiple invocations. */
  42. private static final Semaphore MUTEX = new Semaphore(1);
  43. /** Our timer. */
  44. private static Timer timer = new Timer("Update Checker Timer");
  45. /** The update manager to use. */
  46. @Getter
  47. private static CachingUpdateManager manager;
  48. /** {@inheritDoc} */
  49. @Override
  50. public void run() {
  51. if (!MUTEX.tryAcquire()) {
  52. // Duplicate invocation
  53. return;
  54. }
  55. final AggregateConfigProvider config = IdentityManager.getIdentityManager().getGlobalConfiguration();
  56. if (!config.getOptionBool(DOMAIN, "enable")) {
  57. IdentityManager.getIdentityManager().getUserSettings().setOption(DOMAIN,
  58. "lastcheck", String.valueOf((int) (new Date().getTime() / 1000)));
  59. MUTEX.release();
  60. initTimer();
  61. return;
  62. }
  63. manager.checkForUpdates();
  64. MUTEX.release();
  65. IdentityManager.getIdentityManager().getUserSettings().setOption(DOMAIN,
  66. "lastcheck", String.valueOf((int) (new Date().getTime() / 1000)));
  67. UpdateChecker.initTimer();
  68. if (config.getOptionBool(DOMAIN, "autoupdate")) {
  69. for (UpdateComponent component : manager.getComponents()) {
  70. if (manager.getStatus(component) == UpdateStatus.UPDATE_PENDING) {
  71. manager.install(component);
  72. }
  73. }
  74. } else if (config.getOptionBool(DOMAIN, "autodownload")) {
  75. for (UpdateComponent component : manager.getComponents()) {
  76. if (manager.getStatus(component) == UpdateStatus.UPDATE_PENDING) {
  77. manager.retrieve(component);
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * Initialises the update checker. Sets a timer to check based on the
  84. * frequency specified in the config.
  85. *
  86. * @param manager Manager to monitor updates
  87. */
  88. public static void init(final CachingUpdateManager manager) {
  89. UpdateChecker.manager = manager;
  90. initTimer();
  91. }
  92. /**
  93. * Initialises the timer to check for updates.
  94. *
  95. * @since 0.6.5
  96. */
  97. protected static void initTimer() {
  98. final int last = IdentityManager.getIdentityManager().getGlobalConfiguration()
  99. .getOptionInt(DOMAIN, "lastcheck");
  100. final int freq = IdentityManager.getIdentityManager().getGlobalConfiguration()
  101. .getOptionInt(DOMAIN, "frequency");
  102. final int timestamp = (int) (new Date().getTime() / 1000);
  103. int time = 0;
  104. if (last + freq > timestamp) {
  105. time = last + freq - timestamp;
  106. }
  107. if (time > freq || time < 0) {
  108. Logger.userError(ErrorLevel.LOW, "Attempted to schedule update check "
  109. + (time < 0 ? "in the past" : "too far in the future")
  110. + ", rescheduling.");
  111. time = 1;
  112. }
  113. timer.cancel();
  114. timer = new Timer("Update Checker Timer");
  115. timer.schedule(new TimerTask() {
  116. /** {@inheritDoc} */
  117. @Override
  118. public void run() {
  119. checkNow();
  120. }
  121. }, time * 1000);
  122. }
  123. /**
  124. * Checks for updates now.
  125. */
  126. public static void checkNow() {
  127. new Thread(new UpdateChecker(), "Update Checker thread").start();
  128. }
  129. }