Skip to content

Instantly share code, notes, and snippets.

@jamezrin
Created February 3, 2017 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamezrin/bb8183f880ee8a7e4d5a8e92d0958cb3 to your computer and use it in GitHub Desktop.
Save jamezrin/bb8183f880ee8a7e4d5a8e92d0958cb3 to your computer and use it in GitHub Desktop.
public class GeolocationManager {
private final DatabaseProvider provider;
public GeolocationManager(LobbyBalancer plugin) throws Exception {
File dir = new File(plugin.getDataFolder(), "data");
if (!dir.exists()) {
dir.mkdir();
}
File reader = new File(dir, "geoip2-2.8.0");
if (!reader.exists()) {
plugin.getLogger().info("Downloading reader...");
File packed = new File(dir, "geoip2-2.8.0.zip");
URL url = new URL("https://github.com/maxmind/GeoIP2-java/releases/download/v2.8.0/geoip2-2.8.0-with-dependencies.zip?raw=true");
try (ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
try (FileOutputStream fos = new FileOutputStream(packed)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
plugin.getLogger().info("Unpacking reader...");
ZipFile zipFile = new ZipFile(packed);
zipFile.extractAll(dir.getAbsolutePath());
plugin.getLogger().info("Deleting packed archive, success: " + (packed.delete() ? "yes" : "no"));
}
plugin.getLogger().info("Loading libraries...");
if (reader.exists()) {
File libs = new File(reader, "lib");
if (libs.exists()) {
for (File file : libs.listFiles()) {
URL url = file.toURI().toURL();
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
method.setAccessible(true);
method.invoke(classLoader, url);
}
}
}
File database = new File(dir, "GeoLite2-Country.mmdb");
if (!database.exists()) {
plugin.getLogger().info("Downloading database...");
File packed = new File(dir, "GeoLite2-Country.mmdb.gz");
URL url = new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz");
try (ReadableByteChannel rbc = Channels.newChannel(url.openStream())) {
try (FileOutputStream fos = new FileOutputStream(packed)) {
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
}
plugin.getLogger().info("Unpacking database...");
byte[] buffer = new byte[1024];
try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(packed))) {
try (FileOutputStream out = new FileOutputStream(database)) {
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
}
plugin.getLogger().info("Deleting packed archive, success: " + (packed.delete() ? "yes" : "no"));
} else {
plugin.getLogger().info("Database exists, no need to download again");
}
plugin.getLogger().info("Initializing database...");
provider = new DatabaseProvider(database);
}
public static class DatabaseProvider {
private final DatabaseReader reader;
public DatabaseProvider(File file) throws Exception {
reader = new DatabaseReader.Builder(file).withCache(new CHMCache()).build();
}
public DatabaseReader getReader() {
return reader;
}
}
public DatabaseProvider getProvider() {
return provider;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment