FileDocCategorySizeDatePackage
UpdateInstallerImpl.javaAPI DocAzureus 3.0.3.46709Tue Aug 07 01:04:08 BST 2007org.gudy.azureus2.pluginsimpl.local.update

UpdateInstallerImpl

public class UpdateInstallerImpl extends Object implements UpdateInstaller
author
parg

Fields Summary
protected static final String
UPDATE_DIR
protected static final String
ACTIONS
protected static AEMonitor
class_mon
private UpdateManager
manager
private File
install_dir
Constructors Summary
protected UpdateInstallerImpl(UpdateManager _manager)

		manager	= _manager;
		
		try{
			class_mon.enter();
			
				// updates are in general user-specific (e.g. plugin updates) so store here
				// obviously core ones will affect all users
			
			String	update_dir = getUserDir() + File.separator + UPDATE_DIR;
			
			for (int i=1;i<1024;i++){
				
				File	try_dir = new File( update_dir + File.separator + "inst_" + i );
								
				if ( !try_dir.exists()){
					
					if ( !FileUtil.mkdirs(try_dir)){
		
						throw( new UpdateException( "Failed to create a temporary installation dir"));
					}
					
					install_dir	= try_dir;
					
					break;
				}
			}
			
			if ( install_dir == null ){
				
				throw( new UpdateException( "Failed to find a temporary installation dir"));
			}
		}finally{
			
			class_mon.exit();
		}
	
Methods Summary
public voidaddChangeRightsAction(java.lang.String rights, java.lang.String to_file)

 
		appendAction( "chmod," + rights  + "," + to_file );
	
public voidaddMoveAction(java.lang.String from_file_or_resource, java.lang.String to_file)

		// System.out.println( "move action:" + from_file_or_resource + " -> " + to_file );
		
		if ( from_file_or_resource.indexOf(File.separator) == -1 ){
			
			from_file_or_resource = install_dir.toString() + File.separator + from_file_or_resource;
		}
		
		try{
				// see if this action has a chance of succeeding
			
			File	to_f = new File( to_file );
			
			File	parent = to_f.getParentFile();
			
			if ( parent != null && !parent.exists()){
				
				parent.mkdirs();
			}
			
			if ( parent != null ){
				
				// we're going to need write access to the parent, let's try
				
				if ( !parent.canWrite()){
					
						// Vista install process goes through permissions elevation process
						// so don't warn about lack of write permissions
					
					if ( !Constants.isWindowsVista ){
						
						Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_WARNING,
							"The location '" + parent.toString()
									+ "' isn't writable, this update will probably fail."
									+ " Check permissions and retry the update"));
						
					}
				}
			}
							
			try{
				PlatformManager	pm = PlatformManagerFactory.getPlatformManager();
					
				if ( pm.hasCapability( PlatformManagerCapabilities.CopyFilePermissions )){
					
					String	parent_str = parent.getAbsolutePath();
										
					PlatformManagerFactory.getPlatformManager().copyFilePermissions(
							parent_str, from_file_or_resource );
				}
			}catch( Throwable e ){
					
				Debug.out( e );
			}
		}catch( Throwable e ){
			
		}
		
		appendAction( "move," + from_file_or_resource  + "," + to_file );
	
public voidaddRemoveAction(java.lang.String file)

 
		appendAction( "remove," + file );
	
public voidaddResource(java.lang.String resource_name, java.io.InputStream is)

		addResource(resource_name,is,true);
	
public voidaddResource(java.lang.String resource_name, java.io.InputStream is, boolean closeInputStream)

		try{
			File	target_file = new File(install_dir, resource_name );
		
			FileUtil.copyFile( is, new FileOutputStream( target_file ),closeInputStream);
			
		}catch( Throwable e ){
			
			throw( new UpdateException( "UpdateInstaller: resource addition fails", e ));
		}
	
protected voidappendAction(java.lang.String data)

		PrintWriter	pw = null;
	
		try{		
			
			pw = new PrintWriter(new FileWriter( install_dir.toString() + File.separator + ACTIONS, true ));

			pw.println( data );
			
		}catch( Throwable e ){
			
			throw( new UpdateException( "Failed to write actions file", e ));
			
		}finally{
			
			if ( pw != null ){
		
				try{
		
					pw.close();
					
				}catch( Throwable e ){
	
					throw( new UpdateException( "Failed to write actions file", e ));
				}
			}
		}
	
protected static voidcheckForFailedInstalls(UpdateManager manager)

	
	  
	
			 
	
		try{
			File	update_dir = new File( manager.getUserDir() + File.separator + UPDATE_DIR );
			
			File[]	dirs = update_dir.listFiles();
			
			if ( dirs != null ){
				
				boolean	found_failure = false;
				
				for (int i=0;i<dirs.length;i++){
					
					File	dir = dirs[i];
					
					if ( dir.isDirectory()){
						
							// if somethings here then the install failed
						
						found_failure	= true;
						
						FileUtil.recursiveDelete( dir );
					}
				}
				
				if ( found_failure ){
					Logger.log(new LogAlert(LogAlert.UNREPEATABLE, LogAlert.AT_ERROR,
							MessageText.getString("Alert.failed.update")));
				}
			}
		}catch( Throwable e ){
			
			Debug.printStackTrace( e );
		}
	
public java.lang.StringgetInstallDir()

		return( manager.getInstallDir());
	
public java.lang.StringgetUserDir()

		return( manager.getUserDir());