1 26 27 package com.rift.coad.lib.security.user; 29 30 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.StringTokenizer ; 34 import java.util.Vector ; 35 import java.util.Iterator ; 36 37 import org.apache.log4j.Logger; 39 40 import com.rift.coad.lib.configuration.ConfigurationFactory; 42 import com.rift.coad.lib.configuration.Configuration; 43 import com.rift.coad.lib.security.UserSession; 44 45 53 public class UserStoreManager { 54 55 private final static String USER_STORE_CONNECTORS = "connectors"; 57 58 private Logger log = 60 Logger.getLogger(UserStoreManager.class.getName()); 61 private Map connectors = null; 62 63 64 69 public UserStoreManager() throws UserException { 70 try { 71 Configuration config = ConfigurationFactory.getInstance().getConfig( 72 this.getClass()); 73 StringTokenizer connectorList = new StringTokenizer ( 74 config.getString(USER_STORE_CONNECTORS),","); 75 connectors = new HashMap (); 76 while(connectorList.hasMoreTokens()) { 77 String connectorName = connectorList.nextToken(); 78 try { 79 log.info("Load the connector [" + 80 connectorName + "]"); 81 UserStoreConnector connector = (UserStoreConnector)Class. 82 forName(connectorName).newInstance(); 83 connectors.put(connector.getName(),connector); 84 } catch (Exception ex) { 85 log.error("Failed to load the connector [" + 86 connectorName + "]",ex); 87 } 88 } 89 } catch (Exception ex) { 90 throw new UserException("Failed to load the user stores : " 91 + ex.getMessage(),ex); 92 } 93 } 94 95 96 103 public Vector getLoginHandlers(String authType) throws UserException { 104 try { 105 Vector handlerList = new Vector (); 106 for (Iterator iter = connectors.keySet().iterator(); 107 iter.hasNext();) { 108 UserStoreConnector connector = (UserStoreConnector)connectors. 109 get(iter.next()); 110 if (connector.handleAuthType(authType)) { 111 handlerList.add(connector.getLoginHandler(authType)); 112 } 113 } 114 return handlerList; 115 } catch (Exception ex) { 116 throw new UserException("Fail to retrieve the handlers : " 117 + ex.getMessage(),ex); 118 } 119 } 120 121 122 129 public UserSession getUserInfo(String username) throws UserException { 130 try { 131 for (Iterator iter = connectors.keySet().iterator(); 132 iter.hasNext();) { 133 UserStoreConnector connector = (UserStoreConnector)connectors. 134 get(iter.next()); 135 UserSession userInfo = connector.getUserInfo(username); 136 if (userInfo != null) { 137 return userInfo; 138 } 139 } 140 throw new UserException("Failed to retrieve the user info for [" + 141 username + "]"); 142 } catch (Exception ex) { 143 throw new UserException("Fail to retrieve the handlers : " 144 + ex.getMessage(),ex); 145 } 146 } 147 } 148 | Popular Tags |