From 88e0aa64a0911dfab135ff0924df6697b0880643 Mon Sep 17 00:00:00 2001 From: Tux Date: Mon, 7 Mar 2016 20:56:58 -0500 Subject: [PATCH] reorganize and do proper resource cleanup --- .../minecraft/redisbungee/DataManager.java | 2 +- .../minecraft/redisbungee/RedisBungee.java | 3 +++ .../redisbungee/util/{ => uuid}/NameFetcher.java | 7 +++++-- .../redisbungee/util/{ => uuid}/UUIDFetcher.java | 11 +++++------ .../redisbungee/util/{ => uuid}/UUIDTranslator.java | 2 +- .../minecraft/redisbungee/test/UUIDNameTest.java | 4 ++-- 6 files changed, 17 insertions(+), 12 deletions(-) rename src/main/java/com/imaginarycode/minecraft/redisbungee/util/{ => uuid}/NameFetcher.java (83%) rename src/main/java/com/imaginarycode/minecraft/redisbungee/util/{ => uuid}/UUIDFetcher.java (88%) rename src/main/java/com/imaginarycode/minecraft/redisbungee/util/{ => uuid}/UUIDTranslator.java (99%) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java index a00273d..b063a3a 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/DataManager.java @@ -35,7 +35,6 @@ import java.util.logging.Level; */ public class DataManager implements Listener { private final RedisBungee plugin; - // TODO: Add cleanup for this. private final Cache serverCache = createCache(); private final Cache proxyCache = createCache(); private final Cache ipCache = createCache(); @@ -46,6 +45,7 @@ public class DataManager implements Listener { } private static Cache createCache() { + // TODO: Allow customization via cache specification, ala ServerListPlus return CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(1, TimeUnit.HOURS) diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index a3b728c..4d52f0a 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -7,6 +7,9 @@ import com.google.common.io.ByteStreams; import com.google.gson.Gson; import com.imaginarycode.minecraft.redisbungee.events.PubSubMessageEvent; import com.imaginarycode.minecraft.redisbungee.util.*; +import com.imaginarycode.minecraft.redisbungee.util.uuid.NameFetcher; +import com.imaginarycode.minecraft.redisbungee.util.uuid.UUIDFetcher; +import com.imaginarycode.minecraft.redisbungee.util.uuid.UUIDTranslator; import com.squareup.okhttp.Dispatcher; import com.squareup.okhttp.OkHttpClient; import lombok.AccessLevel; diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/NameFetcher.java similarity index 83% rename from src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java rename to src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/NameFetcher.java index b0515c3..b7c035f 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/NameFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/NameFetcher.java @@ -1,9 +1,10 @@ -package com.imaginarycode.minecraft.redisbungee.util; +package com.imaginarycode.minecraft.redisbungee.util.uuid; import com.google.gson.reflect.TypeToken; import com.imaginarycode.minecraft.redisbungee.RedisBungee; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; +import com.squareup.okhttp.ResponseBody; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.Setter; @@ -22,7 +23,9 @@ public class NameFetcher { public static List nameHistoryFromUuid(UUID uuid) throws IOException { String url = "https://api.mojang.com/user/profiles/" + uuid.toString().replace("-", "") + "/names"; Request request = new Request.Builder().url(url).get().build(); - String response = httpClient.newCall(request).execute().body().string(); + ResponseBody body = httpClient.newCall(request).execute().body(); + String response = body.string(); + body.close(); Type listType = new TypeToken>() { }.getType(); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDFetcher.java similarity index 88% rename from src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java rename to src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDFetcher.java index d7f80fb..61dca92 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDFetcher.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDFetcher.java @@ -1,11 +1,8 @@ -package com.imaginarycode.minecraft.redisbungee.util; +package com.imaginarycode.minecraft.redisbungee.util.uuid; import com.google.common.collect.ImmutableList; import com.imaginarycode.minecraft.redisbungee.RedisBungee; -import com.squareup.okhttp.MediaType; -import com.squareup.okhttp.OkHttpClient; -import com.squareup.okhttp.Request; -import com.squareup.okhttp.RequestBody; +import com.squareup.okhttp.*; import lombok.Setter; import java.util.HashMap; @@ -44,7 +41,9 @@ public class UUIDFetcher implements Callable> { for (int i = 0; i < requests; i++) { String body = RedisBungee.getGson().toJson(names.subList(i * 100, Math.min((i + 1) * 100, names.size()))); Request request = new Request.Builder().url(PROFILE_URL).post(RequestBody.create(JSON, body)).build(); - String response = httpClient.newCall(request).execute().body().string(); + ResponseBody responseBody = httpClient.newCall(request).execute().body(); + String response = responseBody.string(); + responseBody.close(); Profile[] array = RedisBungee.getGson().fromJson(response, Profile[].class); for (Profile profile : array) { UUID uuid = UUIDFetcher.getUUID(profile.id); diff --git a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDTranslator.java similarity index 99% rename from src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java rename to src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDTranslator.java index 984c4ef..0554dbf 100644 --- a/src/main/java/com/imaginarycode/minecraft/redisbungee/util/UUIDTranslator.java +++ b/src/main/java/com/imaginarycode/minecraft/redisbungee/util/uuid/UUIDTranslator.java @@ -1,4 +1,4 @@ -package com.imaginarycode.minecraft.redisbungee.util; +package com.imaginarycode.minecraft.redisbungee.util.uuid; import com.google.common.base.Charsets; import com.google.common.collect.ImmutableMap; diff --git a/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java index 244187a..613f97b 100644 --- a/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java +++ b/src/test/java/com/imaginarycode/minecraft/redisbungee/test/UUIDNameTest.java @@ -1,7 +1,7 @@ package com.imaginarycode.minecraft.redisbungee.test; -import com.imaginarycode.minecraft.redisbungee.util.NameFetcher; -import com.imaginarycode.minecraft.redisbungee.util.UUIDFetcher; +import com.imaginarycode.minecraft.redisbungee.util.uuid.NameFetcher; +import com.imaginarycode.minecraft.redisbungee.util.uuid.UUIDFetcher; import com.squareup.okhttp.OkHttpClient; import org.junit.Test;