KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > authentication > AuthenticationManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Mathieu Peltier.
23  */

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

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

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

68   private HashMap JavaDoc realLogins;
69
70   /*
71    * Constructor(s)
72    */

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

77   public AuthenticationManager()
78   {
79     virtualLogins = new ArrayList JavaDoc();
80     adminUsers = new ArrayList JavaDoc();
81     realLogins = new HashMap JavaDoc();
82   }
83
84   /*
85    * Login/Password checking functions
86    */

87
88   /**
89    * Checks whether this administrator user has been registered to this <code>AuthenticationManager</code>
90    * or not. Returns <code>false</code> if no admin user has been set.
91    *
92    * @param user administrator user login/password to check.
93    * @return <code>true</code> if it matches the registered admin user.
94    */

95   public boolean isValidAdminUser(AdminUser user)
96   {
97     return adminUsers.contains(user);
98   }
99
100   /**
101    * Checks whether a given virtual database user has been registered to this
102    * <code>AuthenticationManager</code> or not.
103    *
104    * @param vUser the virtual database user.
105    * @return <code>true</code> if the user login/password is valid.
106    */

107   public boolean isValidVirtualUser(VirtualDatabaseUser vUser)
108   {
109     return virtualLogins.contains(vUser);
110   }
111
112   /**
113    * Checks whether a given virtual login has been registered to this <code>AuthenticationManager</code>
114    * or not.
115    *
116    * @param vLogin the virtual database login.
117    * @return <code>true</code> if the virtual database login is valid.
118    */

119   public boolean isValidVirtualLogin(String JavaDoc vLogin)
120   {
121     Iterator JavaDoc iter = virtualLogins.iterator();
122     VirtualDatabaseUser u;
123     while (iter.hasNext())
124     {
125       u = (VirtualDatabaseUser) iter.next();
126       if (u.getLogin().equals(vLogin))
127       {
128         return true;
129       }
130     }
131     return false;
132   }
133
134   /*
135    * Getter/Setter
136    */

137
138   /**
139    * Sets the administrator user.
140    *
141    * @param adminUser the administor user to set.
142    */

143   // public void setAdminUser(VirtualDatabaseUser adminUser)
144
// {
145
// this.adminUser = adminUser;
146
// }
147
/**
148    * Registers a new virtual database user.
149    *
150    * @param vUser the <code>VirtualDatabaseUser</code> to register.
151    */

152   public synchronized void addVirtualUser(VirtualDatabaseUser vUser)
153   {
154     virtualLogins.add(vUser);
155   }
156
157   /**
158    * Associates a new database backend user to a virtual database login.
159    *
160    * @param vLogin the virtual database login.
161    * @param rUser the database backend user to add.
162    * @exception AuthenticationManagerException if a real user already exists
163    * for this backend.
164    */

165   public void addRealUser(String JavaDoc vLogin, DatabaseBackendUser rUser)
166       throws AuthenticationManagerException
167   {
168     HashMap JavaDoc list = (HashMap JavaDoc) realLogins.get(vLogin);
169     if (list == null)
170     {
171       list = new HashMap JavaDoc();
172       list.put(rUser.getBackendName(), rUser);
173       realLogins.put(vLogin, list);
174     }
175     else
176     {
177       DatabaseBackendUser u = (DatabaseBackendUser) list.get(rUser
178           .getBackendName());
179       if (u != null)
180         throw new AuthenticationManagerException(
181             Translate.get("authentication.failed.add.user.already.exists",
182                 new String JavaDoc[]{rUser.getLogin(), vLogin, rUser.getBackendName(),
183                     u.getLogin()}));
184       list.put(rUser.getBackendName(), rUser);
185     }
186   }
187
188   /**
189    * Gets the <code>DatabaseBackendUser</code> given a virtual database login
190    * and a database backend logical name.
191    *
192    * @param vLogin virtual database login.
193    * @param backendName database backend logical name.
194    * @return a <code>DatabaseBackendUser</code> value or <code>null</code>
195    * if not found.
196    */

197   public DatabaseBackendUser getDatabaseBackendUser(String JavaDoc vLogin,
198       String JavaDoc backendName)
199   {
200     Object JavaDoc list = realLogins.get(vLogin);
201     if (list == null)
202       return null;
203     else
204       return (DatabaseBackendUser) ((HashMap JavaDoc) list).get(backendName);
205   }
206
207   /**
208    * @return Returns the adminUser.
209    */

210   // public VirtualDatabaseUser getAdminUser()
211
// {
212
// return adminUser;
213
// }
214
/**
215    * @return Returns the realLogins.
216    */

217   public HashMap JavaDoc getRealLogins()
218   {
219     return realLogins;
220   }
221
222   /**
223    * @return Returns the virtualLogins.
224    */

225   public ArrayList JavaDoc getVirtualLogins()
226   {
227     return virtualLogins;
228   }
229
230   /*
231    * 5. Xml
232    */

233   /**
234    * Format to xml
235    *
236    * @return xml formatted representation
237    */

238   public String JavaDoc getXml()
239   {
240     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
241     info.append("<" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
242     info.append("<" + DatabasesXmlTags.ELT_Admin + ">");
243     for (int i = 0; i < adminUsers.size(); i++)
244     {
245       AdminUser vu = (AdminUser) adminUsers.get(i);
246       info.append(vu.getXml());
247     }
248     info.append("</" + DatabasesXmlTags.ELT_Admin + ">");
249
250     info.append("<" + DatabasesXmlTags.ELT_VirtualUsers + ">");
251     for (int i = 0; i < virtualLogins.size(); i++)
252     {
253       VirtualDatabaseUser vu = (VirtualDatabaseUser) virtualLogins.get(i);
254       info.append(vu.getXml());
255     }
256     info.append("</" + DatabasesXmlTags.ELT_VirtualUsers + ">");
257     info.append("</" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
258     return info.toString();
259   }
260
261   /**
262    * Add an admin user for this authentication manager.
263    *
264    * @param user the <code>AdminUser</code> to add to this <code>AuthenticationManager</code>
265    */

266   public void addAdminUser(AdminUser user)
267   {
268     adminUsers.add(user);
269   }
270
271   /**
272    * Remove an admin user from the admin list
273    *
274    * @param user the admin to remove
275    * @return <code>true</code> if was removed.
276    */

277   public boolean removeAdminUser(AdminUser user)
278   {
279     return adminUsers.remove(user);
280   }
281   
282    /**
283    * @return Returns the adminUsers.
284    */

285   public ArrayList JavaDoc getAdminUsers()
286   {
287     return adminUsers;
288   }
289
290 }
291
Popular Tags