1 7 8 package javax.naming; 9 10 import java.util.Hashtable ; 11 import javax.naming.spi.NamingManager ; 12 import com.sun.naming.internal.ResourceManager; 13 14 113 114 public class InitialContext implements Context { 115 116 124 protected Hashtable <Object ,Object > myProps = null; 125 126 133 protected Context defaultInitCtx = null; 134 135 140 protected boolean gotDefault = false; 141 142 159 protected InitialContext(boolean lazy) throws NamingException { 160 if (!lazy) { 161 init(null); 162 } 163 } 164 165 174 public InitialContext() throws NamingException { 175 init(null); 176 } 177 178 191 public InitialContext(Hashtable <?,?> environment) 192 throws NamingException 193 { 194 if (environment != null) { 195 environment = (Hashtable )environment.clone(); 196 } 197 init(environment); 198 } 199 200 216 protected void init(Hashtable <?,?> environment) 217 throws NamingException 218 { 219 myProps = ResourceManager.getInitialEnvironment(environment); 220 221 if (myProps.get(Context.INITIAL_CONTEXT_FACTORY) != null) { 222 getDefaultInitCtx(); 224 } 225 } 226 227 private static String getURLScheme(String str) { 228 int colon_posn = str.indexOf(':'); 229 int slash_posn = str.indexOf('/'); 230 231 if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn)) 232 return str.substring(0, colon_posn); 233 return null; 234 } 235 236 245 protected Context getDefaultInitCtx() throws NamingException { 246 if (!gotDefault) { 247 defaultInitCtx = NamingManager.getInitialContext(myProps); 248 gotDefault = true; 249 } 250 if (defaultInitCtx == null) 251 throw new NoInitialContextException (); 252 253 return defaultInitCtx; 254 } 255 256 272 protected Context getURLOrDefaultInitCtx(String name) 273 throws NamingException { 274 if (NamingManager.hasInitialContextFactoryBuilder()) { 275 return getDefaultInitCtx(); 276 } 277 String scheme = getURLScheme(name); 278 if (scheme != null) { 279 Context ctx = NamingManager.getURLContext(scheme, myProps); 280 if (ctx != null) { 281 return ctx; 282 } 283 } 284 return getDefaultInitCtx(); 285 } 286 287 329 protected Context getURLOrDefaultInitCtx(Name name) 330 throws NamingException { 331 if (NamingManager.hasInitialContextFactoryBuilder()) { 332 return getDefaultInitCtx(); 333 } 334 if (name.size() > 0) { 335 String first = name.get(0); 336 String scheme = getURLScheme(first); 337 if (scheme != null) { 338 Context ctx = NamingManager.getURLContext(scheme, myProps); 339 if (ctx != null) { 340 return ctx; 341 } 342 } 343 } 344 return getDefaultInitCtx(); 345 } 346 347 350 public Object lookup(String name) throws NamingException { 351 return getURLOrDefaultInitCtx(name).lookup(name); 352 } 353 354 public Object lookup(Name name) throws NamingException { 355 return getURLOrDefaultInitCtx(name).lookup(name); 356 } 357 358 public void bind(String name, Object obj) throws NamingException { 359 getURLOrDefaultInitCtx(name).bind(name, obj); 360 } 361 362 public void bind(Name name, Object obj) throws NamingException { 363 getURLOrDefaultInitCtx(name).bind(name, obj); 364 } 365 366 public void rebind(String name, Object obj) throws NamingException { 367 getURLOrDefaultInitCtx(name).rebind(name, obj); 368 } 369 370 public void rebind(Name name, Object obj) throws NamingException { 371 getURLOrDefaultInitCtx(name).rebind(name, obj); 372 } 373 374 public void unbind(String name) throws NamingException { 375 getURLOrDefaultInitCtx(name).unbind(name); 376 } 377 378 public void unbind(Name name) throws NamingException { 379 getURLOrDefaultInitCtx(name).unbind(name); 380 } 381 382 public void rename(String oldName, String newName) throws NamingException { 383 getURLOrDefaultInitCtx(oldName).rename(oldName, newName); 384 } 385 386 public void rename(Name oldName, Name newName) 387 throws NamingException 388 { 389 getURLOrDefaultInitCtx(oldName).rename(oldName, newName); 390 } 391 392 public NamingEnumeration <NameClassPair > list(String name) 393 throws NamingException 394 { 395 return (getURLOrDefaultInitCtx(name).list(name)); 396 } 397 398 public NamingEnumeration <NameClassPair > list(Name name) 399 throws NamingException 400 { 401 return (getURLOrDefaultInitCtx(name).list(name)); 402 } 403 404 public NamingEnumeration <Binding > listBindings(String name) 405 throws NamingException { 406 return getURLOrDefaultInitCtx(name).listBindings(name); 407 } 408 409 public NamingEnumeration <Binding > listBindings(Name name) 410 throws NamingException { 411 return getURLOrDefaultInitCtx(name).listBindings(name); 412 } 413 414 public void destroySubcontext(String name) throws NamingException { 415 getURLOrDefaultInitCtx(name).destroySubcontext(name); 416 } 417 418 public void destroySubcontext(Name name) throws NamingException { 419 getURLOrDefaultInitCtx(name).destroySubcontext(name); 420 } 421 422 public Context createSubcontext(String name) throws NamingException { 423 return getURLOrDefaultInitCtx(name).createSubcontext(name); 424 } 425 426 public Context createSubcontext(Name name) throws NamingException { 427 return getURLOrDefaultInitCtx(name).createSubcontext(name); 428 } 429 430 public Object lookupLink(String name) throws NamingException { 431 return getURLOrDefaultInitCtx(name).lookupLink(name); 432 } 433 434 public Object lookupLink(Name name) throws NamingException { 435 return getURLOrDefaultInitCtx(name).lookupLink(name); 436 } 437 438 public NameParser getNameParser(String name) throws NamingException { 439 return getURLOrDefaultInitCtx(name).getNameParser(name); 440 } 441 442 public NameParser getNameParser(Name name) throws NamingException { 443 return getURLOrDefaultInitCtx(name).getNameParser(name); 444 } 445 446 453 public String composeName(String name, String prefix) 454 throws NamingException { 455 return name; 456 } 457 458 465 public Name composeName(Name name, Name prefix) 466 throws NamingException 467 { 468 return (Name )name.clone(); 469 } 470 471 public Object addToEnvironment(String propName, Object propVal) 472 throws NamingException { 473 myProps.put(propName, propVal); 474 return getDefaultInitCtx().addToEnvironment(propName, propVal); 475 } 476 477 public Object removeFromEnvironment(String propName) 478 throws NamingException { 479 myProps.remove(propName); 480 return getDefaultInitCtx().removeFromEnvironment(propName); 481 } 482 483 public Hashtable <?,?> getEnvironment() throws NamingException { 484 return getDefaultInitCtx().getEnvironment(); 485 } 486 487 public void close() throws NamingException { 488 myProps = null; 489 if (defaultInitCtx != null) { 490 defaultInitCtx.close(); 491 defaultInitCtx = null; 492 } 493 gotDefault = false; 494 } 495 496 public String getNameInNamespace() throws NamingException { 497 return getDefaultInitCtx().getNameInNamespace(); 498 } 499 }; 500 | Popular Tags |