KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > login > LoginManager


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  * LoginManager.java
20  *
21  * This class is responsible for managing the login requests initiated by users.
22  */

23
24 // the package path
25
package com.rift.coad.lib.security.login;
26
27 // java imports
28
import java.util.Vector JavaDoc;
29
30 // logging import
31
import org.apache.log4j.Logger;
32
33 // coadunation imports
34
import com.rift.coad.lib.configuration.Configuration;
35 import com.rift.coad.lib.configuration.ConfigurationFactory;
36 import com.rift.coad.lib.security.UserSession;
37 import com.rift.coad.lib.security.user.UserSessionManager;
38 import com.rift.coad.lib.security.user.UserStoreManager;
39
40 /**
41  * This class is responsible for managing the login requests initiated by users.
42  *
43  * @author Brett Chaldecott
44  */

45 public class LoginManager {
46     
47     // class constants
48
private final static String JavaDoc SESSION_TIMEOUT = "session_timeout";
49     
50     // singleton variables
51
private static LoginManager singleton = null;
52     
53     // private member variables
54
private Logger log =
55             Logger.getLogger(LoginManager.class.getName());
56     private Configuration config = null;
57     private UserSessionManager sessionManager = null;
58     private UserStoreManager userStoreManager = null;
59     private long sessionTimeout = 0;
60     
61     /**
62      * Creates a new instance of LoginManager
63      *
64      * @param sessionManager The reference to the session manager.
65      * @param userStoreManager The reference to the user store manager object.
66      */

67     private LoginManager(UserSessionManager sessionManager,
68             UserStoreManager userStoreManager) throws LoginException {
69         try {
70             config = ConfigurationFactory.getInstance().getConfig(
71                     this.getClass());
72             this.sessionManager = sessionManager;
73             this.userStoreManager = userStoreManager;
74             sessionTimeout = config.getLong(SESSION_TIMEOUT);
75         } catch (Exception JavaDoc ex) {
76             throw new LoginException(
77                     "Failed to instanciate the login manager : " +
78                     ex.getMessage(),ex);
79         }
80     }
81     
82     
83     /**
84      * This method will instanicate a new login manager.
85      *
86      * @param sessionManager A reference to the user session manager.
87      * @param userStoreManager The user store manager information.
88      * @exception LoginException
89      */

90     public synchronized static void init(UserSessionManager sessionManager,
91             UserStoreManager userStoreManager) throws LoginException {
92         if (singleton == null) {
93             singleton = new LoginManager(sessionManager,userStoreManager);
94         }
95     }
96     
97     
98     /**
99      * This method retrieve a reference to the Login manager singleton.
100      *
101      * @return The reference to the login manager.
102      * @exception LoginException
103      */

104     public synchronized static LoginManager getInstance() throws LoginException {
105         if (singleton == null) {
106             throw new LoginException(
107                     "The Login Manager has not been initialized");
108         }
109         return singleton;
110     }
111     
112     
113     /**
114      * This method will perform the authentication request on behalf of a user.
115      *
116      * @return A reference to the user session.
117      * @param infoHandler The information necessary for the login request.
118      * @exception LoginException
119      */

120     protected UserSession authenticate(LoginInfoHandler infoHandler)
121             throws LoginException {
122         try {
123             Vector JavaDoc handlerList = userStoreManager.getLoginHandlers(
124                     infoHandler.getAuthType());
125             for (int i = 0; i < handlerList.size(); i++) {
126                 LoginHandler loginHandler = (LoginHandler)handlerList.get(i);
127                 if (loginHandler.login(infoHandler)) {
128                     UserSession userSession = loginHandler.getUserInfo();
129                     userSession.setExpiryTime(sessionTimeout);
130                     // We add the user session and do not init it
131
// as the init method over rides the current thread session.
132
sessionManager.addUserSession(userSession);
133                     return userSession;
134                 }
135             }
136             throw new AuthenticationException("Failed to authenticate user");
137         } catch (AuthenticationException ex) {
138             throw ex;
139         } catch (Exception JavaDoc ex) {
140             throw new LoginException("Failed to authenticate the user : " +
141                     ex.getMessage(),ex);
142         }
143     }
144 }
145
Popular Tags