001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.security;
017    
018    import javax.swing.JOptionPane;
019    
020    import org.springframework.richclient.application.ApplicationServicesLocator;
021    import org.springframework.richclient.command.support.ApplicationWindowAwareCommand;
022    import org.springframework.security.Authentication;
023    
024    /**
025     * Provides a command to log the current user out.
026     * <p>
027     * Logout handling is performed by calling {@link ApplicationSecurityManager#doLogout()}.
028     * See that class for details.
029     * <P>
030     * No server-side call will occur to indicate logout. If this is required, you
031     * should extend this class and use the {@link #onLogout} method.
032     * 
033     * @author Ben Alex
034     * @author Larry Streepy
035     */
036    public class LogoutCommand extends ApplicationWindowAwareCommand {
037        private static final String ID = "logoutCommand";
038    
039        public LogoutCommand() {
040            super(ID);
041        }
042    
043        private boolean displaySuccess = true;
044    
045        /**
046         * Indicates whether an information message is displayed to the user upon
047         * successful logout. Defaults to true.
048         * 
049         * @param displaySuccess
050         *            displays an information message upon successful logout if
051         *            true, otherwise false
052         */
053        public void setDisplaySuccess(boolean displaySuccess) {
054            this.displaySuccess = displaySuccess;
055        }
056    
057        protected void doExecuteCommand() {
058            ApplicationSecurityManager sm = (ApplicationSecurityManager)ApplicationServicesLocator.services().getService(ApplicationSecurityManager.class);
059            Authentication loggedOutAuth = sm.doLogout();
060            onLogout(loggedOutAuth);
061    
062            if (displaySuccess) {
063                JOptionPane.showMessageDialog(getParentWindowControl(), "You have been logged out.", "Logout Successful",
064                        JOptionPane.INFORMATION_MESSAGE);
065            }
066        }
067    
068        /**
069         * Can be extended by subclasses to perform additional logout processing,
070         * such as notifying a server etc.
071         */
072        public void onLogout(Authentication loggedOut) {
073    
074        }
075    
076    }