FileDocCategorySizeDatePackage
UpdateCheckInstanceImpl.javaAPI DocAzureus 3.0.3.48918Wed Dec 27 18:52:26 GMT 2006org.gudy.azureus2.pluginsimpl.local.update

UpdateCheckInstanceImpl

public class UpdateCheckInstanceImpl extends Object implements UpdateCheckInstance
author
parg

Fields Summary
private static final LogIDs
LOGID
private List
listeners
private List
updates
private List
decision_listeners
private AESemaphore
sem
private UpdateManager
manager
private int
check_type
private String
name
private UpdatableComponentImpl[]
components
private UpdateCheckerImpl[]
checkers
private boolean
completed
private boolean
cancelled
private boolean
automatic
protected AEMonitor
this_mon
Constructors Summary
protected UpdateCheckInstanceImpl(UpdateManager _manager, int _check_type, String _name, UpdatableComponentImpl[] _components)

	
	
	
						
									
								
			 
	
		manager		= _manager;
		check_type	= _check_type;
		name		= _name;
		components	= _components;
		
		checkers	= new UpdateCheckerImpl[components.length];
		
 		for (int i=0;i<components.length;i++){
			
			UpdatableComponentImpl	comp = components[i];
			
			checkers[i] = new UpdateCheckerImpl( this, comp, sem );
		}
	
Methods Summary
public voidaddDecisionListener(UpdateManagerDecisionListener l)

		decision_listeners.add(l);
	
public voidaddListener(UpdateCheckInstanceListener l)

		listeners.add( l );
		
		if ( completed ){
			
			l.complete( this );
			
		}else if ( cancelled ){
			
			l.cancelled( this );
		}
	
public voidaddUpdatableComponent(UpdatableComponent component, boolean mandatory)

			// add new component
		
		UpdatableComponentImpl	comp = new UpdatableComponentImpl( component, mandatory );
		
		UpdatableComponentImpl[]	new_comps = new UpdatableComponentImpl[components.length+1];
		
		System.arraycopy( components, 0, new_comps, 0, components.length );
		
		new_comps[components.length]	= comp;
		
		components	= new_comps;
		
			// add a new checker
		
		UpdateCheckerImpl	checker = new UpdateCheckerImpl( this, comp, sem );
		
		UpdateCheckerImpl[]	new_checkers = new UpdateCheckerImpl[checkers.length+1];
		
		System.arraycopy( checkers, 0, new_checkers, 0, checkers.length );
		
		new_checkers[checkers.length]	= checker;
		
		checkers	= new_checkers;
	
protected UpdateImpladdUpdate(UpdatableComponentImpl comp, java.lang.String update_name, java.lang.String[] desc, java.lang.String new_version, ResourceDownloader[] downloaders, int restart_required)

		try{
			this_mon.enter();
		
			UpdateImpl	update = 
				new UpdateImpl( this, update_name, desc, new_version, 
								downloaders, comp.isMandatory(), restart_required );
			
			updates.add( update );
						
			if ( cancelled ){
				
				update.cancel();
			}
			
			return( update );
			
		}finally{
			
			this_mon.exit();
		}
	
public voidcancel()

		boolean	just_do_updates = false;
		
		try{
			this_mon.enter();
			
			if ( completed ){
				
				just_do_updates = true;
			}
		
			cancelled	= true;
			
		}finally{
			
			this_mon.exit();
		}
			
		
		for (int i=0;i<updates.size();i++){
			
			((UpdateImpl)updates.get(i)).cancel();
		}

		if ( !just_do_updates ){
			
			for (int i=0;i<checkers.length;i++){
				
				if ( checkers[i] != null ){
					
					checkers[i].cancel();
				}
			}
			
			for (int i=0;i<listeners.size();i++){
					
				try{
					((UpdateCheckInstanceListener)listeners.get(i)).cancelled( this );
					
				}catch( Throwable e ){
					
					Debug.printStackTrace(e);
				}
			}
		}
	
public UpdateInstallercreateInstaller()

		return( manager.createInstaller());
	
public UpdateChecker[]getCheckers()

		return( checkers );
	
protected java.lang.ObjectgetDecision(Update update, int decision_type, java.lang.String decision_name, java.lang.String decision_description, java.lang.Object decision_data)

		for (int i=0;i<decision_listeners.size();i++){
			
			Object res = 
				((UpdateManagerDecisionListener)decision_listeners.get(i)).decide(
						update, decision_type, decision_name, decision_description, decision_data );
			
			if ( res != null ){
				
				return( res );
			}
		}
		
		return( null );
	
public UpdateManagergetManager()

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

		return( name );
	
public intgetType()

		return( check_type );
	
public Update[]getUpdates()

		try{
			this_mon.enter();
		
			Update[]	res = new Update[updates.size()];
		
			updates.toArray( res );
		
			return( res );
			
		}finally{
			
			this_mon.exit();
		}
	
public booleanisAutomatic()

		return( automatic );
	
public booleanisCancelled()

		return( cancelled );
	
public voidremoveDecisionListener(UpdateManagerDecisionListener l)

		decision_listeners.remove(l);
	
public voidremoveListener(UpdateCheckInstanceListener l)

		listeners.remove(l);
	
public voidsetAutomatic(boolean a)

		automatic = a;
	
public voidstart()

		for (int i=0;i<components.length;i++){
			
			final UpdateCheckerImpl			checker = checkers[i];
			
			Thread	t = 
				new AEThread( "UpdatableComponent Checker:" + i )
				{
					public void
					runSupport()
					{					
						try{		
							checker.getComponent().checkForUpdate( checker );
							
						}catch( Throwable e ){
							
							checker.failed();
						}
					}
				};
				
			t.setDaemon( true );
			
			t.start();
		}
		
		Thread	t = 
			new AEThread( "UpdatableComponent Completion Waiter" )
			{
				public void
				runSupport()
				{
					for (int i=0;i<components.length;i++){
			
						sem.reserve();
					}
					
					try{
						this_mon.enter();
						
						if ( cancelled ){
							
							return;
						}
					
						completed	= true;
						
					}finally{
						
						this_mon.exit();
					}
					
					boolean	mandatory_failed = false;
					
					for (int i=0;i<checkers.length;i++){
						
						if ( components[i].isMandatory() && checkers[i].getFailed()){
							
							mandatory_failed	= true;
							
							break;
						}
					}
					
					List	target_updates = new ArrayList();
					
						// if any mandatory checks failed then we can't do any more
					
					if ( mandatory_failed ){
						
						if (Logger.isEnabled())
							Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
										"Dropping all updates as a mandatory update check failed"));

					}else{
							// If there are any manadatory updates then we just go ahead with them and drop the rest
						
						boolean	mandatory_only	= false;
						
						for (int i=0;i<updates.size();i++){
							
							UpdateImpl	update = (UpdateImpl)updates.get(i);
							
							if ( update.isMandatory()){
								
								mandatory_only	= true;
								
								break;
							}
						}
						
						for (int i=0;i<updates.size();i++){
							
							UpdateImpl	update = (UpdateImpl)updates.get(i);
														
							if ( update.isMandatory() || !mandatory_only ){
								
								target_updates.add( update );
								
							}else{
								if (Logger.isEnabled())
								  Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR,
								                          "Dropping update '" + update.getName()
                                            + "' as non-mandatory and "
                                            + "mandatory updates found"));
							}
						}
					}

					updates	= target_updates;
					
					for (int i=0;i<listeners.size();i++){
					
						try{
							((UpdateCheckInstanceListener)listeners.get(i)).complete( UpdateCheckInstanceImpl.this );
							
						}catch( Throwable e ){
							
							Debug.printStackTrace(e);
						}
					}
				}
			};
			
		t.setDaemon(true);
		
		t.start();