001    package com.sptci.system;
002    
003    import java.io.IOException;
004    import java.util.logging.Level;
005    
006    import nextapp.echo2.app.event.ActionEvent;
007    
008    import com.sptci.echo2.Configuration;
009    import com.sptci.echo2.ErrorPane;
010    import com.sptci.echo2.Listener;
011    
012    /**
013     * The action listener that is used to trigger the password change request
014     * using the <code>telnet</code> session with the server.
015     *
016     * <p>Copyright 2007 Sans Pareil Technologies, Inc.</p>
017     * @author Rakesh Vidyadharan 2007-04-22
018     * @version $Id: ChangePasswordListener.java 3252 2007-05-12 19:12:31Z rakesh $
019     */
020    class ChangePasswordListener extends Listener<PasswordController>
021    {
022      /**
023       * Create a new instance of the listner associated with the specified
024       * controller.
025       *
026       * @param controller The controller to use to access the controller.getView().
027       */
028      ChangePasswordListener( PasswordController controller )
029      {
030        super( controller );
031      }
032    
033      /**
034       * Implementation of the <code>ActionListener</code> interface.  Handles
035       * login events triggered by {@link PasswordPane}.
036       *
037       * @see #change
038       */
039      public void actionPerformed( ActionEvent event )
040      {
041        change();
042      }
043    
044      /**
045       * Change the password of the currently logged in user.
046       *
047       * @see PasswordController#checkView
048       */
049      private void change()
050      {
051        Application app = ( (Application) controller.getApplication() );
052    
053        try
054        {
055          controller.checkView();
056    
057          app.executor.connect( controller.getView().getPassword() );
058          app.executor.password( controller.getView().getPassword(),
059              controller.getView().getNewPassword() );
060          logger.info( "User " + app.executor.getUser() + " changed password." );
061          controller.getApplication().removePane( controller.getView() );
062          controller.getApplication().addPane( new Success() );
063        }
064        catch ( AuthenticationFailedException afex )
065        {
066          handleException( afex );
067        }
068        catch ( PasswordException pex )
069        {
070          handleException( pex );
071        }
072        catch ( IOException ioex )
073        {
074          handleException( ioex );
075        }
076        finally
077        {
078          app.executor.disconnect();
079        }
080      }
081    
082      /**
083       * Handle an {@link AuthenticationFailedException} that can be triggered
084       * when user attempts to change his password.
085       *
086       * @param afex The exception that was thrown.
087       */
088      private void handleException( AuthenticationFailedException afex )
089      {
090        ErrorPane pane = new ErrorPane(
091            Configuration.getString(
092            controller.getView(), "IncorrectPassword.title" ),
093            Configuration.getString(
094            controller.getView(), "IncorrectPassword.message" ) );
095        ( (Application) controller.getApplication() ).addPane( pane );
096      }
097    
098      /**
099       * Handle an {@link PasswordException} that can be triggered
100       * when user attempts to change his password.
101       *
102       * @param pex The exception that was thrown.
103       */
104      private void handleException( PasswordException pex )
105      {
106        Application app = ( (Application) controller.getApplication() );
107        ErrorPane pane = new ErrorPane(
108            Configuration.getString(
109            controller.getView(), "InvalidPassword.title" ),
110            pex.getMessage() );
111        app.addPane( pane );
112      }
113    
114      /**
115       * Handle an <code>IOException</code> that can be triggered
116       * when user attempts to change his password.
117       *
118       * @param ioex The exception that was thrown.
119       */
120      private void handleException( IOException ioex )
121      {
122        logger.log( Level.INFO,
123            "Unable to get telent connection to server", ioex );
124        ErrorPane pane = new ErrorPane(
125            Configuration.getString(
126            controller.getView(), "ConnectionError.title" ),
127            Configuration.getString(
128            controller.getView(), "ConnectionError.message" ) );
129        ( (Application) controller.getApplication() ).addPane( pane );
130      }
131    }