001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Emmanuel Cecchet.
020: * Contributor(s): Mathieu Peltier.
021: */package org.continuent.sequoia.common.authentication;
022:
023: import java.util.ArrayList;
024: import java.util.HashMap;
025: import java.util.Iterator;
026:
027: import org.continuent.sequoia.common.i18n.Translate;
028: import org.continuent.sequoia.common.users.AdminUser;
029: import org.continuent.sequoia.common.users.DatabaseBackendUser;
030: import org.continuent.sequoia.common.users.VirtualDatabaseUser;
031: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
032:
033: /**
034: * The <code>AuthenticationManager</code> manages the mapping between virtual
035: * login/password (to the <code>VirtualDatabase</code>) and the real
036: * login/password for each backend.
037: *
038: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
039: * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
040: * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
041: * @version 1.0
042: */
043: public class AuthenticationManager {
044: /*
045: * How the code is organized ? 1. Member variables 2. Constructor(s) 3.
046: * Login/Password checking functions 4. Getter/Setter (possibly in
047: * alphabetical order) 5. Xml
048: */
049:
050: /** <code>ArrayList</code> of <code>VirtualDatabaseUser</code> objects. */
051: private ArrayList virtualLogins;
052:
053: /** <code>ArrayList</code> of <code>AdminUser</code> objects. */
054: private ArrayList adminUsers;
055:
056: /**
057: * <code>HashMap</code> of <code>HashMap</code> of
058: * <code>DatabaseBackendUser</code> objects hashed by the backend name,
059: * hashed by their virtual database login. A virtual user can have several
060: * real logins, but has only one real login for a given backend.
061: */
062: private HashMap realLogins;
063:
064: /** Controls whether the transparent login feature is enabled. */
065: private boolean transparentLogin;
066:
067: /*
068: * Constructor(s)
069: */
070:
071: /**
072: * Creates a new <code>AuthenticationManager</code> instance.
073: */
074: public AuthenticationManager() {
075: this (true);
076: }
077:
078: /**
079: * Creates a new <code>AuthenticationManager</code> instance.
080: *
081: * @param transparentLogin enable/disable the transparent login feature.
082: */
083: public AuthenticationManager(boolean transparentLogin) {
084: virtualLogins = new ArrayList();
085: adminUsers = new ArrayList();
086: realLogins = new HashMap();
087: this .transparentLogin = transparentLogin;
088: }
089:
090: /*
091: * Login/Password checking functions
092: */
093:
094: /**
095: * Checks whether this administrator user has been registered to this
096: * <code>AuthenticationManager</code> or not. Returns <code>false</code>
097: * if no admin user has been set.
098: *
099: * @param user administrator user login/password to check.
100: * @return <code>true</code> if it matches the registered admin user.
101: */
102: public boolean isValidAdminUser(AdminUser user) {
103: return adminUsers.contains(user);
104: }
105:
106: /**
107: * Checks whether a given virtual database user has been registered to this
108: * <code>AuthenticationManager</code> or not.
109: *
110: * @param vUser the virtual database user.
111: * @return <code>true</code> if the user login/password is valid.
112: */
113: public boolean isValidVirtualUser(VirtualDatabaseUser vUser) {
114: return virtualLogins.contains(vUser);
115: }
116:
117: /**
118: * Checks whether a given virtual login has been registered to this
119: * <code>AuthenticationManager</code> or not.
120: *
121: * @param vLogin the virtual database login.
122: * @return <code>true</code> if the virtual database login is valid.
123: */
124: public boolean isValidVirtualLogin(String vLogin) {
125: Iterator iter = virtualLogins.iterator();
126: VirtualDatabaseUser u;
127: while (iter.hasNext()) {
128: u = (VirtualDatabaseUser) iter.next();
129: if (u.getLogin().equals(vLogin)) {
130: return true;
131: }
132: }
133: return false;
134: }
135:
136: /**
137: * Checks whether transparent login feature is enabled.
138: *
139: * @return true if enabled, false otherwise.
140: */
141: public boolean isTransparentLoginEnabled() {
142: return transparentLogin;
143: }
144:
145: /**
146: * Sets the administrator user.
147: *
148: * @param adminUser the administor user to set.
149: */
150: // public void setAdminUser(VirtualDatabaseUser adminUser)
151: // {
152: // this.adminUser = adminUser;
153: // }
154: /**
155: * Registers a new virtual database user.
156: *
157: * @param vUser the <code>VirtualDatabaseUser</code> to register.
158: */
159: public synchronized void addVirtualUser(VirtualDatabaseUser vUser) {
160: virtualLogins.add(vUser);
161: }
162:
163: /**
164: * Unregisters a new virtual database user.
165: *
166: * @param vUser the <code>VirtualDatabaseUser</code> to unregister.
167: */
168: public void removeVirtualUser(VirtualDatabaseUser vUser) {
169: virtualLogins.remove(vUser);
170: }
171:
172: /**
173: * Associates a new database backend user to a virtual database login.
174: *
175: * @param vLogin the virtual database login.
176: * @param rUser the database backend user to add.
177: * @exception AuthenticationManagerException if a real user already exists for
178: * this backend.
179: */
180: public void addRealUser(String vLogin, DatabaseBackendUser rUser)
181: throws AuthenticationManagerException {
182: HashMap list = (HashMap) realLogins.get(vLogin);
183: if (list == null) {
184: list = new HashMap();
185: list.put(rUser.getBackendName(), rUser);
186: realLogins.put(vLogin, list);
187: } else {
188: DatabaseBackendUser u = (DatabaseBackendUser) list
189: .get(rUser.getBackendName());
190: if (u != null)
191: throw new AuthenticationManagerException(
192: Translate
193: .get(
194: "authentication.failed.add.user.already.exists",
195: new String[] {
196: rUser.getLogin(),
197: vLogin,
198: rUser.getBackendName(),
199: u.getLogin() }));
200: list.put(rUser.getBackendName(), rUser);
201: }
202: }
203:
204: /**
205: * Gets the <code>DatabaseBackendUser</code> given a virtual database login
206: * and a database backend logical name.
207: *
208: * @param vLogin virtual database login.
209: * @param backendName database backend logical name.
210: * @return a <code>DatabaseBackendUser</code> value or <code>null</code>
211: * if not found.
212: */
213: public DatabaseBackendUser getDatabaseBackendUser(String vLogin,
214: String backendName) {
215: Object list = realLogins.get(vLogin);
216: if (list == null)
217: return null;
218: else
219: return (DatabaseBackendUser) ((HashMap) list)
220: .get(backendName);
221: }
222:
223: /**
224: * @return Returns the realLogins.
225: */
226: public HashMap getRealLogins() {
227: return realLogins;
228: }
229:
230: /**
231: * @return Returns the virtualLogins.
232: */
233: public ArrayList getVirtualLogins() {
234: return virtualLogins;
235: }
236:
237: /**
238: * Return the virtual password corresponding to the given virtual login
239: *
240: * @param vLogin the virtual login
241: * @return the virtual password if found, null otherwise
242: */
243: public String getVirtualPassword(String vLogin) {
244: Iterator iter = virtualLogins.iterator();
245: VirtualDatabaseUser u;
246: while (iter.hasNext()) {
247: u = (VirtualDatabaseUser) iter.next();
248: if (u.getLogin().equals(vLogin)) {
249: return u.getPassword();
250: }
251: }
252: return null;
253: }
254:
255: /*
256: * 5. Xml
257: */
258: /**
259: * Format to xml
260: *
261: * @return xml formatted representation
262: */
263: public String getXml() {
264: StringBuffer info = new StringBuffer();
265: info.append("<" + DatabasesXmlTags.ELT_AuthenticationManager
266: + ">");
267: info.append("<" + DatabasesXmlTags.ELT_Admin + ">");
268: for (int i = 0; i < adminUsers.size(); i++) {
269: AdminUser vu = (AdminUser) adminUsers.get(i);
270: info.append(vu.getXml());
271: }
272: info.append("</" + DatabasesXmlTags.ELT_Admin + ">");
273:
274: info.append("<" + DatabasesXmlTags.ELT_VirtualUsers + ">");
275: for (int i = 0; i < virtualLogins.size(); i++) {
276: VirtualDatabaseUser vu = (VirtualDatabaseUser) virtualLogins
277: .get(i);
278: info.append(vu.getXml());
279: }
280: info.append("</" + DatabasesXmlTags.ELT_VirtualUsers + ">");
281: info.append("</" + DatabasesXmlTags.ELT_AuthenticationManager
282: + ">");
283: return info.toString();
284: }
285:
286: /**
287: * Add an admin user for this authentication manager.
288: *
289: * @param user the <code>AdminUser</code> to add to this
290: * <code>AuthenticationManager</code>
291: */
292: public void addAdminUser(AdminUser user) {
293: adminUsers.add(user);
294: }
295:
296: /**
297: * Remove an admin user from the admin list
298: *
299: * @param user the admin to remove
300: * @return <code>true</code> if was removed.
301: */
302: public boolean removeAdminUser(AdminUser user) {
303: return adminUsers.remove(user);
304: }
305:
306: /**
307: * @return Returns the adminUsers.
308: */
309: public ArrayList getAdminUsers() {
310: return adminUsers;
311: }
312:
313: }
|