From eb934c760dce045ff33ab58b35f2f2729b394751 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Thu, 1 Mar 2012 16:04:18 +0000 Subject: [PATCH] Made the auto-update-checker suggest channels to server administrators if they're upgrading past their preferred channel. --- .../org/bukkit/craftbukkit/CraftServer.java | 1 + .../craftbukkit/updater/ArtifactDetails.java | 47 +++++++++++++++++++ .../craftbukkit/updater/AutoUpdater.java | 20 ++++++++ .../updater/BukkitDLUpdaterService.java | 34 +++++++++++++- src/main/resources/configurations/bukkit.yml | 1 + 5 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 75fb8d537d..7418585a08 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -175,6 +175,7 @@ public final class CraftServer implements Server { updater = new AutoUpdater(new BukkitDLUpdaterService(configuration.getString("auto-updater.host")), getLogger(), configuration.getString("auto-updater.preferred-channel")); updater.setEnabled(configuration.getBoolean("auto-updater.enabled")); + updater.setSuggestChannels(configuration.getBoolean("auto-updater.suggest-channels")); updater.getOnBroken().addAll(configuration.getStringList("auto-updater.on-broken")); updater.getOnUpdate().addAll(configuration.getStringList("auto-updater.on-update")); updater.check(serverVersion); diff --git a/src/main/java/org/bukkit/craftbukkit/updater/ArtifactDetails.java b/src/main/java/org/bukkit/craftbukkit/updater/ArtifactDetails.java index b9af02d8ba..a9c5eafc94 100644 --- a/src/main/java/org/bukkit/craftbukkit/updater/ArtifactDetails.java +++ b/src/main/java/org/bukkit/craftbukkit/updater/ArtifactDetails.java @@ -10,6 +10,23 @@ public class ArtifactDetails { private String version; private Date created; private FileDetails file; + private ChannelDetails channel; + + public ChannelDetails getChannel() { + return channel; + } + + public void setChannel(ChannelDetails channel) { + this.channel = channel; + } + + public boolean isIsBroken() { + return isBroken; + } + + public void setIsBroken(boolean isBroken) { + this.isBroken = isBroken; + } public FileDetails getFile() { return file; @@ -78,4 +95,34 @@ public class ArtifactDetails { this.url = url; } } + + public static class ChannelDetails { + private String name; + private String slug; + private int priority; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getPriority() { + return priority; + } + + public void setPriority(int priority) { + this.priority = priority; + } + + public String getSlug() { + return slug; + } + + public void setSlug(String slug) { + this.slug = slug; + } + } } diff --git a/src/main/java/org/bukkit/craftbukkit/updater/AutoUpdater.java b/src/main/java/org/bukkit/craftbukkit/updater/AutoUpdater.java index 52869645c5..19c527ec50 100644 --- a/src/main/java/org/bukkit/craftbukkit/updater/AutoUpdater.java +++ b/src/main/java/org/bukkit/craftbukkit/updater/AutoUpdater.java @@ -16,6 +16,7 @@ public class AutoUpdater { private boolean enabled; private ArtifactDetails current = null; private ArtifactDetails latest = null; + private boolean suggestChannels = true; public AutoUpdater(BukkitDLUpdaterService service, Logger log, String channel) { this.service = service; @@ -35,6 +36,14 @@ public class AutoUpdater { this.enabled = isEnabled; } + public boolean shouldSuggestChannels() { + return suggestChannels; + } + + public void setSuggestChannels(boolean suggestChannels) { + this.suggestChannels = suggestChannels; + } + public List getOnBroken() { return onBroken; } @@ -100,6 +109,17 @@ public class AutoUpdater { log.severe("Unfortunately, there is not yet a newer version suitable for your server. We would advise you wait an hour or two, or try out a dev build."); log.severe("----- ------------------- -----"); + } else if ((current != null) && (shouldSuggestChannels())) { + ArtifactDetails.ChannelDetails prefChan = service.getChannel(channel, "preferred channel details"); + + if ((prefChan != null) && (current.getChannel().getPriority() < prefChan.getPriority())) { + log.info("----- Bukkit Auto Updater -----"); + log.info("It appears that you're running a " + current.getChannel().getName() + ", when you've specified in bukkit.yml that you prefer to run " + prefChan.getName() + "s."); + log.info("If you would like to be kept informed about new " + current.getChannel().getName() + " releases, it is recommended that you change 'preferred-channel' in your bukkit.yml to '" + current.getChannel().getSlug() + "'."); + log.info("With that set, you will be told whenever a new version is available for download, so that you can always keep up to date and secure with the latest fixes."); + log.info("If you would like to disable this warning, simply set 'suggest-channels' to false in bukkit.yml."); + log.info("----- ------------------- -----"); + } } } }.start(); diff --git a/src/main/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterService.java b/src/main/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterService.java index 3d94268da7..9ca4c691ba 100644 --- a/src/main/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterService.java +++ b/src/main/java/org/bukkit/craftbukkit/updater/BukkitDLUpdaterService.java @@ -13,7 +13,8 @@ import java.util.logging.Level; import java.util.logging.Logger; public class BukkitDLUpdaterService { - private static final String API_PREFIX = "/api/1.0/downloads/projects/craftbukkit/view/"; + private static final String API_PREFIX_ARTIFACT = "/api/1.0/downloads/projects/craftbukkit/view/"; + private static final String API_PREFIX_CHANNEL = "/api/1.0/downloads/channels/"; private static final DateDeserializer dateDeserializer = new DateDeserializer(); private final String host; @@ -34,7 +35,7 @@ public class BukkitDLUpdaterService { } public ArtifactDetails fetchArtifact(String slug) throws UnsupportedEncodingException, IOException { - URL url = new URL("http", host, API_PREFIX + slug); + URL url = new URL("http", host, API_PREFIX_ARTIFACT + slug); InputStreamReader reader = null; try { @@ -50,6 +51,35 @@ public class BukkitDLUpdaterService { } } + public ArtifactDetails.ChannelDetails getChannel(String slug, String name) { + try { + return fetchChannel(slug); + } catch (UnsupportedEncodingException ex) { + Logger.getLogger(BukkitDLUpdaterService.class.getName()).log(Level.WARNING, "Could not get " + name + ": " + ex.getClass().getSimpleName()); + } catch (IOException ex) { + Logger.getLogger(BukkitDLUpdaterService.class.getName()).log(Level.WARNING, "Could not get " + name + ": " + ex.getClass().getSimpleName()); + } + + return null; + } + + public ArtifactDetails.ChannelDetails fetchChannel(String slug) throws UnsupportedEncodingException, IOException { + URL url = new URL("http", host, API_PREFIX_CHANNEL + slug); + InputStreamReader reader = null; + + try { + reader = new InputStreamReader(url.openStream()); + Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, dateDeserializer).setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create(); + ArtifactDetails.ChannelDetails fromJson = gson.fromJson(reader, ArtifactDetails.ChannelDetails.class); + + return fromJson; + } finally { + if (reader != null) { + reader.close(); + } + } + } + static class DateDeserializer implements JsonDeserializer { private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); diff --git a/src/main/resources/configurations/bukkit.yml b/src/main/resources/configurations/bukkit.yml index 3665b925b0..9f89cbf711 100644 --- a/src/main/resources/configurations/bukkit.yml +++ b/src/main/resources/configurations/bukkit.yml @@ -31,6 +31,7 @@ auto-updater: on-update: [warn-console, warn-ops] preferred-channel: rb host: dl.bukkit.org + suggest-channels: true aliases: # icanhasbukkit: # - version