1 24 25 package org.objectweb.cjdbc.controller.authentication; 26 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 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 47 public class AuthenticationManager 48 { 49 54 55 56 private ArrayList virtualLogins; 57 58 59 private ArrayList adminUsers; 60 61 62 68 private HashMap realLogins; 69 70 73 74 77 public AuthenticationManager() 78 { 79 virtualLogins = new ArrayList (); 80 adminUsers = new ArrayList (); 81 realLogins = new HashMap (); 82 } 83 84 87 88 95 public boolean isValidAdminUser(AdminUser user) 96 { 97 return adminUsers.contains(user); 98 } 99 100 107 public boolean isValidVirtualUser(VirtualDatabaseUser vUser) 108 { 109 return virtualLogins.contains(vUser); 110 } 111 112 119 public boolean isValidVirtualLogin(String vLogin) 120 { 121 Iterator 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 137 138 143 152 public synchronized void addVirtualUser(VirtualDatabaseUser vUser) 153 { 154 virtualLogins.add(vUser); 155 } 156 157 165 public void addRealUser(String vLogin, DatabaseBackendUser rUser) 166 throws AuthenticationManagerException 167 { 168 HashMap list = (HashMap ) realLogins.get(vLogin); 169 if (list == null) 170 { 171 list = new HashMap (); 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 []{rUser.getLogin(), vLogin, rUser.getBackendName(), 183 u.getLogin()})); 184 list.put(rUser.getBackendName(), rUser); 185 } 186 } 187 188 197 public DatabaseBackendUser getDatabaseBackendUser(String vLogin, 198 String backendName) 199 { 200 Object list = realLogins.get(vLogin); 201 if (list == null) 202 return null; 203 else 204 return (DatabaseBackendUser) ((HashMap ) list).get(backendName); 205 } 206 207 210 217 public HashMap getRealLogins() 218 { 219 return realLogins; 220 } 221 222 225 public ArrayList getVirtualLogins() 226 { 227 return virtualLogins; 228 } 229 230 233 238 public String getXml() 239 { 240 StringBuffer info = new StringBuffer (); 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 266 public void addAdminUser(AdminUser user) 267 { 268 adminUsers.add(user); 269 } 270 271 277 public boolean removeAdminUser(AdminUser user) 278 { 279 return adminUsers.remove(user); 280 } 281 282 285 public ArrayList getAdminUsers() 286 { 287 return adminUsers; 288 } 289 290 } 291 | Popular Tags |