KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > security > user > UserStoreManager


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  * UserStoreManager.java
20  *
21  * This object is responsible for managing access to the user store objects. It
22  * will not run as a singleton. This will prevent un-wanted threads and objects
23  * from accessing it directly. This means it has to be initialized intentionally
24  * and at the correct place.
25  */

26
27 // package path
28
package com.rift.coad.lib.security.user;
29
30 // java imports
31
import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.Iterator JavaDoc;
36
37 // logging import
38
import org.apache.log4j.Logger;
39
40 // coadunation imports
41
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 /**
46  * This object is responsible for managing access to the user store objects. It
47  * will not run as a singleton. This will prevent un-wanted threads and objects
48  * from accessing it directly. This means it has to be initialized intentionally
49  * and at the correct place.
50  *
51  * @author Brett Chaldecott
52  */

53 public class UserStoreManager {
54     
55     // the class constants
56
private final static String JavaDoc USER_STORE_CONNECTORS = "connectors";
57     
58     // the classes private member variables
59
private Logger log =
60         Logger.getLogger(UserStoreManager.class.getName());
61     private Map JavaDoc connectors = null;
62     
63     
64     /**
65      * Creates a new instance of UserStoreManager.
66      *
67      * @exception UserException
68      */

69     public UserStoreManager() throws UserException {
70         try {
71             Configuration config = ConfigurationFactory.getInstance().getConfig(
72                     this.getClass());
73             StringTokenizer JavaDoc connectorList = new StringTokenizer JavaDoc(
74                     config.getString(USER_STORE_CONNECTORS),",");
75             connectors = new HashMap JavaDoc();
76             while(connectorList.hasMoreTokens()) {
77                 String JavaDoc 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 JavaDoc ex) {
85                     log.error("Failed to load the connector [" +
86                          connectorName + "]",ex);
87                 }
88             }
89         } catch (Exception JavaDoc ex) {
90             throw new UserException("Failed to load the user stores : "
91                     + ex.getMessage(),ex);
92         }
93     }
94     
95     
96     /**
97      * This method will return the list of handlers for the given auth type.
98      *
99      * @return The list of handlers for the given authentication type.
100      * @param authType The type of authentication that is required.
101      * @exception UserException
102      */

103     public Vector JavaDoc getLoginHandlers(String JavaDoc authType) throws UserException {
104         try {
105             Vector JavaDoc handlerList = new Vector JavaDoc();
106             for (Iterator JavaDoc 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 JavaDoc ex) {
116             throw new UserException("Fail to retrieve the handlers : "
117                     + ex.getMessage(),ex);
118         }
119     }
120     
121     
122     /**
123      * This method will return the user information for the requested user name.
124      *
125      * @return The user object containing the user information
126      * @param username The name of the user.
127      * @exception UserException
128      */

129     public UserSession getUserInfo(String JavaDoc username) throws UserException {
130         try {
131             for (Iterator JavaDoc 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 JavaDoc ex) {
143             throw new UserException("Fail to retrieve the handlers : "
144                     + ex.getMessage(),ex);
145         }
146     }
147 }
148
Popular Tags