FileDocCategorySizeDatePackage
SWTUpdateChecker.javaAPI DocAzureus 3.0.3.411960Mon Jul 16 13:51:36 BST 2007org.gudy.azureus2.ui.swt.updater2

SWTUpdateChecker

public class SWTUpdateChecker extends Object implements org.gudy.azureus2.plugins.update.UpdatableComponent
author
Olivier Chalouhi

Fields Summary
private static final LogIDs
LOGID
private static final String
OSX_APP
Constructors Summary
public SWTUpdateChecker()

    
  
Methods Summary
public voidcheckForUpdate(org.gudy.azureus2.plugins.update.UpdateChecker checker)

  	try{
	    SWTVersionGetter versionGetter = new SWTVersionGetter( checker );
	    
     	boolean	update_required  = 	versionGetter.needsUpdate() && 
     								System.getProperty("azureus.skipSWTcheck") == null ;
    	
	    if ( update_required ){
        	    
	       	int	update_prevented_version = COConfigurationManager.getIntParameter( "swt.update.prevented.version", -1 );

	    	try{
		        URL	swt_url = SWT.class.getClassLoader().getResource("org/eclipse/swt/SWT.class");
		        
		        if ( swt_url != null ){
		        	
		        	String	url_str = swt_url.toExternalForm();
	
		        	if ( url_str.startsWith("jar:file:")){
	
		        		File jar_file = FileUtil.getJarFileFromURL(url_str);
		        		
		        		String	expected_location;
		        		
		        	    if ( Constants.isOSX ){
		        	        	  
		        	    	expected_location = checker.getCheckInstance().getManager().getInstallDir() + OSX_APP + "/Contents/Resources/Java";
		        	            
		        	    }else{ 
		        	        	  
		        	    	expected_location = checker.getCheckInstance().getManager().getInstallDir();
		        	    }
		        	    
		        	    File	expected_dir = new File( expected_location );
		        	    
		        	    File	jar_file_dir = jar_file.getParentFile();
		        	    
		        	    	// sanity check
		        	    
		        	    if ( expected_dir.exists() && jar_file_dir.exists() ){
		        	    	
		        	    	expected_dir	= expected_dir.getCanonicalFile();
		        	    	jar_file_dir	= jar_file_dir.getCanonicalFile();
		        	    	
		        	    	if ( expected_dir.equals( jar_file_dir )){
		        	    	
		        	    			// everything looks ok
		        	    		
		        	    		if ( update_prevented_version != -1 ){
		        	    			
		        	    			update_prevented_version	= -1;
		        	    			
			        	    		COConfigurationManager.setParameter( "swt.update.prevented.version", update_prevented_version );
		        	    		}
		        	    	}else{
		        	    		
		        	    			// we need to periodically remind the user there's a problem as they need to realise that
		        	    			// it is causing ALL updates (core/plugin) to fail
		        	    		
		        	    		long	last_prompt = COConfigurationManager.getLongParameter( "swt.update.prevented.version.time", 0 );
		        	    		long	now			= SystemTime.getCurrentTime();
		        	    		
		        	    		boolean force = now < last_prompt || now - last_prompt > 7*24*60*60*1000;
		        	    		
		        	    		if ( !checker.getCheckInstance().isAutomatic()){
		        	    			
		        	    			force = true;
		        	    		}
		        	    		
		        		    	if ( force || update_prevented_version != versionGetter.getCurrentVersion()){
			        		    		
			        	    		String	alert = 
			        	    			MessageText.getString( 
			        	    					"swt.alert.cant.update", 
			        	    					new String[]{
				        	    					String.valueOf( versionGetter.getCurrentVersion()),
				        	    					String.valueOf( versionGetter.getLatestVersion()),
			        	    						jar_file_dir.toString(), 
			        	    						expected_dir.toString()});
			        	    		
			        	     		Logger.log(	new LogAlert(LogAlert.REPEATABLE, LogEvent.LT_ERROR, alert ));
			        						
			        	     		update_prevented_version = versionGetter.getCurrentVersion();
			        	     		
			        	    		COConfigurationManager.setParameter( "swt.update.prevented.version", update_prevented_version );
			        	    		COConfigurationManager.setParameter( "swt.update.prevented.version.time", now );
		        		    	}
		        	    	}
		        	    }
		        	}
		        }
	    	}catch( Throwable e ){
		    	
		    	Debug.printStackTrace(e);		    	
	    	}
	    
		    if ( update_prevented_version == versionGetter.getCurrentVersion()){
			
		    	Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "SWT update aborted due to previously reported issues regarding its install location" ));
		    			
				checker.failed();
				
				checker.getCheckInstance().cancel();
				
				return;
		    }
	    	    	 
	      String[] mirrors = versionGetter.getMirrors();
	      
	      ResourceDownloader swtDownloader = null;
	      
          ResourceDownloaderFactory factory = ResourceDownloaderFactoryImpl.getSingleton();
          List downloaders =  new ArrayList();
          for(int i = 0 ; i < mirrors.length ; i++) {
            try {
              downloaders.add(factory.getSuffixBasedDownloader(factory.create(new URL(mirrors[i]))));
            } catch(MalformedURLException e) {
              //Do nothing
            	if (Logger.isEnabled())
								Logger.log(new LogEvent(LOGID, LogEvent.LT_WARNING,
										"Cannot use URL " + mirrors[i] + " (not valid)"));
            }
          }
          ResourceDownloader[] resourceDownloaders = 
            (ResourceDownloader[]) 
            downloaders.toArray(new ResourceDownloader[downloaders.size()]);
          
          swtDownloader = factory.getRandomDownloader(resourceDownloaders);
	      
	      	// get the size so its cached up
	      
	      try{
	      	swtDownloader.getSize();
	      	
	      }catch( ResourceDownloaderException e ){
	      
	      	Debug.printStackTrace( e );
	      }
	      
	      final Update update = 
	    	  checker.addUpdate("SWT Library for " + versionGetter.getPlatform(),
		          new String[] {"SWT is the graphical library used by Azureus"},
		          "" + versionGetter.getLatestVersion(),
		          swtDownloader,
		          Update.RESTART_REQUIRED_YES
	          );
	      
	      update.setDescriptionURL(versionGetter.getInfoURL());
	      
	      swtDownloader.addListener(new ResourceDownloaderAdapter() {
		        
		        public boolean completed(ResourceDownloader downloader, InputStream data) {
		          //On completion, process the InputStream to store temp files
		          return processData(checker,update,downloader,data);
		        }
		      });
	    }
  	}catch( Throwable e ){
  		Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
					"SWT Version check failed", e));
  		
  		checker.failed();
  		
  	}finally{
  		
  		checker.completed();
  	}
    
  
public intgetMaximumCheckTime()

    return( 30 ); // !!!! TODO: fix this
  
public java.lang.StringgetName()

    return( "SWT library" );
  
public static voidinitialize()

  
    
  
  
    PluginInitializer.getDefaultInterface().getUpdateManager().registerUpdatableComponent(new SWTUpdateChecker(),true);
  
private booleanprocessData(org.gudy.azureus2.plugins.update.UpdateChecker checker, org.gudy.azureus2.plugins.update.Update update, org.gudy.azureus2.plugins.utils.resourcedownloader.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();
        
        	// all jars
        
        if ( name.endsWith( ".jar" )){
        	
          installer.addResource(name,zip,false);
          
          if ( Constants.isOSX ){
        	  
            installer.addMoveAction(name,installer.getInstallDir() + OSX_APP + "/Contents/Resources/Java/" + name);
            
          }else{ 
        	  
            installer.addMoveAction(name,installer.getInstallDir() + File.separator + name);
          }
        }else if ( name.endsWith(".jnilib") && Constants.isOSX ){
        	
        	  //on OS X, any .jnilib
        	
          installer.addResource(name,zip,false);
          
          installer.addMoveAction(name,installer.getInstallDir() + OSX_APP + "/Contents/Resources/Java/dll/" + name);
          
        }else if ( name.equals("java_swt")){
        	
            //on OS X, java_swt (the launcher to start SWT applications)
        	   
          installer.addResource(name,zip,false);
          
          installer.addMoveAction(name,installer.getInstallDir() + OSX_APP + "/Contents/MacOS/" + name);
          
          installer.addChangeRightsAction("755",installer.getInstallDir() + OSX_APP + "/Contents/MacOS/" + name);
          
        }else if( name.endsWith( ".dll" ) || name.endsWith( ".so" ) || name.indexOf( ".so." ) != -1 ) {
        	
           	// native stuff for windows and linux
        	 
          installer.addResource(name,zip,false);
          
          installer.addMoveAction(name,installer.getInstallDir() + File.separator + name);
  
        }else if ( name.equals("javaw.exe.manifest") || name.equals( "azureus.sig" )){
        	
        	// silently ignore this one 
        }else{
        	
    	   Debug.outNoStack( "SWTUpdate: ignoring zip entry '" + name + "'" );
       }
      }     
    } catch(Exception e) {
  		Logger.log(new LogAlert(LogAlert.UNREPEATABLE,
				"SWT Update failed", e));
      return false;
    }finally{
    	if ( zip != null ){
    		
    		try{
    			  zip.close();
    			  
    		}catch( Throwable e ){
    		}
    	}
    }
        
    return true;