FileDocCategorySizeDatePackage
CorePatchChecker.javaAPI DocAzureus 3.0.3.47439Fri Sep 21 14:14:00 BST 2007org.gudy.azureus2.update

CorePatchChecker

public class CorePatchChecker extends Object implements UpdateCheckInstanceListener, UpdatableComponent, Plugin
author
parg

Fields Summary
private static final LogIDs
LOGID
public static final boolean
TESTING
protected PluginInterface
plugin_interface
Constructors Summary
Methods Summary
public voidcancelled(UpdateCheckInstance instance)

	
public voidcheckForUpdate(UpdateChecker checker)

		try{
			UpdateCheckInstance	inst = checker.getCheckInstance();
		
			inst.addListener( this );
		
			checker.addUpdate( "Core Patch Checker", new String[0], "",
								new ResourceDownloader[0],
								Update.RESTART_REQUIRED_MAYBE );
		}finally{
			
			checker.completed();
		}
	
public voidcomplete(UpdateCheckInstance instance)

		Update[]	updates = instance.getUpdates();
		
		final PluginInterface updater_plugin = plugin_interface.getPluginManager().getPluginInterfaceByClass( UpdaterUpdateChecker.class );
		
		for (int i=0;i<updates.length;i++){
			
			final Update	update = updates[i];
						
			Object	user_object = update.getUserObject();
			
			if ( user_object != null && user_object == updater_plugin ){
				
				// OK, we have an updater update
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, "Core Patcher: updater update found"));
				
				update.setRestartRequired( Update.RESTART_REQUIRED_MAYBE );
				
				update.addListener(new UpdateListener() {
					public void complete(Update update) {
						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID,
									"Core Patcher: updater update complete"));

						patch(instance, update, updater_plugin);
					}
				});
			}
		}
	
public intgetMaximumCheckTime()

		return( 0 );
	
public java.lang.StringgetName()

		return( "Core Patch Checker");
	
public voidinitialize(PluginInterface _plugin_interface)

	
	  
	
		  
	  
	  	 
	
		plugin_interface	= _plugin_interface;
		
		plugin_interface.getPluginProperties().setProperty( "plugin.version", 	"1.0" );
		plugin_interface.getPluginProperties().setProperty( "plugin.name", 		"Core Patcher (level=" + CorePatchLevel.getCurrentPatchLevel() + ")" );

		if ( TESTING || !Constants.isCVSVersion()){
		
			if ( TESTING ){
				
				System.out.println( "CorePatchChecker: TESTING !!!!" );
			}
			
			plugin_interface.getUpdateManager().registerUpdatableComponent( this, false );
		}
	
protected voidpatch(UpdateCheckInstance instance, Update updater_update, PluginInterface updater_plugin)

		try{
				// use the update plugin to log stuff....
			
			ResourceDownloader rd_log = updater_update.getDownloaders()[0];

			File[]	files = new File(updater_plugin.getPluginDirectoryName()).listFiles();
			
			if ( files == null ){
			
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID,
							"Core Patcher: no files in plugin dir!!!"));
			
				return;
			}
		
			String	patch_prefix = "Azureus2_" + Constants.getBaseVersion() + "_P";
			
			int		highest_p		= -1;
			File	highest_p_file 	= null;
			
			for (int i=0;i<files.length;i++){
			
				String	name = files[i].getName();
				
				if ( name.startsWith( patch_prefix ) && name.endsWith( ".pat" )){
			
					if (Logger.isEnabled())
						Logger.log(new LogEvent(LOGID, "Core Patcher: found patch file '"
								+ name + "'"));
					
					try{
						int	this_p = Integer.parseInt( name.substring( patch_prefix.length(), name.indexOf( ".pat" )));
						
						if ( this_p > highest_p ){
							
							highest_p = this_p;
							
							highest_p_file	= files[i];
						}
					}catch( Throwable e ){
						
						Debug.printStackTrace( e );
					}
				}
			}
			
			if ( CorePatchLevel.getCurrentPatchLevel() >= highest_p ){
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID,
							"Core Patcher: no applicable patch found (highest = " + highest_p
									+ ")"));
				
					// flip back from maybe as we now know it isn't needed
				
				if ( updater_update.getRestartRequired() == Update.RESTART_REQUIRED_MAYBE ){
					
					updater_update.setRestartRequired( Update.RESTART_REQUIRED_NO );
				}
			}else{
				
				rd_log.reportActivity( "Applying patch '" + highest_p_file.getName() + "'");
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, "Core Patcher: applying patch '"
							+ highest_p_file.toString() + "'"));
				
				InputStream pis = new FileInputStream( highest_p_file );
								
				patchAzureus2( instance, pis, "P" + highest_p, plugin_interface.getLogger().getChannel( "CorePatcher" ));
				
				Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_INFORMATION,
						"Patch " + highest_p_file.getName() + " ready to be applied"));
				
				String done_file = highest_p_file.toString();
				
				done_file = done_file.substring(0,done_file.length()-1) + "x";
				
				highest_p_file.renameTo( new File( done_file ));
				
					// flip the original update over to 'restart required'
				
				updater_update.setRestartRequired( Update.RESTART_REQUIRED_YES );
			}
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "Core Patcher failed", e));
		}
	
public static voidpatchAzureus2(UpdateCheckInstance instance, java.io.InputStream pis, java.lang.String resource_tag, org.gudy.azureus2.plugins.logging.LoggerChannel log)

		String resource_name = "Azureus2_" + resource_tag + ".jar";

		UpdateInstaller	installer = instance.createInstaller();
		
		File	tmp = AETemporaryFileHandler.createTempFile();
						
		OutputStream	os = new FileOutputStream( tmp );
		
		String	az2_jar;

		if( Constants.isOSX ){

			az2_jar = installer.getInstallDir() + "/" + SystemProperties.getApplicationName() + ".app/Contents/Resources/Java/";
			
		}else{

			az2_jar = installer.getInstallDir() + File.separator;
		}
		
		az2_jar	+= "Azureus2.jar";				
		
		InputStream	is 	= new FileInputStream( az2_jar );
		
		new UpdateJarPatcher( is, pis, os, log );
		
		is.close();
		
		pis.close();
		
		os.close();
		
		installer.addResource(  resource_name,
								new FileInputStream( tmp ));
		
		tmp.delete();
		
		installer.addMoveAction( resource_name, az2_jar );