KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > authentication > AuthenticationManager


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Emmanuel Cecchet.
20  * Contributor(s): Mathieu Peltier.
21  */

22
23 package org.continuent.sequoia.controller.authentication;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.continuent.sequoia.common.i18n.Translate;
30 import org.continuent.sequoia.common.users.AdminUser;
31 import org.continuent.sequoia.common.users.DatabaseBackendUser;
32 import org.continuent.sequoia.common.users.VirtualDatabaseUser;
33 import org.continuent.sequoia.common.xml.DatabasesXmlTags;
34
35 /**
36  * The <code>AuthenticationManager</code> manages the mapping between virtual
37  * login/password (to the <code>VirtualDatabase</code>) and the real
38  * login/password for each backend.
39  *
40  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
41  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
42  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
43  * @version 1.0
44  */

45 public class AuthenticationManager
46 {
47   /*
48    * How the code is organized ? 1. Member variables 2. Constructor(s) 3.
49    * Login/Password checking functions 4. Getter/Setter (possibly in
50    * alphabetical order) 5. Xml
51    */

52
53   /** <code>ArrayList</code> of <code>VirtualDatabaseUser</code> objects. */
54   private ArrayList JavaDoc virtualLogins;
55
56   /** <code>ArrayList</code> of <code>AdminUser</code> objects. */
57   private ArrayList JavaDoc adminUsers;
58
59   /**
60    * <code>HashMap</code> of <code>HashMap</code> of
61    * <code>DatabaseBackendUser</code> objects hashed by the backend name,
62    * hashed by their virtual database login. A virtual user can have several
63    * real logins, but has only one real login for a given backend.
64    */

65   private HashMap JavaDoc realLogins;
66
67   /** Controls whether the transparent login feature is enabled. */
68   private boolean transparentLogin;
69
70   /*
71    * Constructor(s)
72    */

73
74   /**
75    * Creates a new <code>AuthenticationManager</code> instance.
76    */

77   public AuthenticationManager()
78   {
79     this(true);
80   }
81
82   /**
83    * Creates a new <code>AuthenticationManager</code> instance.
84    *
85    * @param transparentLogin enable/disable the transparent login feature.
86    */

87   public AuthenticationManager(boolean transparentLogin)
88   {
89     virtualLogins = new ArrayList JavaDoc();
90     adminUsers = new ArrayList JavaDoc();
91     realLogins = new HashMap JavaDoc();
92     this.transparentLogin = transparentLogin;
93   }
94
95   /*
96    * Login/Password checking functions
97    */

98
99   /**
100    * Checks whether this administrator user has been registered to this
101    * <code>AuthenticationManager</code> or not. Returns <code>false</code>
102    * if no admin user has been set.
103    *
104    * @param user administrator user login/password to check.
105    * @return <code>true</code> if it matches the registered admin user.
106    */

107   public boolean isValidAdminUser(AdminUser user)
108   {
109     synchronized (adminUsers)
110     {
111       return adminUsers.contains(user);
112     }
113   }
114
115   /**
116    * Checks whether a given virtual database user has been registered to this
117    * <code>AuthenticationManager</code> or not.
118    *
119    * @param vUser the virtual database user.
120    * @return <code>true</code> if the user login/password is valid.
121    */

122   public boolean isValidVirtualUser(VirtualDatabaseUser vUser)
123   {
124     synchronized (virtualLogins)
125     {
126       return virtualLogins.contains(vUser);
127     }
128   }
129
130   /**
131    * Checks whether a given virtual login has been registered to this
132    * <code>AuthenticationManager</code> or not.
133    *
134    * @param vLogin the virtual database login.
135    * @return <code>true</code> if the virtual database login is valid.
136    */

137   public boolean isValidVirtualLogin(String JavaDoc vLogin)
138   {
139     Iterator JavaDoc iter = virtualLogins.iterator();
140     VirtualDatabaseUser u;
141     while (iter.hasNext())
142     {
143       u = (VirtualDatabaseUser) iter.next();
144       if (u.getLogin().equals(vLogin))
145       {
146         return true;
147       }
148     }
149     return false;
150   }
151
152   /**
153    * Checks whether transparent login feature is enabled.
154    *
155    * @return true if enabled, false otherwise.
156    */

157   public boolean isTransparentLoginEnabled()
158   {
159     return transparentLogin;
160   }
161
162   /**
163    * Add an admin user for this authentication manager.
164    *
165    * @param user the <code>AdminUser</code> to add to this
166    * <code>AuthenticationManager</code>
167    */

168   public void addAdminUser(AdminUser user)
169   {
170     synchronized (adminUsers)
171     {
172       adminUsers.add(user);
173     }
174   }
175
176   /**
177    * Associates a new database backend user to a virtual database login.
178    *
179    * @param vLogin the virtual database login.
180    * @param rUser the database backend user to add.
181    * @exception AuthenticationManagerException if a real user already exists for
182    * this backend.
183    */

184   public void addRealUser(String JavaDoc vLogin, DatabaseBackendUser rUser)
185       throws AuthenticationManagerException
186   {
187     HashMap JavaDoc list = (HashMap JavaDoc) realLogins.get(vLogin);
188     if (list == null)
189     {
190       list = new HashMap JavaDoc();
191       list.put(rUser.getBackendName(), rUser);
192       realLogins.put(vLogin, list);
193     }
194     else
195     {
196       DatabaseBackendUser u = (DatabaseBackendUser) list.get(rUser
197           .getBackendName());
198       if (u != null)
199         throw new AuthenticationManagerException(
200             Translate.get("authentication.failed.add.user.already.exists",
201                 new String JavaDoc[]{rUser.getLogin(), vLogin, rUser.getBackendName(),
202                     u.getLogin()}));
203       list.put(rUser.getBackendName(), rUser);
204     }
205   }
206
207   /**
208    * Registers a new virtual database user.
209    *
210    * @param vUser the <code>VirtualDatabaseUser</code> to register.
211    */

212   public void addVirtualUser(VirtualDatabaseUser vUser)
213   {
214     synchronized (virtualLogins)
215     {
216       virtualLogins.add(vUser);
217     }
218   }
219
220   /**
221    * Remove an admin user from the admin list
222    *
223    * @param user the admin to remove
224    * @return <code>true</code> if was removed.
225    */

226   public boolean removeAdminUser(AdminUser user)
227   {
228     synchronized (adminUsers)
229     {
230       return adminUsers.remove(user);
231     }
232   }
233
234   /**
235    * Unregisters a new virtual database user.
236    *
237    * @param vUser the <code>VirtualDatabaseUser</code> to unregister.
238    */

239   public void removeVirtualUser(VirtualDatabaseUser vUser)
240   {
241     synchronized (virtualLogins)
242     {
243       virtualLogins.remove(vUser);
244     }
245   }
246
247   /**
248    * Returns true if the given user is allowed to connect from the given
249    * address. Not that if the user does not exist, this function returns false.
250    * If no access control is defined for this user, the function returns always
251    * true.
252    *
253    * @param user the user to look for
254    * @param clientIpAddress the address the client connects from
255    * @return true if the client is allowed to connect
256    */

257   public boolean userIsAuthorizedToConnectFrom(VirtualDatabaseUser user,
258       String JavaDoc clientIpAddress)
259   {
260     synchronized (virtualLogins)
261     {
262       int vUserIdx = virtualLogins.indexOf(user);
263       if (vUserIdx == -1)
264         return false;
265       VirtualDatabaseUser vLogin = (VirtualDatabaseUser) virtualLogins
266           .get(vUserIdx);
267       AccessControl acl = vLogin.getAccessControl();
268       if (acl == null)
269         return true;
270       return acl.acceptConnectionFrom(clientIpAddress);
271     }
272   }
273
274   /**
275    * @return Returns the adminUsers.
276    */

277   public ArrayList JavaDoc getAdminUsers()
278   {
279     return adminUsers;
280   }
281
282   /**
283    * Gets the <code>DatabaseBackendUser</code> given a virtual database login
284    * and a database backend logical name.
285    *
286    * @param vLogin virtual database login.
287    * @param backendName database backend logical name.
288    * @return a <code>DatabaseBackendUser</code> value or <code>null</code>
289    * if not found.
290    */

291   public DatabaseBackendUser getDatabaseBackendUser(String JavaDoc vLogin,
292       String JavaDoc backendName)
293   {
294     Object JavaDoc list = realLogins.get(vLogin);
295     if (list == null)
296       return null;
297     else
298       return (DatabaseBackendUser) ((HashMap JavaDoc) list).get(backendName);
299   }
300
301   /**
302    * @return Returns the realLogins.
303    */

304   public HashMap JavaDoc getRealLogins()
305   {
306     return realLogins;
307   }
308
309   /**
310    * @return Returns the virtualLogins.
311    */

312   public ArrayList JavaDoc getVirtualLogins()
313   {
314     return virtualLogins;
315   }
316
317   /**
318    * Return the virtual password corresponding to the given virtual login
319    *
320    * @param vLogin the virtual login
321    * @return the virtual password if found, null otherwise
322    */

323   public String JavaDoc getVirtualPassword(String JavaDoc vLogin)
324   {
325     Iterator JavaDoc iter = virtualLogins.iterator();
326     VirtualDatabaseUser u;
327     while (iter.hasNext())
328     {
329       u = (VirtualDatabaseUser) iter.next();
330       if (u.getLogin().equals(vLogin))
331       {
332         return u.getPassword();
333       }
334     }
335     return null;
336   }
337
338   /*
339    * 5. Xml
340    */

341
342   /**
343    * Format to xml
344    *
345    * @return xml formatted representation
346    */

347   public String JavaDoc getXml()
348   {
349     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
350     info.append("<" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
351     for (int i = 0; i < adminUsers.size(); i++)
352     {
353       AdminUser adminUser = (AdminUser) adminUsers.get(i);
354       info.append(adminUser.getXml());
355     }
356
357     for (int i = 0; i < virtualLogins.size(); i++)
358     {
359       VirtualDatabaseUser vu = (VirtualDatabaseUser) virtualLogins.get(i);
360       info.append(vu.getXml());
361     }
362     info.append("</" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
363     return info.toString();
364   }
365
366 }
367
Popular Tags