Finish implementing modules. If anyone wants to test... be my guest.

This commit is contained in:
md_5 2014-01-14 13:13:19 +11:00
parent d4e4796739
commit f1b329bf21
5 changed files with 121 additions and 33 deletions

View File

@ -40,4 +40,8 @@ public class PluginDescription
* File we were loaded from. * File we were loaded from.
*/ */
private File file = null; private File file = null;
/**
* Optional description.
*/
private String description = null;
} }

View File

@ -4,8 +4,10 @@ import com.google.common.io.ByteStreams;
import com.google.common.io.Files; import com.google.common.io.Files;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import lombok.Data;
import net.md_5.bungee.Util; import net.md_5.bungee.Util;
@Data
public class JenkinsModuleSource implements ModuleSource public class JenkinsModuleSource implements ModuleSource
{ {

View File

@ -2,7 +2,12 @@ package net.md_5.bungee.module;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -11,6 +16,8 @@ import java.util.jar.JarFile;
import java.util.logging.Level; import java.util.logging.Level;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.PluginDescription; import net.md_5.bungee.api.plugin.PluginDescription;
import net.md_5.bungee.util.CaseInsensitiveMap;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
public class ModuleManager public class ModuleManager
@ -25,27 +32,101 @@ public class ModuleManager
public void load(ProxyServer proxy) throws Exception public void load(ProxyServer proxy) throws Exception
{ {
ModuleVersion bungeeVersion = ModuleVersion.parse( "git:BungeeCord-Proxy:1.7-SNAPSHOT:\"93cf50b\":792" ); ModuleVersion bungeeVersion = ModuleVersion.parse( proxy.getVersion() );
if ( bungeeVersion == null )
{
System.out.println( "Could detect bungee version. Custom build?" );
return;
}
File moduleDirectory = new File( "modules" ); File moduleDirectory = new File( "modules" );
moduleDirectory.mkdir(); moduleDirectory.mkdir();
Map<String, ModuleSource> modules = new HashMap<>(); List<ModuleSpec> modules = new ArrayList<>();
File configFile = new File( "modules.yml" );
// Start Yaml
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK );
Yaml yaml = new Yaml( options );
// TODO: Use filename filter here and in PluginManager Map<String, Object> config;
Yaml yaml = new Yaml();
for ( File file : moduleDirectory.listFiles() ) configFile.createNewFile();
try ( InputStream is = new FileInputStream( configFile ) )
{ {
if ( file.isFile() && file.getName().endsWith( ".jar" ) ) config = (Map) yaml.load( is );
}
if ( config == null )
{ {
String moduleName = file.getName().substring( 0, file.getName().length() - 4 ); // 4 = .jar.length() config = new CaseInsensitiveMap();
ModuleSource source = modules.get( moduleName ); } else
{
config = new CaseInsensitiveMap( config );
}
// End yaml
List<String> defaults = new ArrayList<>();
if ( config.containsKey( "modules" ) )
{
defaults.addAll( (Collection) config.get( "modules" ) );
}
int version = ( config.containsKey( "version" ) ) ? (int) config.get( "version" ) : 0;
switch ( version )
{
case 0:
defaults.add( "jenkins://cmd_alert" );
defaults.add( "jenkins://cmd_find" );
defaults.add( "jenkins://cmd_list" );
defaults.add( "jenkins://cmd_send" );
defaults.add( "jenkins://cmd_server" );
}
config.put( "modules", defaults );
config.put( "version", 1 );
try ( FileWriter wr = new FileWriter( configFile ) )
{
yaml.dump( config, wr );
}
for ( String s : (List<String>) config.get( "modules" ) )
{
URI uri = new URI( s );
ModuleSource source = knownSources.get( uri.getScheme() );
if ( source == null ) if ( source == null )
{ {
System.out.println( "No source for module in file: " + file ); System.out.println( "Unknown module source: " + s );
continue;
}
String name = uri.getAuthority();
if ( name == null )
{
System.out.println( "Unknown module host: " + s );
continue; continue;
} }
ModuleSpec spec = new ModuleSpec( name, new File( moduleDirectory, name + ".jar" ), source );
modules.add( spec );
System.out.println( "Discovered module: " + spec );
}
for ( ModuleSpec module : modules )
{
ModuleVersion moduleVersion = ( module.getFile().exists() ) ? getVersion( module.getFile() ) : null;
if ( !bungeeVersion.equals( moduleVersion ) )
{
System.out.println( "Attempting to update plugin from " + moduleVersion + " to " + bungeeVersion );
module.getProvider().retrieve( module, bungeeVersion );
}
}
proxy.getPluginManager().detectPlugins( moduleDirectory );
}
private ModuleVersion getVersion(File file)
{
try ( JarFile jar = new JarFile( file ) ) try ( JarFile jar = new JarFile( file ) )
{ {
JarEntry pdf = jar.getJarEntry( "plugin.yml" ); JarEntry pdf = jar.getJarEntry( "plugin.yml" );
@ -53,19 +134,14 @@ public class ModuleManager
try ( InputStream in = jar.getInputStream( pdf ) ) try ( InputStream in = jar.getInputStream( pdf ) )
{ {
PluginDescription desc = yaml.loadAs( in, PluginDescription.class ); PluginDescription desc = new Yaml().loadAs( in, PluginDescription.class );
ModuleVersion moduleVersion = ModuleVersion.parse( desc.getVersion() ); return 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 ) } catch ( Exception ex )
{ {
ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not check module from file " + file, ex ); ProxyServer.getInstance().getLogger().log( Level.WARNING, "Could not check module from file " + file, ex );
} }
}
} return null;
} }
} }

View File

@ -9,5 +9,5 @@ public class ModuleSpec
private final String name; private final String name;
private final File file; private final File file;
private ModuleSource provider; private final ModuleSource provider;
} }

View File

@ -16,6 +16,12 @@ public class ModuleVersion
{ {
int lastColon = version.lastIndexOf( ':' ); int lastColon = version.lastIndexOf( ':' );
int secondLastColon = version.lastIndexOf( ':', lastColon - 1 ); int secondLastColon = version.lastIndexOf( ':', lastColon - 1 );
if ( lastColon == -1 || secondLastColon == -1 )
{
return null;
}
String buildNumber = version.substring( lastColon + 1, version.length() ); String buildNumber = version.substring( lastColon + 1, version.length() );
String gitCommit = version.substring( secondLastColon + 1, lastColon ).replaceAll( "\"", "" ); String gitCommit = version.substring( secondLastColon + 1, lastColon ).replaceAll( "\"", "" );