UserManager.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *
00024  */
00025 
00026 package org.openmobileis.common.user;
00027 
00028 import java.util.Hashtable;
00029 
00030 import org.openmobileis.common.context.ApplicationContextManager;
00031 import org.openmobileis.common.util.exception.ServiceException;
00032 
00040 public class UserManager {
00041   private static UserManager manager;
00042 //  private UserManagerFactory factory;
00043   private Hashtable factoryList;
00044   private UserManagerDefaultFactory defaultFactory;
00045 
00046   private UserManager() {
00047     factoryList = new Hashtable(10);
00048   }
00049 
00050   public static UserManager getManager() {
00051     if (manager == null)  {
00052       synchronized (UserManager.class) {
00053         if (manager == null)  {
00054           manager = new UserManager();
00055           ApplicationContextManager.getManager().addManager(manager);
00056         }
00057       }
00058     }
00059     return manager;
00060   }
00061 
00062   public void registerUserManagerFactoryForGroup(String group, UserManagerFactory factory) {
00063     factoryList.put(group, factory);
00064   }
00065   
00066   public void registerUserManagerDefaultFactory(UserManagerDefaultFactory factory)      {
00067         this.defaultFactory = factory;
00068   }
00069 
00080   public String authenticateUser(String group, String login, String pass) throws ServiceException, UserNotFoundException {
00081     UserManagerFactory factory = (UserManagerFactory) factoryList.get(group);
00082     if (factory != null)  {
00083       return factory.authenticateUser(login, pass);
00084     } else  {
00085         if (defaultFactory != null)     {
00086                 defaultFactory.setAuthenticationGroup(group);
00087                 return defaultFactory.authenticateUser(login, pass);
00088         }
00089       throw new UserNotFoundException("No UserManagerFactory registered for group :"+group);
00090     }
00091   }
00092 
00093   public void logoutUser(String group, String userid)   throws ServiceException {
00094     UserManagerFactory factory = (UserManagerFactory) factoryList.get(group);
00095     if (factory != null)  {
00096       factory.logoutUser(userid);
00097       return;
00098     } else  {
00099         if (defaultFactory != null)     {
00100                 defaultFactory.setAuthenticationGroup(group);
00101                 defaultFactory.logoutUser(userid);
00102                 return;
00103         }
00104     }
00105                 throw new ServiceException("No UserManagerFactory registered for group :"+group);
00106  }
00107 
00108   public void storeUser(String group, User user) throws ServiceException, UserAlreadyExistException {
00109     UserManagerFactory factory = (UserManagerFactory) factoryList.get(group);
00110     if (factory != null)  {
00111       factory.storeUser(user);
00112     } else  {
00113         if (defaultFactory != null)     {
00114                 defaultFactory.setAuthenticationGroup(group);
00115                 defaultFactory.storeUser(user);
00116         }
00117       throw new ServiceException("No UserManagerFactory registered for group :"+group);
00118     }
00119   }
00120 
00121   public User getUser(String group, String userID) throws ServiceException, UserNotFoundException {
00122     UserManagerFactory factory = (UserManagerFactory) factoryList.get(group);
00123     if (factory != null)  {
00124       return factory.getUser(userID);
00125     } else  {
00126         if (defaultFactory != null)     {
00127                 defaultFactory.setAuthenticationGroup(group);
00128                 return defaultFactory.getUser(userID);
00129         }
00130      throw new UserNotFoundException("No UserManagerFactory registered for group :"+group);
00131     }
00132   }
00133 
00134   public void deleteUser(String group, String userID) throws ServiceException, UserNotFoundException {
00135     UserManagerFactory factory = (UserManagerFactory) factoryList.get(group);
00136     if (factory != null)  {
00137       factory.deleteUser(userID);
00138     } else  {
00139         if (defaultFactory != null)     {
00140                 defaultFactory.setAuthenticationGroup(group);
00141                 defaultFactory.deleteUser(userID);
00142         }
00143       throw new UserNotFoundException("No UserManagerFactory registered for group :"+group);
00144     }
00145   }
00146 }

Generated on Mon Jan 11 21:19:17 2010 for OpenMobileIS by  doxygen 1.5.4