1 23 24 package com.rift.coad.lib.security.login; 26 27 import java.util.Vector ; 29 30 import org.apache.log4j.Logger; 32 33 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 45 public class LoginManager { 46 47 private final static String SESSION_TIMEOUT = "session_timeout"; 49 50 private static LoginManager singleton = null; 52 53 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 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 ex) { 76 throw new LoginException( 77 "Failed to instanciate the login manager : " + 78 ex.getMessage(),ex); 79 } 80 } 81 82 83 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 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 120 protected UserSession authenticate(LoginInfoHandler infoHandler) 121 throws LoginException { 122 try { 123 Vector 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 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 ex) { 140 throw new LoginException("Failed to authenticate the user : " + 141 ex.getMessage(),ex); 142 } 143 } 144 } 145 | Popular Tags |