Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

AuthenticationManager.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Mathieu Peltier.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.authentication;
00026 
00027 import java.util.ArrayList;
00028 import java.util.HashMap;
00029 import java.util.Iterator;
00030 
00031 import org.objectweb.cjdbc.common.i18n.Translate;
00032 import org.objectweb.cjdbc.common.users.AdminUser;
00033 import org.objectweb.cjdbc.common.users.DatabaseBackendUser;
00034 import org.objectweb.cjdbc.common.users.VirtualDatabaseUser;
00035 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00036 
00037 /**
00038  * The <code>AuthenticationManager</code> manages the mapping between virtual
00039  * login/password (to the <code>VirtualDatabase</code>) and the real
00040  * login/password for each backend.
00041  * 
00042  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00043  * @author <a href="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
00044  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00045  * @version 1.0
00046  */
00047 public class AuthenticationManager
00048 {
00049   /*
00050    * How the code is organized ? 1. Member variables 2. Constructor(s) 3.
00051    * Login/Password checking functions 4. Getter/Setter (possibly in
00052    * alphabetical order) 5. Xml
00053    */
00054 
00055   /** <code>ArrayList</code> of <code>VirtualDatabaseUser</code> objects. */
00056   private ArrayList           virtualLogins;
00057 
00058   /** <code>ArrayList</code> of <code>AdminUser</code> objects. */
00059   private ArrayList           adminUsers;
00060 
00061  
00062   /**
00063    * <code>HashMap</code> of <code>HashMap</code> of <code>DatabaseBackendUser</code>
00064    * objects hashed by the backend name, hashed by their virtual database
00065    * login. A virtual user can have several real logins, but has only one real
00066    * login for a given backend.
00067    */
00068   private HashMap             realLogins;
00069 
00070   /*
00071    * Constructor(s)
00072    */
00073 
00074   /**
00075    * Creates a new <code>AuthenticationManager</code> instance.
00076    */
00077   public AuthenticationManager()
00078   {
00079     virtualLogins = new ArrayList();
00080     adminUsers = new ArrayList();
00081     realLogins = new HashMap();
00082   }
00083 
00084   /*
00085    * Login/Password checking functions
00086    */
00087 
00088   /**
00089    * Checks whether this administrator user has been registered to this <code>AuthenticationManager</code>
00090    * or not. Returns <code>false</code> if no admin user has been set.
00091    * 
00092    * @param user administrator user login/password to check.
00093    * @return <code>true</code> if it matches the registered admin user.
00094    */
00095   public boolean isValidAdminUser(AdminUser user)
00096   {
00097     return adminUsers.contains(user);
00098   }
00099 
00100   /**
00101    * Checks whether a given virtual database user has been registered to this
00102    * <code>AuthenticationManager</code> or not.
00103    * 
00104    * @param vUser the virtual database user.
00105    * @return <code>true</code> if the user login/password is valid.
00106    */
00107   public boolean isValidVirtualUser(VirtualDatabaseUser vUser)
00108   {
00109     return virtualLogins.contains(vUser);
00110   }
00111 
00112   /**
00113    * Checks whether a given virtual login has been registered to this <code>AuthenticationManager</code>
00114    * or not.
00115    * 
00116    * @param vLogin the virtual database login.
00117    * @return <code>true</code> if the virtual database login is valid.
00118    */
00119   public boolean isValidVirtualLogin(String vLogin)
00120   {
00121     Iterator iter = virtualLogins.iterator();
00122     VirtualDatabaseUser u;
00123     while (iter.hasNext())
00124     {
00125       u = (VirtualDatabaseUser) iter.next();
00126       if (u.getLogin().equals(vLogin))
00127       {
00128         return true;
00129       }
00130     }
00131     return false;
00132   }
00133 
00134   /*
00135    * Getter/Setter
00136    */
00137 
00138   /**
00139    * Sets the administrator user.
00140    * 
00141    * @param adminUser the administor user to set.
00142    */
00143   //  public void setAdminUser(VirtualDatabaseUser adminUser)
00144   //  {
00145   //    this.adminUser = adminUser;
00146   //  }
00147   /**
00148    * Registers a new virtual database user.
00149    * 
00150    * @param vUser the <code>VirtualDatabaseUser</code> to register.
00151    */
00152   public synchronized void addVirtualUser(VirtualDatabaseUser vUser)
00153   {
00154     virtualLogins.add(vUser);
00155   }
00156 
00157   /**
00158    * Associates a new database backend user to a virtual database login.
00159    * 
00160    * @param vLogin the virtual database login.
00161    * @param rUser the database backend user to add.
00162    * @exception AuthenticationManagerException if a real user already exists
00163    *                for this backend.
00164    */
00165   public void addRealUser(String vLogin, DatabaseBackendUser rUser)
00166       throws AuthenticationManagerException
00167   {
00168     HashMap list = (HashMap) realLogins.get(vLogin);
00169     if (list == null)
00170     {
00171       list = new HashMap();
00172       list.put(rUser.getBackendName(), rUser);
00173       realLogins.put(vLogin, list);
00174     }
00175     else
00176     {
00177       DatabaseBackendUser u = (DatabaseBackendUser) list.get(rUser
00178           .getBackendName());
00179       if (u != null)
00180         throw new AuthenticationManagerException(
00181             Translate.get("authentication.failed.add.user.already.exists",
00182                 new String[]{rUser.getLogin(), vLogin, rUser.getBackendName(),
00183                     u.getLogin()}));
00184       list.put(rUser.getBackendName(), rUser);
00185     }
00186   }
00187 
00188   /**
00189    * Gets the <code>DatabaseBackendUser</code> given a virtual database login
00190    * and a database backend logical name.
00191    * 
00192    * @param vLogin virtual database login.
00193    * @param backendName database backend logical name.
00194    * @return a <code>DatabaseBackendUser</code> value or <code>null</code>
00195    *         if not found.
00196    */
00197   public DatabaseBackendUser getDatabaseBackendUser(String vLogin,
00198       String backendName)
00199   {
00200     Object list = realLogins.get(vLogin);
00201     if (list == null)
00202       return null;
00203     else
00204       return (DatabaseBackendUser) ((HashMap) list).get(backendName);
00205   }
00206 
00207   /**
00208    * @return Returns the adminUser.
00209    */
00210   //  public VirtualDatabaseUser getAdminUser()
00211   //  {
00212   //    return adminUser;
00213   //  }
00214   /**
00215    * @return Returns the realLogins.
00216    */
00217   public HashMap getRealLogins()
00218   {
00219     return realLogins;
00220   }
00221 
00222   /**
00223    * @return Returns the virtualLogins.
00224    */
00225   public ArrayList getVirtualLogins()
00226   {
00227     return virtualLogins;
00228   }
00229 
00230   /*
00231    * 5. Xml
00232    */
00233   /**
00234    * Format to xml
00235    * 
00236    * @return xml formatted representation
00237    */
00238   public String getXml()
00239   {
00240     StringBuffer info = new StringBuffer();
00241     info.append("<" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
00242     info.append("<" + DatabasesXmlTags.ELT_Admin + ">");
00243     for (int i = 0; i < adminUsers.size(); i++)
00244     {
00245       AdminUser vu = (AdminUser) adminUsers.get(i);
00246       info.append(vu.getXml());          
00247     }
00248     info.append("</" + DatabasesXmlTags.ELT_Admin + ">");
00249 
00250     info.append("<" + DatabasesXmlTags.ELT_VirtualUsers + ">");
00251     for (int i = 0; i < virtualLogins.size(); i++)
00252     {
00253       VirtualDatabaseUser vu = (VirtualDatabaseUser) virtualLogins.get(i);
00254       info.append(vu.getXml());
00255     }
00256     info.append("</" + DatabasesXmlTags.ELT_VirtualUsers + ">");
00257     info.append("</" + DatabasesXmlTags.ELT_AuthenticationManager + ">");
00258     return info.toString();
00259   }
00260 
00261   /**
00262    * Add an admin user for this authentication manager.
00263    * 
00264    * @param user the <code>AdminUser</code> to add to this <code>AuthenticationManager</code>
00265    */
00266   public void addAdminUser(AdminUser user)
00267   {
00268     adminUsers.add(user);
00269   }
00270 
00271   /**
00272    * Remove an admin user from the admin list
00273    * 
00274    * @param user the admin to remove
00275    * @return <code>true</code> if was removed.
00276    */
00277   public boolean removeAdminUser(AdminUser user)
00278   {
00279     return adminUsers.remove(user);
00280   }
00281   
00282    /**
00283    * @return Returns the adminUsers.
00284    */
00285   public ArrayList getAdminUsers()
00286   {
00287     return adminUsers;
00288   }
00289 
00290 }

Generated on Mon Apr 11 22:01:29 2005 for C-JDBC by  doxygen 1.3.9.1