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