1 23 package com.sun.enterprise.security.auth.realm; 24 25 import java.io.*; 26 import java.util.*; 27 import java.util.logging.*; 28 import java.security.Principal ; 29 import java.security.acl.Group ; 30 import com.sun.logging.*; 31 import com.sun.enterprise.*; 32 import com.sun.enterprise.util.*; 33 import com.sun.enterprise.security.auth.realm.IASRealm; 34 35 36 47 public abstract class Realm implements Comparable { 48 49 private static LocalStringManagerImpl localStrings = 50 new LocalStringManagerImpl(Realm.class); 51 52 private static Hashtable loadedRealms = new Hashtable(); 53 private String myName; 54 55 private static String defaultRealmName="default"; 58 59 private final static String RI_DEFAULT="default"; 63 64 private Properties ctxProps; 66 67 68 73 public final String getName() { 74 return myName; 75 } 76 77 78 86 protected final void setName(String name) { 87 if (myName != null) { 88 return; 89 } 90 myName = name; 91 } 92 93 94 99 public String toString() { 100 return myName; 101 } 102 103 104 112 public int compareTo (Object realm) { 113 if (!(realm instanceof Realm)) { 114 return 1; 115 } 116 117 Realm r = (Realm) realm; 118 String str = r.getAuthType (); 119 int temp; 120 121 if ((temp = getAuthType ().compareTo (str)) != 0) { 122 return temp; 123 } 124 125 str = r.getName (); 126 return getName ().compareTo (str); 127 } 128 129 130 143 public static Realm instantiate(String name, String className, 144 Properties props) 145 throws BadRealmException 146 { 147 return doInstantiate(name, className, props); 148 } 149 150 151 159 public static Realm instantiate(String realmName, File f) 160 throws NoSuchRealmException, BadRealmException, FileNotFoundException 161 { 162 if (!f.exists() || !f.isFile()) { 163 throw new FileNotFoundException (); 164 } 165 166 if(_getInstance(realmName) != null) { 167 throw new BadRealmException( 168 localStrings.getLocalString("realm.already_exists", 169 "This Realm already exists.")); 170 } 171 172 InputStream in = null; 176 Properties props = new Properties(); 177 178 try{ 179 in = new FileInputStream(f); 180 props.load(in); 181 String classname = props.getProperty("classname"); 186 assert (classname != null); 187 188 return doInstantiate(realmName, classname, props); 189 } catch (IOException e) { 190 throw new BadRealmException(e.toString()); 191 } finally { 192 if (in != null) { 193 try { 194 in.close(); 195 } catch(Exception ex) { 196 } 197 } 198 } 199 } 200 201 202 206 private static Realm doInstantiate(String name, String className, 207 Properties props) 208 throws BadRealmException 209 { 210 try { 211 Class realmClass = Class.forName(className); 212 Object obj = realmClass.newInstance(); 213 Realm r = (Realm) obj; 214 r.setName(name); 215 r.init(props); 216 217 loadedRealms.put(name, r); 218 return r; 219 220 } catch(Exception e) { 221 throw new BadRealmException(e); 222 } 223 } 224 225 226 240 protected static void updateInstance(Realm realm, String name) 241 { 242 Realm oldRealm = (Realm)loadedRealms.get(name); 243 if (!oldRealm.getClass().equals(realm.getClass())) { 244 throw new Error ("Incompatible class "+realm.getClass()+ 246 " in replacement realm "+name); 247 } 248 realm.setName(oldRealm.getName()); 249 loadedRealms.put(name, realm); 250 } 251 252 253 261 public static Realm getDefaultInstance() throws NoSuchRealmException 262 { 263 return getInstance(defaultRealmName); 264 } 265 266 267 273 public static String getDefaultRealm() { 274 return defaultRealmName; 275 } 276 277 278 284 public static void setDefaultRealm(String realmName) { 285 defaultRealmName = realmName; 286 } 287 288 293 static void unloadInstance(String realmName) throws NoSuchRealmException { 294 getInstance(realmName); 296 loadedRealms.remove(realmName); 297 } 298 299 300 307 public void setProperty(String name, String value) 308 { 309 ctxProps.setProperty(name, value); 310 } 311 312 313 320 public String getProperty(String name) 321 { 322 return ctxProps.getProperty(name); 323 } 324 325 328 protected Properties getProperties() { 329 return ctxProps; 330 } 331 332 333 342 public String getJAASContext() 343 { 344 return ctxProps.getProperty(IASRealm.JAAS_CONTEXT_PARAM); 345 } 346 347 348 359 public static Realm getInstance(String name) throws NoSuchRealmException 360 { 361 Realm retval = _getInstance(name); 362 363 if (retval == null) { 364 throw new NoSuchRealmException( 365 localStrings.getLocalString("realm.no_such_realm", 366 name + " realm does not exist.", 367 new Object [] { name })); 368 } 369 370 return retval; 371 } 372 373 380 private static Realm _getInstance(String name) { 381 Realm retval = null; 382 retval = (Realm) loadedRealms.get (name); 383 384 388 if ( (retval == null) && (RI_DEFAULT.equals(name)) ) { 392 retval = (Realm) loadedRealms.get (defaultRealmName); 393 } 394 395 return retval; 396 } 397 398 399 403 public static Enumeration getRealmNames() { 404 return loadedRealms.keys(); 405 } 406 407 408 412 protected Realm() { 413 ctxProps = new Properties(); 414 } 415 416 417 428 protected void init(Properties props) 429 throws BadRealmException, NoSuchRealmException { 430 } 431 432 433 434 436 437 444 public abstract String getAuthType (); 445 446 452 public abstract AuthenticationHandler getAuthenticationHandler (); 453 454 460 public abstract Enumeration getUserNames() throws BadRealmException; 461 462 470 public abstract User getUser(String name) 471 throws NoSuchUserException, BadRealmException; 472 473 479 public abstract Enumeration getGroupNames() 480 throws BadRealmException; 481 482 491 public abstract Enumeration getGroupNames (String username) 492 throws InvalidOperationException, NoSuchUserException; 493 494 499 public abstract void refresh() throws BadRealmException; 500 501 506 public static boolean isValidRealm(String name){ 507 if(name == null){ 508 return false; 509 } else { 510 return loadedRealms.containsKey(name); 511 } 512 } 513 } 514 515 516 517 518 519 | Popular Tags |