package mit.util.ui ;

public class ValidServiceComponent
extends mit.swing.xJComponent
implements	mit.util.event.ValidServiceRaiser
	,	mit.event.Listener
{
	private mit.event.RaiserAdapter raiserAdapter = new mit.event.RaiserAdapter( this ) ;
	public mit.event.RaiserAdapter getRaiserAdapter() { return this.raiserAdapter ; }
	public void setRaiserAdapter( mit.event.RaiserAdapter raiserAdapter ) { this.raiserAdapter = raiserAdapter ; }

	private mit.event.ListenerAdapter listenerAdapter = new mit.event.ListenerAdapter( this ) ;
	public mit.event.ListenerAdapter getListenerAdapter() { return this.listenerAdapter ; }
	public void setListenerAdapter( mit.event.ListenerAdapter listenerAdapter ) { this.listenerAdapter = listenerAdapter ; }

	private String service = "rcmd" ;
	public String getService() { return this.service ; }
	public void setService( String service ) { this.service = service ; }

	private String host = null ;
	public String getHost() { return this.host ; }
	public void setHost( String host ) { this.host = host ; }

	private String userName = null ;
	public String getUserName() { return this.userName ; }
	public void setUserName( String userName ) { this.userName = userName ; }

	private String userInst = null ;
	public String getUserInst() { return this.userInst ; }
	public void setUserInst( String userInst ) { this.userInst = userInst ; }

	private byte[] serviceTicket = null ;
	public byte[] getServiceTicket() { return this.serviceTicket ; }
	public void setServiceTicket( byte[] serviceTicket ) { this.serviceTicket = serviceTicket ; }

	private krb4.lib.Krb4Creds creds = null ;
	public krb4.lib.Krb4Creds getCreds() { return this.creds ; }
	public void setCreds( krb4.lib.Krb4Creds creds ) { this.creds = creds ; }

	public void eventRaised( mit.event.Event e )
	{
		try
		{
			if( e instanceof mit.krb4.event.ValidUserEvent )
			{
				HandleValidUserEvent( ( mit.krb4.event.ValidUserEvent ) e ) ;
			}
			else if( e instanceof mit.comm.event.HostEvent )
			{
				HandleHostEvent( ( mit.comm.event.HostEvent ) e ) ;
			}
		}
		catch( Throwable ex )
		{
			ex.printStackTrace() ;
		}
	}

	private void HandleValidUserEvent( mit.krb4.event.ValidUserEvent e )
	throws Throwable
	{
		mit.krb4.event.ValidUserRaiser r = ( mit.krb4.event.ValidUserRaiser ) e.getSource() ;
		setCreds( r.getCreds() ) ;
		setUserName( r.getUserName() ) ;
		setUserInst( r.getUserInst() ) ;
		validateService() ;
	}

	private void HandleHostEvent( mit.comm.event.HostEvent e )
	throws Throwable
	{
		mit.comm.event.HostRaiser r = ( mit.comm.event.HostRaiser ) e.getSource() ;
		setHost( r.getHost() ) ;
		setService( r.getService() ) ;
		validateService() ;
	}

	private void validateService()
	{
		try
		{
			if( ( null != getCreds() )
			&&  ( null != getService() )
			&&  ( null != getHost() )
			&&  ( null != getUserName() )
			)
			{
				setServiceTicket( mit.krb4.Authenticate.getServiceTicket( getCreds() , getService() , getHost() , getUserName() , getUserInst() ) ) ;
				if( null != getServiceTicket() )
				{
					( new mit.util.event.ValidServiceEvent( this ) ).raise() ;
				}
			}
		}
		catch( Throwable ex )
		{
			System.out.println( "ValidServiceComponent.validateService exception"
						+ "\n\tcreds: " + getCreds()
						+ "\n\tService: " + getService()
						+ "\n\tHost: " + getHost()
						+ "\n\tService: " + getService()
						+ "\n\tUserName: " + getUserName()
						+ "\n\tUserInst: " + getUserInst()
						) ;
		}
	}

	public void addNotify()
	{
		super.addNotify() ;
		try
		{
			setVisible( false ) ;

			getListenerAdapter().addHandled( mit.comm.event.HostEvent.class ) ;
			getListenerAdapter().addHandled( mit.krb4.event.ValidUserEvent.class ) ;
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
		}
	}
}