From ab1aacbdc94d5f24b910b9694471e6edd482de07 Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 13 Jan 2014 15:16:17 +1100 Subject: [PATCH] Implement a first tempt and module retrieval system --- .../main/java/net/md_5/bungee/BungeeCord.java | 1 + .../java/net/md_5/bungee/ModuleManager.java | 38 ---------- .../bungee/module/JenkinsModuleSource.java | 26 +++++++ .../net/md_5/bungee/module/ModuleManager.java | 71 +++++++++++++++++++ .../net/md_5/bungee/module/ModuleSource.java | 7 ++ .../net/md_5/bungee/module/ModuleSpec.java | 13 ++++ .../net/md_5/bungee/module/ModuleVersion.java | 24 +++++++ 7 files changed, 142 insertions(+), 38 deletions(-) delete mode 100644 proxy/src/main/java/net/md_5/bungee/ModuleManager.java create mode 100644 proxy/src/main/java/net/md_5/bungee/module/JenkinsModuleSource.java create mode 100644 proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java create mode 100644 proxy/src/main/java/net/md_5/bungee/module/ModuleSource.java create mode 100644 proxy/src/main/java/net/md_5/bungee/module/ModuleSpec.java create mode 100644 proxy/src/main/java/net/md_5/bungee/module/ModuleVersion.java diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 375e97c1..4825fb51 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -1,5 +1,6 @@ package net.md_5.bungee; +import net.md_5.bungee.module.ModuleManager; import com.google.common.io.ByteStreams; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; diff --git a/proxy/src/main/java/net/md_5/bungee/ModuleManager.java b/proxy/src/main/java/net/md_5/bungee/ModuleManager.java deleted file mode 100644 index ce41097e..00000000 --- a/proxy/src/main/java/net/md_5/bungee/ModuleManager.java +++ /dev/null @@ -1,38 +0,0 @@ -package net.md_5.bungee; - -import java.io.File; -import java.util.List; -import net.md_5.bungee.api.ProxyServer; - -public class ModuleManager -{ - - private class ModuleSpec - { - } - - public void load(ProxyServer proxy) throws Exception - { - String version = proxy.getVersion(); - version = "git:BungeeCord-Proxy:1.7-SNAPSHOT:\"93cf50b\":1337"; - - int lastColon = version.lastIndexOf( ':' ); - int secondLastColon = version.lastIndexOf( ':', lastColon - 1 ); - String buildNumber = version.substring( lastColon + 1, version.length() ); - String gitCommit = version.substring( secondLastColon + 1, lastColon ).replaceAll( "\"", "" ); - - File moduleDirectory = new File( "modules" ); - moduleDirectory.mkdir(); - - List modules = null; - - // TODO: Use filename filter here and in PluginManager - for ( File file : moduleDirectory.listFiles() ) - { - if ( file.isFile() && file.getName().endsWith( ".jar" ) ) - { - - } - } - } -} diff --git a/proxy/src/main/java/net/md_5/bungee/module/JenkinsModuleSource.java b/proxy/src/main/java/net/md_5/bungee/module/JenkinsModuleSource.java new file mode 100644 index 00000000..0ef1e76d --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/module/JenkinsModuleSource.java @@ -0,0 +1,26 @@ +package net.md_5.bungee.module; + +import com.google.common.io.ByteStreams; +import com.google.common.io.Files; +import java.io.IOException; +import java.net.URL; +import net.md_5.bungee.Util; + +public class JenkinsModuleSource implements ModuleSource +{ + + @Override + public void retrieve(ModuleSpec module, ModuleVersion version) + { + System.out.println( "Attempting to Jenkins download module " + module.getName() + "v" + version.getBuild() ); + try + { + URL website = new URL( "http://ci.md-5.net/job/BungeeCord/" + version.getBuild() + "/artifact/module/" + module.getName().replace( '_', '-' ) + "/target/" + module.getName() + ".jar" ); + Files.copy( ByteStreams.newInputStreamSupplier( ByteStreams.toByteArray( website.openStream() ) ), module.getFile() ); + System.out.println( "Download complete" ); + } catch ( IOException ex ) + { + System.out.println( "Failed to download: " + Util.exception( ex ) ); + } + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java b/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java new file mode 100644 index 00000000..84b254e1 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/module/ModuleManager.java @@ -0,0 +1,71 @@ +package net.md_5.bungee.module; + +import com.google.common.base.Preconditions; +import java.io.File; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; +import java.util.logging.Level; +import net.md_5.bungee.api.ProxyServer; +import net.md_5.bungee.api.plugin.PluginDescription; +import org.yaml.snakeyaml.Yaml; + +public class ModuleManager +{ + + private final Map knownSources = new HashMap<>(); + + public ModuleManager() + { + knownSources.put( "jenkins", new JenkinsModuleSource() ); + } + + public void load(ProxyServer proxy) throws Exception + { + ModuleVersion bungeeVersion = ModuleVersion.parse( "git:BungeeCord-Proxy:1.7-SNAPSHOT:\"93cf50b\":792" ); + + File moduleDirectory = new File( "modules" ); + moduleDirectory.mkdir(); + + Map modules = new HashMap<>(); + + // TODO: Use filename filter here and in PluginManager + Yaml yaml = new Yaml(); + for ( File file : moduleDirectory.listFiles() ) + { + if ( file.isFile() && file.getName().endsWith( ".jar" ) ) + { + String moduleName = file.getName().substring( 0, file.getName().length() - 4 ); // 4 = .jar.length() + ModuleSource source = modules.get( moduleName ); + if ( source == null ) + { + System.out.println( "No source for module in file: " + file ); + continue; + } + + try ( JarFile jar = new JarFile( file ) ) + { + JarEntry pdf = jar.getJarEntry( "plugin.yml" ); + Preconditions.checkNotNull( pdf, "Plugin must have a plugin.yml" ); + + try ( InputStream in = jar.getInputStream( pdf ) ) + { + PluginDescription desc = yaml.loadAs( in, PluginDescription.class ); + ModuleVersion moduleVersion = ModuleVersion.parse( desc.getVersion() ); + if ( !moduleVersion.equals( bungeeVersion ) ) + { + System.out.println( "Attempting to update plugin from " + moduleVersion + " to " + bungeeVersion ); + source.retrieve( new ModuleSpec( moduleName, file ), bungeeVersion ); + } + } + } catch ( Exception ex ) + { + ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not check module from file " + file, ex ); + } + } + } + } +} diff --git a/proxy/src/main/java/net/md_5/bungee/module/ModuleSource.java b/proxy/src/main/java/net/md_5/bungee/module/ModuleSource.java new file mode 100644 index 00000000..09f5624c --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/module/ModuleSource.java @@ -0,0 +1,7 @@ +package net.md_5.bungee.module; + +interface ModuleSource +{ + + void retrieve(ModuleSpec module, ModuleVersion version); +} diff --git a/proxy/src/main/java/net/md_5/bungee/module/ModuleSpec.java b/proxy/src/main/java/net/md_5/bungee/module/ModuleSpec.java new file mode 100644 index 00000000..8bde0f1f --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/module/ModuleSpec.java @@ -0,0 +1,13 @@ +package net.md_5.bungee.module; + +import java.io.File; +import lombok.Data; + +@Data +public class ModuleSpec +{ + + private final String name; + private final File file; + private ModuleSource provider; +} diff --git a/proxy/src/main/java/net/md_5/bungee/module/ModuleVersion.java b/proxy/src/main/java/net/md_5/bungee/module/ModuleVersion.java new file mode 100644 index 00000000..b1236514 --- /dev/null +++ b/proxy/src/main/java/net/md_5/bungee/module/ModuleVersion.java @@ -0,0 +1,24 @@ +package net.md_5.bungee.module; + +import lombok.AccessLevel; +import lombok.Data; +import lombok.RequiredArgsConstructor; + +@Data +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class ModuleVersion +{ + + private final String build; + private final String git; + + public static ModuleVersion parse(String version) + { + int lastColon = version.lastIndexOf( ':' ); + int secondLastColon = version.lastIndexOf( ':', lastColon - 1 ); + String buildNumber = version.substring( lastColon + 1, version.length() ); + String gitCommit = version.substring( secondLastColon + 1, lastColon ).replaceAll( "\"", "" ); + + return new ModuleVersion( buildNumber, gitCommit ); + } +}