LoginService.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:8k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LoginService.java,v 1.5 2005/10/10 18:02:51 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.auth;
  22. import java.io.IOException;
  23. import java.util.*;
  24. import java.awt.EventQueue;
  25. import javax.swing.SwingUtilities;
  26. /**
  27.  * <b>LoginService</b> is the abstract base class for all classes implementing
  28.  * a login mechanism. It allows you to customize the threading behaviour
  29.  * used to perform the login. Subclasses need to override the <b>authenticate</b>
  30.  * method.
  31.  * Subclasses may implement the getUserRoles() method to return a meaningful value
  32.  * this method will be called once upon a successful login to determine the user roles.
  33.  * It is not defined as abstract to simplify the task of implementing a login service
  34.  * for those who do not require this functionality.
  35.  *
  36.  * @author Bino George
  37.  * @author Shai Almog
  38.  */
  39. public abstract class LoginService {
  40.      private Vector<LoginListener> listenerList = new Vector<LoginListener>();
  41.      private Thread loginThread;
  42.     
  43.      /*
  44.       * Controls the authentication behaviour to be either
  45.       * synchronous or asynchronous
  46.       */
  47.      private boolean synchronous;
  48.      private boolean canceled;
  49.      private String server;
  50.      
  51.      public LoginService() {
  52.      }
  53.      
  54.      public LoginService(String server) {
  55.          setServer(server);
  56.      }
  57.      
  58.     /**
  59.      * This method is intended to be implemented by clients
  60.      * wishing to authenticate a user with a given password.
  61.      * Clients should implement the authentication in a 
  62.      * manner that the authentication can be cancelled at
  63.      * any time.
  64.      *
  65.      * @param name username
  66.      * @param password password
  67.      * @param server server (optional)
  68.      * 
  69.      * @return <code>true</code> on authentication success
  70.      * @throws IOException
  71.      */
  72.     public abstract boolean authenticate(String name, char[] password, String server) throws IOException;
  73.     
  74.     /**
  75.      * Called immediately after a successful authentication. This method should return an array
  76.      * of user roles or null if role based permissions are not used.
  77.      * 
  78.      * @return per default <code>null</code> 
  79.      */
  80.     public String[] getUserRoles() {
  81.         return null;
  82.     }
  83.     
  84.     /**
  85.      * Notifies the LoginService that an already running
  86.      * authentication request should be cancelled. This
  87.      * method is intended to be used by clients who want
  88.      * to provide user with control over cancelling a long
  89.      * running authentication request.
  90.      */
  91.     public void cancelAuthentication() {
  92.      canceled = true;
  93.      EventQueue.invokeLater(new Runnable() {
  94.             public void run() {
  95.                 fireLoginCanceled(new LoginEvent(this)); 
  96.             } 
  97.          });
  98.     }
  99.   
  100.     /**
  101.      * This method is intended to be overridden by subclasses
  102.      * to customize the threading to use pooling etc. The default
  103.      * implementation just creates a new Thread with the given runnable.
  104.      *
  105.      * @param runnable runnable
  106.      * @return a new login thread
  107.      */
  108.     public Thread getLoginThread(Runnable runnable) {
  109.       return new Thread(runnable);  
  110.     }
  111.     
  112.     /**
  113.      * This method starts the authentication process and is either
  114.      * synchronous or asynchronous based on the synchronous property
  115.      * 
  116.      * @param user user
  117.      * @param password password
  118.      * @param server server
  119.      * @throws IOException
  120.      */
  121.     public void startAuthentication(final String user, final char[] password, final String server) throws IOException {
  122.        canceled = false;
  123.        if (getSynchronous()) {
  124.          if (authenticate(user,password,server)) {
  125.            fireLoginSucceeded(new LoginEvent(this));  
  126.          } else {
  127.            fireLoginFailed(new LoginEvent(this));  
  128.          }  
  129.        } else {
  130.          Runnable runnable = new Runnable() {
  131.            public void run() {
  132.              try {
  133.                  final boolean result = authenticate(user,password,server);
  134.                  if (!canceled) {
  135.                      EventQueue.invokeLater(new Runnable() {
  136.                         public void run() {
  137.                            if (result) {
  138.                               fireLoginSucceeded(new LoginEvent(LoginService.this));  
  139.                            }
  140.                            else {
  141.                               fireLoginFailed(new LoginEvent(LoginService.this)); 
  142.                            }
  143.                         } 
  144.                      });
  145.                  }
  146.              } catch(final IOException failed) {
  147.                  SwingUtilities.invokeLater(new Runnable() {
  148.                      public void run() {
  149.                          fireLoginFailed(new LoginEvent(LoginService.this, failed));
  150.                      }
  151.                  });
  152.              }
  153.            }  
  154.          };  
  155.          loginThread = getLoginThread(runnable) ; 
  156.          loginThread.start();
  157.          fireLoginStarted(new LoginEvent(this));
  158.        }
  159.     }
  160.     
  161.     /**
  162.      * Get the synchronous property
  163.      * @return the synchronous property
  164.      */
  165.     public boolean getSynchronous() {
  166.         return synchronous;
  167.     }
  168.     /**
  169.      * Sets the synchronous property
  170.      * 
  171.      * @param synchronous synchronous property
  172.      */
  173.     public void setSynchronous(boolean synchronous) {
  174.         this.synchronous = synchronous;
  175.     }
  176.     
  177.     /**
  178.      * Adds a <strong>LoginListener</strong> to the list of listeners
  179.      *
  180.      * @param listener listener
  181.      */
  182.     
  183.     public void addLoginListener(LoginListener listener) {
  184.         listenerList.add(listener);
  185.     }
  186.     
  187.     /**
  188.      * Removes a <strong>LoginListener</strong> from the list of listeners
  189.      *
  190.      * @param listener listener
  191.      */
  192.     public void removeLoginListener(LoginListener listener) {
  193.         listenerList.remove(listener);
  194.     }
  195.     
  196.     
  197.     void fireLoginStarted(final LoginEvent source) {
  198.         Iterator iter = listenerList.iterator();
  199.         while (iter.hasNext()) {
  200.             LoginListener listener = (LoginListener) iter.next();
  201.             listener.loginStarted(source);
  202.         }
  203.     }
  204.     
  205.     void fireLoginSucceeded(final LoginEvent source) {
  206.         Iterator iter = listenerList.iterator();
  207.         while (iter.hasNext()) {
  208.             LoginListener listener = (LoginListener) iter.next();
  209.             listener.loginSucceeded(source);
  210.         }
  211.     }
  212.     
  213.     void fireLoginFailed(final LoginEvent source) {
  214.         Iterator iter = listenerList.iterator();
  215.         while (iter.hasNext()) {
  216.             LoginListener listener = (LoginListener) iter.next();
  217.             listener.loginFailed(source);
  218.         }
  219.     }
  220.     
  221.     void fireLoginCanceled(final LoginEvent source) {
  222.         Iterator iter = listenerList.iterator();
  223.         while (iter.hasNext()) {
  224.             LoginListener listener = (LoginListener) iter.next();
  225.             listener.loginCanceled(source);
  226.         }
  227.     }
  228.  
  229.     
  230. /**
  231.  * @return Returns the server.
  232.  */
  233. public String getServer() {
  234. return server;
  235. }
  236. /**
  237.  * @param server The server to set.
  238.  */
  239. public void setServer(String server) {
  240. this.server = server;
  241. }
  242. }