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    import nextapp.echo2.app.event.ActionListener;
008    
009    import com.sptci.echo2.Configuration;
010    import com.sptci.echo2.ErrorPane;
011    import com.sptci.echo2.LoginPane;
012    import com.sptci.echo2.LoginPaneController;
013    
014    /**
015     * The controller for the {@link LoginView}. Handles all actions/events
016     * triggered by the view.
017     *
018     * <p>Copyright 2007 Sans Pareil Technologies, Inc.</p>
019     * @author Rakesh Vidyadharan 2007-04-21
020     * @version $Id: LoginController.java 3252 2007-05-12 19:12:31Z rakesh $
021     */
022    class LoginController extends LoginPaneController
023    {
024      /**
025       * Create a new instance of the controller for {@link LoginView}
026       */
027      LoginController()
028      {
029        super();
030      }
031    
032      /**
033       * Implementation of the mandatory method defined in {@link
034       * com.sptci.echo2.LoginPaneController}.  Returns the appropriate
035       * <code>ActionListener</code> depending upon the component from which
036       * the <code>ActionEvent</code> was triggered.
037       *
038       * @param component The name of the component for which the ActionListener
039       *   is to be returned.
040       * @return The appropriate ActionListener instance.
041       */
042      @Override
043      public ActionListener getListener( String component )
044      {
045        ActionListener listener = null;
046    
047        if ( LoginPane.USERNAME.equals( component ) )
048        {
049          listener = new UserNameListener();
050        }
051        else
052        {
053          listener = new LoginListener();
054        }
055    
056        return listener;
057      }
058    
059      /**
060       * Check the {@link com.sptci.echo2.LoginPane#userName} field to ensure
061       * that the value specified is not <code>root</code>.
062       *
063       * @return Returns <code>true</code> if the user is not <code>root</code>.
064       */
065      public boolean checkRoot()
066      {
067        return ( ! "root".equals( view.getUserName() ) );
068      }
069    
070      /**
071       * The action listener that is used to trigger initialisation of a
072       * <code>telnet</code> session with the server.
073       */
074      class LoginListener implements ActionListener
075      {
076        /**
077         * Implementation of the <code>ActionListener</code> interface.  Handles
078         * login events triggered by {@link #view}.
079         *
080         * @see #checkView
081         * @see #checkRoot
082         * @see #displayError
083         * @see #login
084         */
085        public void actionPerformed( ActionEvent event )
086        {
087          if ( ! checkView() )
088          {
089            displayError( "NoCredentials.title", "NoCredentials.message" );
090          }
091          else if ( ! checkRoot() )
092          {
093            displayError( "NoRootLogin.title", "NoRootLogin.message" );
094          }
095          else
096          {
097            login();
098          }
099        }
100    
101        /**
102         * Login the user to the <code>telnet</code> server.
103         */
104        void login()
105        {
106          Application app = (Application) application;
107    
108          try
109          {
110            app.executor.connect( view.getUserName(), view.getPassword() );
111            logger.info( "User " + view.getUserName() +
112                " logged in successfully to change password." );
113            application.removePane( view );
114            application.addPane( new PasswordPane() );
115          }
116          catch ( AuthenticationFailedException afex )
117          {
118            handleException( afex );
119          }
120          catch ( IOException ioex )
121          {
122            handleException( ioex );
123          }
124          finally
125          {
126            app.executor.disconnect();
127          }
128        }
129    
130        /**
131         * Handles an {@link AuthenticationFailedException} when user enters
132         * invalid credentials.
133         *
134         * @see #displayError
135         * @param afex The exception that was thrown.
136         */
137        void handleException( AuthenticationFailedException afex )
138        {
139          logger.warning( "Invalid authentication request by user: " +
140              view.getUserName() );
141          displayError( "InvalidLogin.title", "InvalidLogin.message" );
142        }
143    
144        /**
145         * Handles an <code>IOException</code> when user tries to log  on.
146         *
147         * @see #displayError
148         * @param ioex The exception that was thrown.
149         */
150        void handleException( IOException ioex )
151        {
152          logger.log( Level.INFO,
153              "Unable to get telent connection to server", ioex );
154          displayError( "ConnectionError.title", "ConnectionError.message" );
155        }
156    
157        /**
158         * Display an {@link com.sptci.echo2.ErrorPane} with the title and
159         * message specified.
160         *
161         * @param title The key for the localised title to display
162         * @param message The key for the localised message to display
163         */
164        void displayError( String title, String message )
165        {
166          ErrorPane pane = new ErrorPane(
167              Configuration.getString( view, title ),
168              Configuration.getString( view, message ) );
169          application.addPane( pane );
170        }
171      }
172    }