KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > user > xml > XMLLoginHandler


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLLoginHandler.java
20  *
21  * This class is responsible for authenticating a login request against the
22  * XML user information.
23  */

24
25 // the package path
26
package com.rift.coad.lib.security.user.xml;
27
28 // java imports
29
import java.util.Map JavaDoc;
30
31 // logging import
32
import org.apache.log4j.Logger;
33
34 // coadunation imports
35
import com.rift.coad.lib.security.login.AuthValues;
36 import com.rift.coad.lib.security.login.LoginHandler;
37 import com.rift.coad.lib.security.login.LoginException;
38 import com.rift.coad.lib.security.login.LoginInfoHandler;
39 import com.rift.coad.lib.security.UserSession;
40
41 /**
42  * This class is responsible for authenticating a login request against the
43  * XML user information.
44  *
45  * @author Brett Chaldecott
46  */

47 public class XMLLoginHandler implements LoginHandler {
48     
49     // the classes private member variables
50
private Logger log =
51             Logger.getLogger(XMLLoginHandler.class.getName());
52     private Map JavaDoc users = null;
53     private UserSession user = null;
54     
55     /**
56      * Creates a new instance of XMLLoginHandler
57      */

58     public XMLLoginHandler(Map JavaDoc users) {
59         this.users = users;
60     }
61     
62     /**
63      * This method returns a reference to the login manager the interface that
64      * this object was retrieved from.
65      *
66      * @return A reference to the login manager.
67      * @exception LoginException
68      */

69     public UserSession getUserInfo() throws LoginException {
70         if (user == null) {
71             throw new LoginException(
72                     "No valid login request has been processed by this object.");
73         }
74         return user;
75     }
76     
77     
78     /**
79      * This method preforms the login for a given user using the login
80      * information supplied to that user.
81      *
82      * @return TRUE if the login succeeded, FALSE if not.
83      * @param loginInfoHandler The handler containing the login information.
84      * @exception LoginException
85      */

86     public boolean login(LoginInfoHandler loginInfoHandler)
87     throws LoginException {
88         try {
89             Map JavaDoc parameters = loginInfoHandler.getInfo();
90             String JavaDoc username = (String JavaDoc)parameters.get(AuthValues.USERNAME);
91             if (username == null) {
92                 throw new LoginException(
93                         "The login information handler has not supplied the " +
94                         "username");
95             }
96             UserData userData = (UserData)users.get(username);
97             if (userData == null) {
98                 log.debug("Username [" + username + "] was not found.");
99                 return false;
100             }
101             String JavaDoc password = (String JavaDoc)parameters.get(AuthValues.PASSWORD);
102             if (userData.getPassword().equals(password) == false) {
103                 return false;
104             }
105             user = userData.getUser();
106             return true;
107         } catch (Exception JavaDoc ex) {
108             throw new LoginException("Failed to log the user in because : " +
109                     ex.getMessage(),ex);
110         }
111     }
112 }
113
Popular Tags