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.

NightlyChecker.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2006-2015 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.checking;
  23. import com.dmdirc.config.ConfigBinding;
  24. import com.dmdirc.config.GlobalConfig;
  25. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  26. import com.dmdirc.updater.UpdateChannel;
  27. import com.dmdirc.updater.UpdateComponent;
  28. import com.dmdirc.updater.Version;
  29. import com.dmdirc.util.io.Downloader;
  30. import com.google.common.base.MoreObjects;
  31. import com.google.gson.Gson;
  32. import com.google.gson.reflect.TypeToken;
  33. import java.io.IOException;
  34. import java.net.MalformedURLException;
  35. import java.net.URL;
  36. import java.util.Collection;
  37. import java.util.Collections;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. import java.util.Map;
  41. import java.util.Objects;
  42. import java.util.function.Function;
  43. import java.util.regex.Matcher;
  44. import java.util.regex.Pattern;
  45. import java.util.stream.Collectors;
  46. import javax.inject.Inject;
  47. import org.slf4j.Logger;
  48. import org.slf4j.LoggerFactory;
  49. /**
  50. * Nightly update checker.
  51. */
  52. public class NightlyChecker implements UpdateCheckStrategy {
  53. private static final Logger LOG = LoggerFactory.getLogger(NightlyChecker.class);
  54. /** Name matching regex. */
  55. private final Pattern pattern = Pattern.compile(
  56. "^(.*?)-([^-]+(-[0-9]+-g[0-9a-f]+)?)(-SNAPSHOT)?\\.jar$");
  57. /** The URL to request to check for updates. */
  58. private static final String UPDATE_URL = "https://nightlies.dmdirc.com/json/latest";
  59. /** The update channel to check for updates on. */
  60. private UpdateChannel channel;
  61. /** Downloader to download files. */
  62. private final Downloader downloader;
  63. /**
  64. * Creates a new instance of {@link NightlyChecker}.
  65. *
  66. * @param configProvider The provider to use to retrieve update channel information.
  67. * @param downloader Used to download files
  68. */
  69. @Inject
  70. public NightlyChecker(@GlobalConfig final AggregateConfigProvider configProvider,
  71. final Downloader downloader) {
  72. configProvider.getBinder().bind(this, NightlyChecker.class);
  73. this.downloader = downloader;
  74. }
  75. /**
  76. * Sets the channel which will be used by the {@link NightlyChecker}.
  77. *
  78. * @param channel The new channel to use
  79. */
  80. @ConfigBinding(domain = "updater", key = "channel")
  81. public void setChannel(final String channel) {
  82. LOG.info("Changing channel to {}", channel);
  83. try {
  84. this.channel = UpdateChannel.valueOf(channel.toUpperCase());
  85. } catch (IllegalArgumentException ex) {
  86. this.channel = null;
  87. LOG.warn("Unknown channel {}", channel, ex);
  88. }
  89. }
  90. @Override
  91. public Map<UpdateComponent, UpdateCheckResult> checkForUpdates(
  92. final Collection<UpdateComponent> components) {
  93. if (channel != UpdateChannel.NIGHTLY) {
  94. LOG.info("Channel {} is not nightly, aborting", channel);
  95. return Collections.emptyMap();
  96. }
  97. LOG.info("Retrieving latest versions.");
  98. final List<NightlyResult> resultsList = new Gson().fromJson(getJson(),
  99. new TypeToken<List<NightlyResult>>(){}.getType());
  100. if (resultsList == null) {
  101. return Collections.emptyMap();
  102. }
  103. resultsList.stream()
  104. .filter(Objects::nonNull) //This is incase the JSON is broken
  105. .forEach(e -> {
  106. final Matcher matcher = pattern.matcher(e.getName());
  107. if (matcher.matches()) {
  108. e.setOtherName(matcher.group(1));
  109. e.setVersion(new Version(matcher.group(2)));
  110. e.setUrl(UPDATE_URL + '/' + e.getName());
  111. }
  112. });
  113. final Map<String, NightlyResult> resultsMap = resultsList.stream()
  114. .collect(Collectors.toMap(NightlyResult::getOtherName, Function.identity()));
  115. final Map<UpdateComponent, UpdateCheckResult> returns = new HashMap<>();
  116. components.forEach(e -> {
  117. if (resultsMap.containsKey(e.getName())) {
  118. if (resultsMap.get(e.getName()).getVersion().compareTo(e.getVersion()) > 0) {
  119. final String name = e.getName();
  120. final NightlyResult result = resultsMap.get(e.getName());
  121. try {
  122. returns.put(e, new BaseDownloadableResult(e, getURL(result),
  123. result.getOtherName(), result.getVersion()));
  124. } catch (MalformedURLException e1) {
  125. LOG.error("Unable to create a URL for {}", name);
  126. }
  127. LOG.info("Updating {} from {} to {}", e.getName(), e.getVersion(),
  128. resultsMap.get(e.getName()).getVersion());
  129. } else {
  130. LOG.info("Not updating {} from {} to {}", e.getName(), e.getVersion(),
  131. resultsMap.get(e.getName()).getVersion());
  132. }
  133. }
  134. });
  135. return returns;
  136. }
  137. private URL getURL(final NightlyResult result) throws MalformedURLException {
  138. return new URL(result.getUrl());
  139. }
  140. private String getJson() {
  141. try {
  142. return downloader.getPage(UPDATE_URL).stream().map(String::toString)
  143. .collect(Collectors.joining("\r\n"));
  144. } catch (IOException e) {
  145. LOG.warn("Error when getting update page: {}", e.getMessage());
  146. return "";
  147. }
  148. }
  149. /**
  150. * Wrapper class for GSON to deserialise the JSON.
  151. */
  152. private static class NightlyResult {
  153. private final String name;
  154. private final String type;
  155. private final String mtime;
  156. private final int size;
  157. private String otherName;
  158. private Version version;
  159. private String url;
  160. NightlyResult(final String name, final String type, final String mtime,
  161. final int size) {
  162. this.name = name;
  163. this.type = type;
  164. this.mtime = mtime;
  165. this.size = size;
  166. }
  167. String getName() {
  168. return name;
  169. }
  170. String getUrl() {
  171. return url;
  172. }
  173. String getOtherName() {
  174. return otherName;
  175. }
  176. void setOtherName(final String otherName) {
  177. this.otherName = otherName;
  178. }
  179. Version getVersion() {
  180. return version;
  181. }
  182. void setVersion(final Version version) {
  183. this.version = version;
  184. }
  185. void setUrl(final String url) {
  186. this.url = url;
  187. }
  188. public String toString() {
  189. return MoreObjects.toStringHelper(NightlyChecker.class)
  190. .add("name", otherName)
  191. .add("version", version)
  192. .toString();
  193. }
  194. }
  195. }