FileDocCategorySizeDatePackage
PlatformManagerUpdateChecker.javaAPI DocAzureus 3.0.3.48921Mon Jul 16 13:51:40 BST 2007org.gudy.azureus2.platform.win32

PlatformManagerUpdateChecker

public class PlatformManagerUpdateChecker extends Object implements UpdatableComponent, Plugin
author
parg

Fields Summary
private static final LogIDs
LOGID
public static final String
UPDATE_NAME
public static final int
RD_SIZE_RETRIES
public static final int
RD_SIZE_TIMEOUT
protected PluginInterface
plugin_interface
Constructors Summary
Methods Summary
public voidcheckForUpdate(UpdateChecker checker)

		try{						
			SFPluginDetails	sf_details = SFPluginDetailsLoaderFactory.getSingleton().getPluginDetails( plugin_interface.getPluginID());
					
			String	current_version = plugin_interface.getPluginVersion();
			
			if (Logger.isEnabled())
				Logger.log(new LogEvent(LOGID,
						"PlatformManager:Win32 update check starts: current = "
								+ current_version));
						
			boolean current_az_is_cvs	= Constants.isCVSVersion();
						
			String sf_plugin_version	= sf_details.getVersion();
			
			String sf_comp_version	 	= sf_plugin_version;
	
			if ( current_az_is_cvs ){
				
				String	sf_cvs_version = sf_details.getCVSVersion();
				
				if ( sf_cvs_version.length() > 0 ){
					
						// sf cvs version ALWAYS entry in _CVS
					
					sf_plugin_version	= sf_cvs_version;
					
					sf_comp_version = sf_plugin_version.substring(0,sf_plugin_version.length()-4);
				}
			}
			
			String	target_version	= null;			

			if (	 sf_comp_version.length() == 0 ||
					!Character.isDigit(sf_comp_version.charAt(0))){
				
				if (Logger.isEnabled())
					Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
							"PlatformManager:Win32 no valid version to check against ("
									+ sf_comp_version + ")"));

			}else if ( Constants.compareVersions( current_version, sf_comp_version ) < 0 ){
				
				target_version	= sf_comp_version;
			}
	
			checker.reportProgress( "Win32: current = " + current_version + ", latest = " + sf_comp_version );
			
			if (Logger.isEnabled())
				Logger.log(new LogEvent(LOGID,
						"PlatformManager:Win32 update required = "
								+ (target_version != null)));
			
			if ( target_version != null ){
					
				String target_download		= sf_details.getDownloadURL();
		
				if ( current_az_is_cvs ){
					
					String	sf_cvs_version = sf_details.getCVSVersion();
					
					if ( sf_cvs_version.length() > 0 ){
												
						target_download	= sf_details.getCVSDownloadURL();
					}
				}				

				ResourceDownloaderFactory rdf = ResourceDownloaderFactoryImpl.getSingleton();
				
				ResourceDownloader direct_rdl = rdf.create( new URL( target_download ));
			
				String	torrent_download = Constants.AELITIS_TORRENTS;
				
				int	slash_pos = target_download.lastIndexOf("/");
				
				if ( slash_pos == -1 ){
					
					torrent_download += target_download;
					
				}else{
					
					torrent_download += target_download.substring( slash_pos + 1 );
				}
				
				torrent_download	+= ".torrent";
				
				ResourceDownloader torrent_rdl = rdf.create( new URL( torrent_download ));

				torrent_rdl	= rdf.getSuffixBasedDownloader( torrent_rdl );
				
					// create an alternate downloader with torrent attempt first
				
				ResourceDownloader alternate_rdl = rdf.getAlternateDownloader( new ResourceDownloader[]{ torrent_rdl, direct_rdl });

					// get size here so it is cached
				
				rdf.getTimeoutDownloader(rdf.getRetryDownloader(alternate_rdl,RD_SIZE_RETRIES),RD_SIZE_TIMEOUT).getSize();
				
			
				List	update_desc = new ArrayList();
				
				List	desc_lines = HTMLUtils.convertHTMLToText( "", sf_details.getDescription());
								
				update_desc.addAll( desc_lines );
								
				List	comment_lines = HTMLUtils.convertHTMLToText( "    ", sf_details.getComment());
				
				update_desc.addAll( comment_lines );

				String[]	update_d = new String[update_desc.size()];
				
				update_desc.toArray( update_d );

				final Update	update = 
					checker.addUpdate(
						UPDATE_NAME,
						update_d,
						target_version,
						alternate_rdl,
						Update.RESTART_REQUIRED_YES );
				
				update.setDescriptionURL(sf_details.getInfoURL());
				
				alternate_rdl.addListener( 
						new ResourceDownloaderAdapter()
						{
							public boolean
							completed(
								final ResourceDownloader	downloader,
								InputStream					data )
							{	
								installUpdate( checker, update, downloader, data );
									
								return( true );
							}							
						});
			}
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
			
			checker.failed();
			
		}finally{
			
			checker.completed();
		}
	
public intgetMaximumCheckTime()

		return(( RD_SIZE_RETRIES * RD_SIZE_TIMEOUT )/1000);
	
public java.lang.StringgetName()

		return( UPDATE_NAME );
	
public voidinitialize(PluginInterface _plugin_interface)

	
	 
	
			
	
		plugin_interface	= _plugin_interface;
		
		plugin_interface.getPluginProperties().setProperty( "plugin.name", "Platform-Specific Support" );

		String	version = "1.0";	// default version if plugin not present
		
		PlatformManager platform	= PlatformManagerFactory.getPlatformManager();

		if (  platform.getPlatformType() == PlatformManager.PT_WINDOWS ){

			if ( platform.hasCapability( PlatformManagerCapabilities.GetVersion )){
				
				try{
					version = platform.getVersion();
					
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
			
			plugin_interface.getUpdateManager().registerUpdatableComponent( this, false );
			
		}else{
			
			plugin_interface.getPluginProperties().setProperty( "plugin.version.info", "Not required for this platform" );
			
		}
		
		plugin_interface.getPluginProperties().setProperty( "plugin.version", version );
	
protected voidinstallUpdate(UpdateChecker checker, Update update, ResourceDownloader rd, java.io.InputStream data)

		ZipInputStream zip = null;
		
		try {
			data = update.verifyData( data, true );

			rd.reportActivity( "Data verified successfully" );
			
			UpdateInstaller installer = checker.createInstaller();

			zip = new ZipInputStream(data);

			ZipEntry entry = null;

			while ((entry = zip.getNextEntry()) != null) {

				String name = entry.getName();

				if (name.toLowerCase().startsWith("windows/")) {

					// win32 only files

					name = name.substring(8);

					// skip the directory entry

					if (name.length() > 0) {

						rd.reportActivity("Adding update action for '" + name + "'");

						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID,
									"PlatformManager:Win32 adding action for '" + name + "'"));

						installer.addResource(name, zip, false);

						installer.addMoveAction(name, installer.getInstallDir()
								+ File.separator + name);
					}
				}
			}
		} catch (Throwable e) {

			rd.reportActivity("Update install failed:" + e.getMessage());
		}finally{
			
			if ( zip != null ){
				
				try{
					zip.close();
					
				}catch( Throwable e ){
				}
			}
		}
	
protected java.util.ListsplitMultiLine(java.lang.String indent, java.lang.String text)

		int		pos = 0;
		
		String	lc_text = text.toLowerCase();
		
		List	lines = new ArrayList();
		
		while( true ){
			
			String	line;
			
			int	p1 = lc_text.indexOf( "<br>", pos );
			
			if ( p1 == -1 ){
				
				line = text.substring(pos);
				
			}else{
				
				line = text.substring(pos,p1);
				
				pos = p1+4;
			}
			
			lines.add( indent + line );
			
			if ( p1 == -1 ){
				
				break;
			}
		}
		
		return( lines );