KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > auth > LoginService


1 package org.jdesktop.swing.auth;
2
3 /*
4  * $Id: LoginService.java,v 1.2 2005/01/28 01:35:06 bino_george Exp $
5  *
6  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
7  * Santa Clara, California 95054, U.S.A. All rights reserved.
8  */

9
10
11 import java.util.*;
12 import java.awt.EventQueue JavaDoc;
13 /**
14  * <b>LoginService</b> is the abstract base class for all classes implementing
15  * a login mechanism. It allows you to customize the threading behaviour
16  * used to perform the login. Subclasses need to override the <b>authenticate</b>
17  * method.
18  *
19  * @author Bino George
20  */

21 public abstract class LoginService {
22     
23      private Vector<LoginListener> listenerList = new Vector<LoginListener>();
24
25      private Thread JavaDoc loginThread;
26     
27      /*
28       * Controls the authentication behaviour to be either
29       * synchronous or asynchronous
30       */

31      private boolean synchronous;
32      private boolean canceled;
33      private String JavaDoc server;
34      
35      
36      public LoginService(String JavaDoc serverName) {
37         setServer(serverName);
38      }
39      
40     /**
41      * This method is intended to be implemented by clients
42      * wishing to authenticate a user with a given password.
43      * Clients should implement the authentication in a
44      * manner that the authentication can be cancelled at
45      * any time.
46      *
47      * @param name username
48      * @param password password
49      * @param server server (optional)
50      */

51     public abstract boolean authenticate(String JavaDoc name, char[] password, String JavaDoc server);
52     
53     /**
54      * Notifies the LoginService that an already running
55      * authentication request should be cancelled. This
56      * method is intended to be used by clients who want
57      * to provide user with control over cancelling a long
58      * running authentication request.
59      */

60     public void cancelAuthentication() {
61         canceled = true;
62         EventQueue.invokeLater(new Runnable JavaDoc() {
63             public void run() {
64                 fireLoginCanceled(new EventObject(this));
65             }
66          });
67     }
68   
69     /**
70      * This method is intended to be overridden by subclasses
71      * to customize the threading to use pooling etc. The default
72      * implementation just creates a new Thread with the given runnable.
73      *
74      * @param runnable runnable
75      */

76     public Thread JavaDoc getLoginThread(Runnable JavaDoc runnable) {
77       return new Thread JavaDoc(runnable);
78     }
79     
80     /**
81      * This method starts the authentication process and is either
82      * synchronous or asynchronous based on the synchronous property
83      *
84      * @param user user
85      * @param password password
86      * @param server server
87      */

88     public void startAuthentication(final String JavaDoc user, final char[] password, final String JavaDoc server) {
89        canceled = false;
90        if (getSynchronous()) {
91          if (authenticate(user,password,server)) {
92            fireLoginSucceeded(new EventObject(this));
93          } else {
94            fireLoginFailed(new EventObject(this));
95          }
96        } else {
97          Runnable JavaDoc runnable = new Runnable JavaDoc() {
98            public void run() {
99              final boolean result = authenticate(user,password,server);
100              if (!canceled) {
101              EventQueue.invokeLater(new Runnable JavaDoc() {
102                 public void run() {
103                    if (result) {
104                       fireLoginSucceeded(new EventObject(this));
105                    }
106                    else {
107                       fireLoginFailed(new EventObject(this));
108                    }
109                 }
110              });
111              }
112            }
113          };
114          loginThread = getLoginThread(runnable) ;
115          loginThread.start();
116        }
117     }
118     
119     /**
120      * Returns the synchronous property
121      */

122     public boolean getSynchronous() {
123         return synchronous;
124     }
125     /**
126      * Sets the synchronous property
127      *
128      * @param synchronous synchronous property
129      */

130     public void setSynchronous(boolean synchronous) {
131         this.synchronous = synchronous;
132     }
133     
134     /**
135      * Adds a <strong>LoginListener</strong> to the list of listeners
136      *
137      * @param listener listener
138      */

139     
140     public void addLoginListener(LoginListener listener) {
141         listenerList.add(listener);
142     }
143     
144     /**
145      * Removes a <strong>LoginListener</strong> from the list of listeners
146      *
147      * @param listener listener
148      */

149     public void removeLoginListener(LoginListener listener) {
150         listenerList.remove(listener);
151     }
152     
153     
154     void fireLoginSucceeded(final EventObject source) {
155         Iterator iter = listenerList.iterator();
156         while (iter.hasNext()) {
157             LoginListener listener = (LoginListener) iter.next();
158             listener.loginSucceeded(source);
159         }
160     }
161     
162     void fireLoginFailed(final EventObject source) {
163         Iterator iter = listenerList.iterator();
164         while (iter.hasNext()) {
165             LoginListener listener = (LoginListener) iter.next();
166             listener.loginFailed(source);
167         }
168     }
169     
170     void fireLoginCanceled(final EventObject source) {
171         Iterator iter = listenerList.iterator();
172         while (iter.hasNext()) {
173             LoginListener listener = (LoginListener) iter.next();
174             listener.loginCanceled(source);
175         }
176     }
177  
178     
179     /**
180      * @return Returns the server.
181      */

182     public String JavaDoc getServer() {
183         return server;
184     }
185     /**
186      * @param server The server to set.
187      */

188     public void setServer(String JavaDoc server) {
189         this.server = server;
190     }
191 }
192
Popular Tags