1 7 8 package javax.naming.spi; 9 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.StringTokenizer ; 13 import java.net.MalformedURLException ; 14 15 import javax.naming.*; 16 import com.sun.naming.internal.VersionHelper; 17 import com.sun.naming.internal.ResourceManager; 18 import com.sun.naming.internal.FactoryEnumeration; 19 20 46 47 public class NamingManager { 48 49 53 54 NamingManager() {} 55 56 static final VersionHelper helper = VersionHelper.getVersionHelper(); 58 59 61 64 private static ObjectFactoryBuilder object_factory_builder = null; 65 66 91 public static synchronized void setObjectFactoryBuilder( 92 ObjectFactoryBuilder builder) throws NamingException { 93 if (object_factory_builder != null) 94 throw new IllegalStateException ("ObjectFactoryBuilder already set"); 95 96 SecurityManager security = System.getSecurityManager(); 97 if (security != null) { 98 security.checkSetFactory(); 99 } 100 object_factory_builder = builder; 101 } 102 103 106 static synchronized ObjectFactoryBuilder getObjectFactoryBuilder() { 107 return object_factory_builder; 108 } 109 110 111 120 static ObjectFactory getObjectFactoryFromReference( 121 Reference ref, String factoryName) 122 throws IllegalAccessException , 123 InstantiationException , 124 MalformedURLException { 125 Class clas = null; 126 127 try { 129 clas = helper.loadClass(factoryName); 130 } catch (ClassNotFoundException e) { 131 } 134 136 String codebase; 138 if (clas == null && 139 (codebase = ref.getFactoryClassLocation()) != null) { 140 try { 141 clas = helper.loadClass(factoryName, codebase); 142 } catch (ClassNotFoundException e) { 143 } 144 } 145 146 return (clas != null) ? (ObjectFactory ) clas.newInstance() : null; 147 } 148 149 150 157 private static Object createObjectFromFactories(Object obj, Name name, 158 Context nameCtx, Hashtable environment) throws Exception { 159 160 FactoryEnumeration factories = ResourceManager.getFactories( 161 Context.OBJECT_FACTORIES, environment, nameCtx); 162 163 if (factories == null) 164 return null; 165 166 ObjectFactory factory; 168 Object answer = null; 169 while (answer == null && factories.hasMore()) { 170 factory = (ObjectFactory )factories.next(); 171 answer = factory.getObjectInstance(obj, name, nameCtx, environment); 172 } 173 return answer; 174 } 175 176 private static String getURLScheme(String str) { 177 int colon_posn = str.indexOf(':'); 178 int slash_posn = str.indexOf('/'); 179 180 if (colon_posn > 0 && (slash_posn == -1 || colon_posn < slash_posn)) 181 return str.substring(0, colon_posn); 182 return null; 183 } 184 185 270 public static Object 271 getObjectInstance(Object refInfo, Name name, Context nameCtx, 272 Hashtable <?,?> environment) 273 throws Exception 274 { 275 276 ObjectFactory factory; 277 278 ObjectFactoryBuilder builder = getObjectFactoryBuilder(); 280 if (builder != null) { 281 factory = builder.createObjectFactory(refInfo, environment); 283 return factory.getObjectInstance(refInfo, name, nameCtx, 284 environment); 285 } 286 287 Reference ref = null; 289 if (refInfo instanceof Reference) { 290 ref = (Reference) refInfo; 291 } else if (refInfo instanceof Referenceable) { 292 ref = ((Referenceable)(refInfo)).getReference(); 293 } 294 295 Object answer; 296 297 if (ref != null) { 298 String f = ref.getFactoryClassName(); 299 if (f != null) { 300 302 factory = getObjectFactoryFromReference(ref, f); 303 if (factory != null) { 304 return factory.getObjectInstance(ref, name, nameCtx, 305 environment); 306 } 307 return refInfo; 311 312 } else { 313 316 answer = processURLAddrs(ref, name, nameCtx, environment); 317 if (answer != null) { 318 return answer; 319 } 320 } 321 } 322 323 answer = 325 createObjectFromFactories(refInfo, name, nameCtx, environment); 326 return (answer != null) ? answer : refInfo; 327 } 328 329 334 static Object processURLAddrs(Reference ref, Name name, Context nameCtx, 335 Hashtable environment) 336 throws NamingException { 337 338 for (int i = 0; i < ref.size(); i++) { 339 RefAddr addr = ref.get(i); 340 if (addr instanceof StringRefAddr && 341 addr.getType().equalsIgnoreCase("URL")) { 342 343 String url = (String )addr.getContent(); 344 Object answer = processURL(url, name, nameCtx, environment); 345 if (answer != null) { 346 return answer; 347 } 348 } 349 } 350 return null; 351 } 352 353 private static Object processURL(Object refInfo, Name name, 354 Context nameCtx, Hashtable environment) 355 throws NamingException { 356 Object answer; 357 358 if (refInfo instanceof String ) { 361 String url = (String )refInfo; 362 String scheme = getURLScheme(url); 363 if (scheme != null) { 364 answer = getURLObject(scheme, refInfo, name, nameCtx, 365 environment); 366 if (answer != null) { 367 return answer; 368 } 369 } 370 } 371 372 if (refInfo instanceof String []) { 376 String [] urls = (String [])refInfo; 377 for (int i = 0; i <urls.length; i++) { 378 String scheme = getURLScheme(urls[i]); 379 if (scheme != null) { 380 answer = getURLObject(scheme, refInfo, name, nameCtx, 381 environment); 382 if (answer != null) 383 return answer; 384 } 385 } 386 } 387 return null; 388 } 389 390 391 412 static Context getContext(Object obj, Name name, Context nameCtx, 413 Hashtable environment) throws NamingException { 414 Object answer; 415 416 if (obj instanceof Context) { 417 return (Context)obj; 419 } 420 421 try { 422 answer = getObjectInstance(obj, name, nameCtx, environment); 423 } catch (NamingException e) { 424 throw e; 425 } catch (Exception e) { 426 NamingException ne = new NamingException(); 427 ne.setRootCause(e); 428 throw ne; 429 } 430 431 return (answer instanceof Context) 432 ? (Context)answer 433 : null; 434 } 435 436 static Resolver getResolver(Object obj, Name name, Context nameCtx, 438 Hashtable environment) throws NamingException { 439 Object answer; 440 441 if (obj instanceof Resolver ) { 442 return (Resolver )obj; 444 } 445 446 try { 447 answer = getObjectInstance(obj, name, nameCtx, environment); 448 } catch (NamingException e) { 449 throw e; 450 } catch (Exception e) { 451 NamingException ne = new NamingException(); 452 ne.setRootCause(e); 453 throw ne; 454 } 455 456 return (answer instanceof Resolver ) 457 ? (Resolver )answer 458 : null; 459 } 460 461 462 463 464 526 public static Context getURLContext(String scheme, 527 Hashtable <?,?> environment) 528 throws NamingException 529 { 530 533 Object answer = getURLObject(scheme, null, null, null, environment); 534 if (answer instanceof Context) { 535 return (Context)answer; 536 } else { 537 return null; 538 } 539 } 540 541 private static final String defaultPkgPrefix = "com.sun.jndi.url"; 542 543 569 private static Object getURLObject(String scheme, Object urlInfo, 570 Name name, Context nameCtx, 571 Hashtable environment) 572 throws NamingException { 573 574 ObjectFactory factory = (ObjectFactory )ResourceManager.getFactory( 576 Context.URL_PKG_PREFIXES, environment, nameCtx, 577 "." + scheme + "." + scheme + "URLContextFactory", defaultPkgPrefix); 578 579 if (factory == null) 580 return null; 581 582 try { 584 return factory.getObjectInstance(urlInfo, name, nameCtx, environment); 585 } catch (NamingException e) { 586 throw e; 587 } catch (Exception e) { 588 NamingException ne = new NamingException(); 589 ne.setRootCause(e); 590 throw ne; 591 } 592 593 } 594 595 596 private static InitialContextFactoryBuilder initctx_factory_builder = null; 598 599 603 private static synchronized InitialContextFactoryBuilder 604 getInitialContextFactoryBuilder() { 605 return initctx_factory_builder; 606 } 607 608 633 public static Context getInitialContext(Hashtable <?,?> env) 634 throws NamingException { 635 InitialContextFactory factory; 636 637 InitialContextFactoryBuilder builder = getInitialContextFactoryBuilder(); 638 if (builder == null) { 639 642 String className = env != null ? 643 (String )env.get(Context.INITIAL_CONTEXT_FACTORY) : null; 644 if (className == null) { 645 NoInitialContextException ne = new NoInitialContextException( 646 "Need to specify class name in environment or system " + 647 "property, or as an applet parameter, or in an " + 648 "application resource file: " + 649 Context.INITIAL_CONTEXT_FACTORY); 650 throw ne; 651 } 652 653 try { 654 factory = (InitialContextFactory ) 655 helper.loadClass(className).newInstance(); 656 } catch(Exception e) { 657 NoInitialContextException ne = 658 new NoInitialContextException( 659 "Cannot instantiate class: " + className); 660 ne.setRootCause(e); 661 throw ne; 662 } 663 } else { 664 factory = builder.createInitialContextFactory(env); 665 } 666 667 return factory.getInitialContext(env); 668 } 669 670 671 688 public static synchronized void setInitialContextFactoryBuilder( 689 InitialContextFactoryBuilder builder) 690 throws NamingException { 691 if (initctx_factory_builder != null) 692 throw new IllegalStateException ( 693 "InitialContextFactoryBuilder already set"); 694 695 SecurityManager security = System.getSecurityManager(); 696 if (security != null) { 697 security.checkSetFactory(); 698 } 699 initctx_factory_builder = builder; 700 } 701 702 709 public static boolean hasInitialContextFactoryBuilder() { 710 return (getInitialContextFactoryBuilder() != null); 711 } 712 713 715 728 public static final String CPE = "java.naming.spi.CannotProceedException"; 729 730 757 public static Context getContinuationContext(CannotProceedException cpe) 758 throws NamingException { 759 760 Hashtable env = cpe.getEnvironment(); 761 if (env == null) { 762 env = new Hashtable (7); 763 } else { 764 env = (Hashtable ) env.clone(); 766 } 767 env.put(CPE, cpe); 768 769 ContinuationContext cctx = new ContinuationContext (cpe, env); 770 return cctx.getTargetContext(); 771 } 772 773 775 834 public static Object 835 getStateToBind(Object obj, Name name, Context nameCtx, 836 Hashtable <?,?> environment) 837 throws NamingException 838 { 839 840 FactoryEnumeration factories = ResourceManager.getFactories( 841 Context.STATE_FACTORIES, environment, nameCtx); 842 843 if (factories == null) { 844 return obj; 845 } 846 847 StateFactory factory; 849 Object answer = null; 850 while (answer == null && factories.hasMore()) { 851 factory = (StateFactory )factories.next(); 852 answer = factory.getStateToBind(obj, name, nameCtx, environment); 853 } 854 855 return (answer != null) ? answer : obj; 856 } 857 } 858 | Popular Tags |