1 22 package org.jnp.interfaces; 23 24 import java.io.BufferedInputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.lang.ref.WeakReference ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.net.DatagramPacket ; 31 import java.net.InetAddress ; 32 import java.net.MulticastSocket ; 33 import java.net.Socket ; 34 import java.net.InetSocketAddress ; 35 import java.rmi.ConnectException ; 36 import java.rmi.MarshalledObject ; 37 import java.util.ArrayList ; 38 import java.util.Arrays ; 39 import java.util.Collection ; 40 import java.util.Enumeration ; 41 import java.util.HashMap ; 42 import java.util.Hashtable ; 43 import java.util.Iterator ; 44 import java.util.StringTokenizer ; 45 import javax.naming.Binding ; 46 import javax.naming.CannotProceedException ; 47 import javax.naming.CommunicationException ; 48 import javax.naming.ConfigurationException ; 49 import javax.naming.Context ; 50 import javax.naming.InitialContext ; 51 import javax.naming.InvalidNameException ; 52 import javax.naming.LinkRef ; 53 import javax.naming.Name ; 54 import javax.naming.NameParser ; 55 import javax.naming.NamingEnumeration ; 56 import javax.naming.NamingException ; 57 import javax.naming.NotContextException ; 58 import javax.naming.ContextNotEmptyException ; 59 import javax.naming.OperationNotSupportedException ; 60 import javax.naming.Reference ; 61 import javax.naming.Referenceable ; 62 import javax.naming.ServiceUnavailableException ; 63 import javax.naming.event.EventContext ; 64 import javax.naming.event.NamingListener ; 65 import javax.naming.spi.NamingManager ; 66 import javax.naming.spi.ResolveResult ; 67 import javax.net.SocketFactory; 68 69 import org.jboss.logging.Logger; 70 71 83 public class NamingContext 84 implements EventContext , java.io.Serializable 85 { 86 90 static final long serialVersionUID = 8906455608484282128L; 91 94 public static final String JNP_SOCKET_FACTORY = "jnp.socketFactory"; 95 98 public static final String JNP_LOCAL_ADDRESS = "jnp.localAddress"; 99 102 public static final String JNP_LOCAL_PORT = "jnp.localPort"; 103 106 public static final String JNP_DISABLE_DISCOVERY = "jnp.disableDiscovery"; 107 110 public static final String JNP_PARTITION_NAME = "jnp.partitionName"; 111 114 public static final String JNP_DISCOVERY_GROUP = "jnp.discoveryGroup"; 115 118 public static final String JNP_DISCOVERY_PORT = "jnp.discoveryPort"; 119 120 121 public static final String JNP_DISCOVERY_TTL = "jnp.discoveryTTL"; 122 123 126 public static final String JNP_DISCOVERY_TIMEOUT = "jnp.discoveryTimeout"; 127 132 public static final String JNP_PARSED_NAME = "jnp.parsedName"; 133 138 public static final String JNP_USE_RELATIVE_NAME = "jnp.useRelativeName"; 139 145 public static final String JNP_MAX_RETRIES = "jnp.maxRetries"; 146 147 150 public final static String DEFAULT_DISCOVERY_GROUP_ADDRESS = "230.0.0.4"; 151 public final static int DEFAULT_DISCOVERY_GROUP_PORT = 1102; 152 public final static int DEFAULT_DISCOVERY_TIMEOUT = 5000; 153 154 157 public static int MAX_RETRIES = 1; 158 161 private static Logger log = Logger.getLogger(NamingContext.class); 162 163 165 public static Hashtable haServers = new Hashtable (); 166 167 public static void setHANamingServerForPartition(String partitionName, Naming haServer) 168 { 169 haServers.put(partitionName, haServer); 170 } 171 172 public static void removeHANamingServerForPartition(String partitionName) 173 { 174 haServers.remove(partitionName); 175 } 176 177 public static Naming getHANamingServerForPartition(String partitionName) 178 { 179 return (Naming) haServers.get(partitionName); 180 } 181 182 public static Naming localServer; 183 184 Naming naming; 186 Hashtable env; 187 Name prefix; 188 189 NameParser parser = new NamingParser(); 190 191 193 static HashMap cachedServers = new HashMap (); 200 201 static void addServer(String name, Naming server) 202 { 203 synchronized (NamingContext.class) 205 { 206 cachedServers.put(name, new WeakReference (server)); 207 } 208 } 209 210 static Naming getServer(String host, int port, Hashtable serverEnv) 211 throws NamingException 212 { 213 String hostKey = host + ":" + port; 215 WeakReference ref = (WeakReference ) cachedServers.get(hostKey); 216 Naming server; 217 if (ref != null) 218 { 219 server = (Naming) ref.get(); 220 if (server != null) 221 { 222 return server; 223 } 224 } 225 226 try 228 { 229 SocketFactory factory = loadSocketFactory(serverEnv); 230 Socket s; 231 232 try 233 { 234 InetAddress localAddr = null; 235 int localPort = 0; 236 String localAddrStr = (String ) serverEnv.get(JNP_LOCAL_ADDRESS); 237 String localPortStr = (String ) serverEnv.get(JNP_LOCAL_PORT); 238 if (localAddrStr != null) 239 localAddr = InetAddress.getByName(localAddrStr); 240 if (localPortStr != null) 241 localPort = Integer.parseInt(localPortStr); 242 s = factory.createSocket(host, port, localAddr, localPort); 243 } 244 catch (IOException e) 245 { 246 NamingException ex = new ServiceUnavailableException ("Failed to connect to server " + hostKey); 247 ex.setRootCause(e); 248 throw ex; 249 } 250 251 BufferedInputStream bis = new BufferedInputStream (s.getInputStream()); 253 ObjectInputStream in = new ObjectInputStream (bis); 254 MarshalledObject stub = (MarshalledObject ) in.readObject(); 255 server = (Naming) stub.get(); 256 s.close(); 257 258 addServer(hostKey, server); 260 serverEnv.put("hostKey", hostKey); 261 262 return server; 263 } 264 catch (IOException e) 265 { 266 NamingException ex = new CommunicationException ("Failed to retrieve stub from server " + hostKey); 267 ex.setRootCause(e); 268 throw ex; 269 } 270 catch (Exception e) 271 { 272 NamingException ex = new CommunicationException ("Failed to connect to server " + hostKey); 273 ex.setRootCause(e); 274 throw ex; 275 } 276 } 277 278 283 static SocketFactory loadSocketFactory(Hashtable serverEnv) 284 throws ClassNotFoundException , IllegalAccessException , 285 InstantiationException , InvocationTargetException 286 { 287 SocketFactory factory = null; 288 289 String socketFactoryName = (String ) serverEnv.get(JNP_SOCKET_FACTORY); 291 if (socketFactoryName == null || 292 socketFactoryName.equals(TimedSocketFactory.class.getName())) 293 { 294 factory = new TimedSocketFactory(serverEnv); 295 return factory; 296 } 297 298 301 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 302 Class factoryClass = loader.loadClass(socketFactoryName); 303 try 304 { 305 Class [] ctorSig = {Hashtable .class}; 306 Constructor ctor = factoryClass.getConstructor(ctorSig); 307 Object [] ctorArgs = {serverEnv}; 308 factory = (SocketFactory) ctor.newInstance(ctorArgs); 309 } 310 catch (NoSuchMethodException e) 311 { 312 factory = (SocketFactory) factoryClass.newInstance(); 314 } 315 return factory; 316 } 317 318 static void removeServer(Hashtable serverEnv) 319 { 320 String host = "localhost"; 321 int port = 1099; 322 323 if (serverEnv.get(Context.PROVIDER_URL) != null) 325 { 326 String providerURL = (String ) serverEnv.get(Context.PROVIDER_URL); 327 328 StringTokenizer tokenizer = new StringTokenizer (providerURL, ", "); 329 while (tokenizer.hasMoreElements()) 330 { 331 String url = tokenizer.nextToken(); 332 333 try 334 { 335 Name urlAsName = new NamingParser().parse(url); 337 String server = parseNameForScheme(urlAsName, null); 338 if (server != null) 339 url = server; 340 int colon = url.indexOf(':'); 341 if (colon < 0) 342 { 343 host = url.trim(); 344 } 345 else 346 { 347 host = url.substring(0, colon).trim(); 348 try 349 { 350 port = Integer.parseInt(url.substring(colon + 1).trim()); 351 } 352 catch (Exception ex) 353 { 354 } 356 } 357 358 synchronized (NamingContext.class) 360 { 361 cachedServers.remove(host + ":" + port); 362 } 363 } 364 catch (NamingException ignored) 365 { 366 } 367 } 368 Object hostKey = serverEnv.remove("hostKey"); 369 if (hostKey != null) 370 { 371 synchronized (NamingContext.class) 372 { 373 cachedServers.remove(hostKey); 374 } 375 } 376 } 377 else 378 { 379 } 381 } 382 383 391 static String parseNameForScheme(Name n, Hashtable nameEnv) 392 throws InvalidNameException 393 { 394 String serverInfo = null; 395 if (n.size() > 0) 396 { 397 String scheme = n.get(0); 398 int schemeLength = 0; 399 if (scheme.startsWith("java:")) 400 schemeLength = 5; 401 else if (scheme.startsWith("jnp:")) 402 schemeLength = 4; 403 else if (scheme.startsWith("jnps:")) 404 schemeLength = 5; 405 else if (scheme.startsWith("jnp-http:")) 406 schemeLength = 9; 407 else if (scheme.startsWith("jnp-https:")) 408 schemeLength = 10; 409 if (schemeLength > 0) 410 { 411 n = (Name ) n.clone(); 413 String suffix = scheme.substring(schemeLength); 414 if (suffix.length() == 0) 415 { 416 n.remove(0); 418 if (n.size() > 1 && n.get(0).equals("")) 419 { 420 serverInfo = n.get(1); 423 n.remove(0); 424 n.remove(0); 425 if (n.size() == 1 && n.get(0).length() == 0) 427 n.remove(0); 428 } 429 } 430 else 431 { 432 n.remove(0); 434 n.add(0, suffix); 435 } 436 if (nameEnv != null) 437 nameEnv.put(JNP_PARSED_NAME, n); 438 } 439 } 440 return serverInfo; 441 } 442 443 public static void setLocal(Naming server) 444 { 445 localServer = server; 446 } 447 448 public NamingContext(Hashtable e, Name baseName, Naming server) 450 throws NamingException 451 { 452 if (baseName == null) 453 this.prefix = parser.parse(""); 454 else 455 this.prefix = baseName; 456 457 if (e != null) 458 this.env = (Hashtable ) e.clone(); 459 else 460 this.env = new Hashtable (); 461 462 this.naming = server; 463 } 464 465 public Naming getNaming() 467 { 468 return this.naming; 469 } 470 471 public void setNaming(Naming server) 472 { 473 this.naming = server; 474 } 475 476 public void rebind(String name, Object obj) 478 throws NamingException 479 { 480 rebind(getNameParser(name).parse(name), obj); 481 } 482 483 public void rebind(Name name, Object obj) 484 throws NamingException 485 { 486 Hashtable refEnv = getEnv(name); 487 checkRef(refEnv); 488 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 489 if (parsedName != null) 490 name = parsedName; 491 492 obj = getStateToBind(obj, name, refEnv); 494 495 try 496 { 497 String className; 498 499 if (obj instanceof Referenceable ) 501 obj = ((Referenceable ) obj).getReference(); 502 503 if (!(obj instanceof Reference )) 504 { 505 className = obj.getClass().getName(); 506 obj = new MarshalledValuePair(obj); 508 } 509 else 510 { 511 className = ((Reference ) obj).getClassName(); 512 } 513 naming.rebind(getAbsoluteName(name), obj, className); 514 } 515 catch (CannotProceedException cpe) 516 { 517 cpe.setEnvironment(refEnv); 518 Context cctx = NamingManager.getContinuationContext(cpe); 519 cctx.rebind(cpe.getRemainingName(), obj); 520 } 521 catch (IOException e) 522 { 523 naming = null; 524 removeServer(refEnv); 525 NamingException ex = new CommunicationException (); 526 ex.setRootCause(e); 527 throw ex; 528 } 529 } 530 531 public void bind(String name, Object obj) 532 throws NamingException 533 { 534 bind(getNameParser(name).parse(name), obj); 535 } 536 537 public void bind(Name name, Object obj) 538 throws NamingException 539 { 540 Hashtable refEnv = getEnv(name); 541 checkRef(refEnv); 542 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 543 if (parsedName != null) 544 name = parsedName; 545 546 obj = getStateToBind(obj, name, refEnv); 548 549 try 550 { 551 String className; 552 553 if (obj instanceof Referenceable ) 555 obj = ((Referenceable ) obj).getReference(); 556 557 if (!(obj instanceof Reference )) 558 { 559 className = obj.getClass().getName(); 560 561 obj = new MarshalledValuePair(obj); 563 } 564 else 565 { 566 className = ((Reference ) obj).getClassName(); 567 } 568 name = getAbsoluteName(name); 569 naming.bind(name, obj, className); 570 } 571 catch (CannotProceedException cpe) 572 { 573 cpe.setEnvironment(refEnv); 574 Context cctx = NamingManager.getContinuationContext(cpe); 575 cctx.bind(cpe.getRemainingName(), obj); 576 } 577 catch (IOException e) 578 { 579 naming = null; 580 removeServer(refEnv); 581 NamingException ex = new CommunicationException (); 582 ex.setRootCause(e); 583 throw ex; 584 } 585 } 586 587 public Object lookup(String name) 588 throws NamingException 589 { 590 return lookup(getNameParser(name).parse(name)); 591 } 592 593 public Object lookup(Name name) 594 throws NamingException 595 { 596 Hashtable refEnv = getEnv(name); 597 checkRef(refEnv); 598 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 599 if (parsedName != null) 600 name = parsedName; 601 602 if (name.isEmpty()) 604 return new NamingContext(refEnv, prefix, naming); 605 606 try 607 { 608 int maxTries = 1; 609 try 610 { 611 String n = (String ) refEnv.get(JNP_MAX_RETRIES); 612 if( n != null ) 613 maxTries = Integer.parseInt(n); 614 if( maxTries <= 0 ) 615 maxTries = 1; 616 } 617 catch(Exception e) 618 { 619 log.debug("Failed to get JNP_MAX_RETRIES, using 1", e); 620 } 621 Name n = getAbsoluteName(name); 622 Object res = null; 623 boolean trace = log.isTraceEnabled(); 624 for (int i = 0; i < maxTries; i++) 625 { 626 try 627 { 628 res = naming.lookup(n); 629 break; 630 } 631 catch (ConnectException ce) 632 { 633 int retries = maxTries - i - 1; 634 if( trace ) 635 log.trace("Connect failed, retry count: "+retries, ce); 636 if (retries > 0) 638 { 639 try 640 { 641 Thread.sleep(1); 642 } 643 catch (InterruptedException ignored) 644 { 645 } 646 continue; 647 } 648 throw ce; 650 } 651 } 652 if (res instanceof MarshalledValuePair) 653 { 654 MarshalledValuePair mvp = (MarshalledValuePair) res; 655 Object storedObj = mvp.get(); 656 return getObjectInstanceWrapFailure(storedObj, name, refEnv); 657 } 658 else if (res instanceof MarshalledObject ) 659 { 660 MarshalledObject mo = (MarshalledObject ) res; 661 return mo.get(); 662 } 663 else if (res instanceof Context ) 664 { 665 Enumeration keys = refEnv.keys(); 667 while (keys.hasMoreElements()) 668 { 669 String key = (String ) keys.nextElement(); 670 ((Context ) res).addToEnvironment(key, refEnv.get(key)); 671 } 672 return res; 673 } 674 else if (res instanceof ResolveResult ) 675 { 676 ResolveResult rr = (ResolveResult ) res; 678 Object resolveRes = rr.getResolvedObj(); 679 Object context; 680 Object instanceID; 681 682 if (resolveRes instanceof LinkRef ) 683 { 684 context = resolveLink(resolveRes, null); 685 instanceID = ((LinkRef ) resolveRes).getLinkName(); 686 } 687 else 688 { 689 context = getObjectInstanceWrapFailure(resolveRes, name, refEnv); 690 instanceID = context; 691 } 692 693 if ((context instanceof Context ) == false) 694 { 695 throw new NotContextException (instanceID + " is not a Context"); 696 } 697 Context ncontext = (Context ) context; 698 return ncontext.lookup(rr.getRemainingName()); 699 } 700 else if (res instanceof LinkRef ) 701 { 702 res = resolveLink(res, refEnv); 704 } 705 else if (res instanceof Reference ) 706 { 707 res = getObjectInstanceWrapFailure(res, name, refEnv); 709 if (res instanceof LinkRef ) 710 res = resolveLink(res, refEnv); 711 } 712 713 return res; 714 } 715 catch (CannotProceedException cpe) 716 { 717 cpe.setEnvironment(refEnv); 718 Context cctx = NamingManager.getContinuationContext(cpe); 719 return cctx.lookup(cpe.getRemainingName()); 720 } 721 catch (IOException e) 722 { 723 naming = null; 724 removeServer(refEnv); 725 NamingException ex = new CommunicationException (); 726 ex.setRootCause(e); 727 throw ex; 728 } 729 catch (ClassNotFoundException e) 730 { 731 NamingException ex = new CommunicationException (); 732 ex.setRootCause(e); 733 throw ex; 734 } 735 } 736 737 public void unbind(String name) 738 throws NamingException 739 { 740 unbind(getNameParser(name).parse(name)); 741 } 742 743 744 public void unbind(Name name) 745 throws NamingException 746 { 747 Hashtable refEnv = getEnv(name); 748 checkRef(refEnv); 749 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 750 if (parsedName != null) 751 name = parsedName; 752 753 try 754 { 755 naming.unbind(getAbsoluteName(name)); 756 } 757 catch (CannotProceedException cpe) 758 { 759 cpe.setEnvironment(refEnv); 760 Context cctx = NamingManager.getContinuationContext(cpe); 761 cctx.unbind(cpe.getRemainingName()); 762 } 763 catch (IOException e) 764 { 765 naming = null; 766 removeServer(refEnv); 767 NamingException ex = new CommunicationException (); 768 ex.setRootCause(e); 769 throw ex; 770 } 771 } 772 773 public void rename(String oldname, String newname) 774 throws NamingException 775 { 776 rename(getNameParser(oldname).parse(oldname), getNameParser(newname).parse(newname)); 777 } 778 779 public void rename(Name oldName, Name newName) 780 throws NamingException 781 { 782 bind(newName, lookup(oldName)); 783 unbind(oldName); 784 } 785 786 public NamingEnumeration list(String name) 787 throws NamingException 788 { 789 return list(getNameParser(name).parse(name)); 790 } 791 792 public NamingEnumeration list(Name name) 793 throws NamingException 794 { 795 Hashtable refEnv = getEnv(name); 796 checkRef(refEnv); 797 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 798 if (parsedName != null) 799 name = parsedName; 800 801 try 802 { 803 return new NamingEnumerationImpl(naming.list(getAbsoluteName(name))); 804 } 805 catch (CannotProceedException cpe) 806 { 807 cpe.setEnvironment(refEnv); 808 Context cctx = NamingManager.getContinuationContext(cpe); 809 return cctx.list(cpe.getRemainingName()); 810 } 811 catch (IOException e) 812 { 813 naming = null; 814 removeServer(refEnv); 815 NamingException ex = new CommunicationException (); 816 ex.setRootCause(e); 817 throw ex; 818 } 819 } 820 821 public NamingEnumeration listBindings(String name) 822 throws NamingException 823 { 824 return listBindings(getNameParser(name).parse(name)); 825 } 826 827 public NamingEnumeration listBindings(Name name) 828 throws NamingException 829 { 830 Hashtable refEnv = getEnv(name); 831 checkRef(refEnv); 832 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 833 if (parsedName != null) 834 name = parsedName; 835 836 try 837 { 838 Collection bindings = naming.listBindings(getAbsoluteName(name)); 840 Collection realBindings = new ArrayList (bindings.size()); 841 842 Iterator i = bindings.iterator(); 844 while (i.hasNext()) 845 { 846 Binding binding = (Binding ) i.next(); 847 Object obj = binding.getObject(); 848 if (obj instanceof MarshalledValuePair) 849 { 850 try 851 { 852 obj = ((MarshalledValuePair) obj).get(); 853 } 854 catch (ClassNotFoundException e) 855 { 856 NamingException ex = new CommunicationException (); 857 ex.setRootCause(e); 858 throw ex; 859 } 860 } 861 else if (obj instanceof MarshalledObject ) 862 { 863 try 864 { 865 obj = ((MarshalledObject ) obj).get(); 866 } 867 catch (ClassNotFoundException e) 868 { 869 NamingException ex = new CommunicationException (); 870 ex.setRootCause(e); 871 throw ex; 872 } 873 } 874 realBindings.add(new Binding (binding.getName(), binding.getClassName(), obj)); 875 } 876 877 return new NamingEnumerationImpl(realBindings); 879 } 880 catch (CannotProceedException cpe) 881 { 882 cpe.setEnvironment(refEnv); 883 Context cctx = NamingManager.getContinuationContext(cpe); 884 return cctx.listBindings(cpe.getRemainingName()); 885 } 886 catch (IOException e) 887 { 888 naming = null; 889 removeServer(refEnv); 890 NamingException ex = new CommunicationException (); 891 ex.setRootCause(e); 892 throw ex; 893 } 894 } 895 896 public String composeName(String name, String prefix) 897 throws NamingException 898 { 899 Name result = composeName(parser.parse(name), 900 parser.parse(prefix)); 901 return result.toString(); 902 } 903 904 public Name composeName(Name name, Name prefix) 905 throws NamingException 906 { 907 Name result = (Name ) (prefix.clone()); 908 result.addAll(name); 909 return result; 910 } 911 912 public NameParser getNameParser(String name) 913 throws NamingException 914 { 915 return parser; 916 } 917 918 public NameParser getNameParser(Name name) 919 throws NamingException 920 { 921 return getNameParser(name.toString()); 922 } 923 924 public Context createSubcontext(String name) 925 throws NamingException 926 { 927 return createSubcontext(getNameParser(name).parse(name)); 928 } 929 930 public Context createSubcontext(Name name) 931 throws NamingException 932 { 933 if (name.size() == 0) 934 throw new InvalidNameException ("Cannot pass an empty name to createSubcontext"); 935 936 Hashtable refEnv = getEnv(name); 937 checkRef(refEnv); 938 Name parsedName = (Name ) refEnv.get(JNP_PARSED_NAME); 939 if (parsedName != null) 940 name = parsedName; 941 942 try 943 { 944 name = getAbsoluteName(name); 945 return naming.createSubcontext(name); 946 } 947 catch (CannotProceedException cpe) 948 { 949 cpe.setEnvironment(refEnv); 950 Context cctx = NamingManager.getContinuationContext(cpe); 951 return cctx.createSubcontext(cpe.getRemainingName()); 952 } 953 catch (IOException e) 954 { 955 naming = null; 956 removeServer(refEnv); 957 NamingException ex = new CommunicationException (); 958 ex.setRootCause(e); 959 throw ex; 960 } 961 } 962 963 public Object addToEnvironment(String propName, Object propVal) 964 throws NamingException 965 { 966 Object old = env.get(propName); 967 env.put(propName, propVal); 968 return old; 969 } 970 971 public Object removeFromEnvironment(String propName) 972 throws NamingException 973 { 974 return env.remove(propName); 975 } 976 977 public Hashtable getEnvironment() 978 throws NamingException 979 { 980 return env; 981 } 982 983 public void close() 984 throws NamingException 985 { 986 env = null; 987 naming = null; 988 } 989 990 public String getNameInNamespace() 991 throws NamingException 992 { 993 return prefix.toString(); 994 } 995 996 public void destroySubcontext(String name) 997 throws NamingException 998 { 999 destroySubcontext(getNameParser(name).parse(name)); 1000 } 1001 1002 public void destroySubcontext(Name name) 1003 throws NamingException 1004 { 1005 if (!list(name).hasMore()) 1006 { 1007 unbind(name); 1008 } 1009 else 1010 throw new ContextNotEmptyException (); 1011 } 1012 1013 public Object lookupLink(String name) 1014 throws NamingException 1015 { 1016 return lookupLink(getNameParser(name).parse(name)); 1017 } 1018 1019 1025 public Object lookupLink(Name name) 1026 throws NamingException 1027 { 1028 if (name.isEmpty()) 1029 return lookup(name); 1030 1031 Object link = null; 1032 try 1033 { 1034 Name n = getAbsoluteName(name); 1035 link = naming.lookup(n); 1036 if (!(link instanceof LinkRef ) && link instanceof Reference ) 1037 link = getObjectInstance(link, name, null); 1038 ; 1039 } 1040 catch (IOException e) 1041 { 1042 naming = null; 1043 removeServer(env); 1044 NamingException ex = new CommunicationException (); 1045 ex.setRootCause(e); 1046 throw ex; 1047 } 1048 catch (Exception e) 1049 { 1050 NamingException ex = new NamingException ("Could not lookup link"); 1051 ex.setRemainingName(name); 1052 ex.setRootCause(e); 1053 throw ex; 1054 } 1055 return link; 1056 } 1057 1058 public void addNamingListener(Name target, int scope, NamingListener l) 1060 throws NamingException 1061 { 1062 throw new UnsupportedOperationException ("This is not supported currently"); 1064 } 1065 1066 public void addNamingListener(String target, int scope, NamingListener l) 1067 throws NamingException 1068 { 1069 throw new UnsupportedOperationException ("This is not supported currently"); 1071 } 1072 1073 public void removeNamingListener(NamingListener l) 1074 throws NamingException 1075 { 1076 throw new UnsupportedOperationException ("This is not supported currently"); 1078 } 1079 1080 public boolean targetMustExist() 1081 throws NamingException 1082 { 1083 return false; 1085 } 1086 1088 protected Object resolveLink(Object res, Hashtable refEnv) 1089 throws NamingException 1090 { 1091 Object linkResult = null; 1092 try 1093 { 1094 LinkRef link = (LinkRef ) res; 1095 String ref = link.getLinkName(); 1096 if (ref.startsWith("./")) 1097 linkResult = lookup(ref.substring(2)); 1098 else if (refEnv != null) 1099 linkResult = new InitialContext (refEnv).lookup(ref); 1100 else 1101 linkResult = new InitialContext ().lookup(ref); 1102 } 1103 catch (Exception e) 1104 { 1105 NamingException ex = new NamingException ("Could not dereference object"); 1106 ex.setRootCause(e); 1107 throw ex; 1108 } 1109 return linkResult; 1110 } 1111 1112 1114 1124 private boolean useAbsoluteName(Hashtable env) 1125 { 1126 if (env == null) 1127 return true; 1128 String useRelativeName = (String ) env.get(JNP_USE_RELATIVE_NAME); 1129 return Boolean.valueOf(useRelativeName) == Boolean.FALSE; 1130 } 1131 1132 1141 private Object getStateToBind(Object obj, Name name, Hashtable env) 1142 throws NamingException 1143 { 1144 if (useAbsoluteName(env)) 1145 name = getAbsoluteName(name); 1146 return NamingManager.getStateToBind(obj, name, this, env); 1147 } 1148 1149 1158 private Object getObjectInstance(Object obj, Name name, Hashtable env) 1159 throws Exception 1160 { 1161 if (useAbsoluteName(env)) 1162 name = getAbsoluteName(name); 1163 return NamingManager.getObjectInstance(obj, name, this, env); 1164 } 1165 1166 1175 private Object getObjectInstanceWrapFailure(Object obj, Name name, Hashtable env) 1176 throws NamingException 1177 { 1178 try 1179 { 1180 return getObjectInstance(obj, name, env); 1181 } 1182 catch (NamingException e) 1183 { 1184 throw e; 1185 } 1186 catch (Exception e) 1187 { 1188 NamingException ex = new NamingException ("Could not dereference object"); 1189 ex.setRootCause(e); 1190 throw ex; 1191 } 1192 } 1193 1194 1198 private Naming discoverServer(Hashtable serverEnv) throws NamingException 1199 { 1200 boolean trace = log.isTraceEnabled(); 1201 String disableDiscovery = (String ) serverEnv.get(JNP_DISABLE_DISCOVERY); 1203 if (Boolean.valueOf(disableDiscovery) == Boolean.TRUE) 1204 { 1205 if (trace) 1206 log.trace("Skipping discovery due to disable flag"); 1207 return null; 1208 } 1209 1210 String partitionName = (String ) serverEnv.get(JNP_PARTITION_NAME); 1213 Naming server = null; 1214 if (partitionName != null) 1215 { 1216 server = getHANamingServerForPartition(partitionName); 1217 if (server != null) 1218 return server; 1219 } 1220 1221 MulticastSocket s = null; 1226 InetAddress iaGroup = null; 1227 try 1228 { 1229 String group = DEFAULT_DISCOVERY_GROUP_ADDRESS; 1230 int port = DEFAULT_DISCOVERY_GROUP_PORT; 1231 int timeout = DEFAULT_DISCOVERY_TIMEOUT; 1232 int ttl = 16; 1233 1234 String discoveryGroup = (String ) serverEnv.get(JNP_DISCOVERY_GROUP); 1235 if (discoveryGroup != null) 1236 group = discoveryGroup; 1237 1238 String discoveryTTL = (String ) serverEnv.get(JNP_DISCOVERY_TTL); 1239 if(discoveryTTL != null) 1240 ttl = Integer.parseInt(discoveryTTL); 1241 1242 String discoveryTimeout = (String ) serverEnv.get(JNP_DISCOVERY_TIMEOUT); 1243 if (discoveryTimeout == null) 1244 { 1245 discoveryTimeout = (String ) serverEnv.get("DISCOVERY_TIMEOUT"); 1247 } 1248 if (discoveryTimeout != null && !discoveryTimeout.equals("")) 1249 timeout = Integer.parseInt(discoveryTimeout); 1250 1251 String discoveryGroupPort = (String ) serverEnv.get(JNP_DISCOVERY_PORT); 1252 if (discoveryGroupPort == null) 1253 { 1254 discoveryGroupPort = (String ) serverEnv.get("DISCOVERY_GROUP"); 1256 } 1257 if (discoveryGroupPort != null && !discoveryGroupPort.equals("")) 1258 { 1259 int colon = discoveryGroupPort.indexOf(':'); 1260 if (colon < 0) 1261 { 1262 try 1264 { 1265 port = Integer.parseInt(discoveryGroupPort); 1266 } 1267 catch (Exception ex) 1268 { 1269 log.warn("Failed to parse port: " + discoveryGroupPort, ex); 1270 } 1271 } 1272 else 1273 { 1274 group = discoveryGroupPort.substring(0, colon); 1276 String portStr = discoveryGroupPort.substring(colon + 1); 1277 try 1278 { 1279 port = Integer.parseInt(portStr); 1280 } 1281 catch (Exception ex) 1282 { 1283 log.warn("Failed to parse port: " + portStr, ex); 1284 } 1285 } 1286 } 1287 1288 iaGroup = InetAddress.getByName(group); 1289 String localAddrStr = (String ) serverEnv.get(JNP_LOCAL_ADDRESS); 1290 String localPortStr = (String ) serverEnv.get(JNP_LOCAL_PORT); 1291 int localPort = 0; 1292 if (localPortStr != null) 1293 localPort = Integer.parseInt(localPortStr); 1294 if (localAddrStr != null) 1295 { 1296 InetSocketAddress localAddr = new InetSocketAddress (localAddrStr, localPort); 1297 s = new MulticastSocket (localAddr); 1298 } 1299 else 1300 { 1301 s = new MulticastSocket (localPort); 1302 } 1303 s.setSoTimeout(timeout); 1304 s.setTimeToLive(ttl); 1305 if(log.isTraceEnabled()) 1306 log.trace("TTL on multicast discovery socket is " + ttl); 1307 s.joinGroup(iaGroup); 1308 if (trace) 1309 log.trace("MulticastSocket: " + s); 1310 DatagramPacket packet; 1311 StringBuffer data = new StringBuffer ("GET_ADDRESS"); 1313 if (partitionName != null) 1314 data.append(":" + partitionName); 1315 byte[] buf = data.toString().getBytes(); 1316 packet = new DatagramPacket (buf, buf.length, iaGroup, port); 1317 if (trace) 1318 log.trace("Sending discovery packet(" + data + ") to: " + iaGroup + ":" + port); 1319 s.send(packet); 1320 1323 buf = new byte[50]; 1324 packet = new DatagramPacket (buf, buf.length); 1325 s.receive(packet); 1326 String myServer = new String (packet.getData()).trim(); 1327 if (trace) 1328 log.trace("Received answer packet: " + myServer); 1329 while (myServer != null && myServer.startsWith("GET_ADDRESS")) 1330 { 1331 Arrays.fill(buf, (byte) 0); 1332 packet.setLength(buf.length); 1333 s.receive(packet); 1334 byte[] reply = packet.getData(); 1335 myServer = new String (reply).trim(); 1336 if (trace) 1337 log.trace("Received answer packet: " + myServer); 1338 } 1339 String serverHost; 1340 int serverPort; 1341 1342 int colon = myServer.indexOf(':'); 1343 if (colon >= 0) 1344 { 1345 serverHost = myServer.substring(0, colon); 1346 serverPort = Integer.valueOf(myServer.substring(colon + 1)).intValue(); 1347 server = getServer(serverHost, serverPort, serverEnv); 1348 } 1349 return server; 1350 } 1351 catch (IOException e) 1352 { 1353 if (trace) 1354 log.trace("Discovery failed", e); 1355 NamingException ex = new CommunicationException (e.getMessage()); 1356 ex.setRootCause(e); 1357 throw ex; 1358 } 1359 finally 1360 { 1361 try 1362 { 1363 if (s != null) 1364 s.leaveGroup(iaGroup); 1365 } 1366 catch (Exception ignore) 1367 { 1368 } 1369 try 1370 { 1371 if (s != null) 1372 s.close(); 1373 } 1374 catch (Exception ignore) 1375 { 1376 } 1377 } 1378 } 1379 1380 private void checkRef(Hashtable refEnv) 1381 throws NamingException 1382 { 1383 if (naming == null) 1384 { 1385 String host = "localhost"; 1386 int port = 1099; 1387 Exception serverEx = null; 1388 1389 String urls = (String ) refEnv.get(Context.PROVIDER_URL); 1391 if (urls != null && urls.length() > 0) 1392 { 1393 StringTokenizer tokenizer = new StringTokenizer (urls, ","); 1394 1395 while (naming == null && tokenizer.hasMoreElements()) 1396 { 1397 String url = tokenizer.nextToken(); 1398 Name urlAsName = getNameParser("").parse(url); 1400 String server = parseNameForScheme(urlAsName, null); 1401 if (server != null) 1402 url = server; 1403 int colon = url.indexOf(':'); 1404 if (colon < 0) 1405 { 1406 host = url; 1407 } 1408 else 1409 { 1410 host = url.substring(0, colon).trim(); 1411 try 1412 { 1413 port = Integer.parseInt(url.substring(colon + 1).trim()); 1414 } 1415 catch (Exception ex) 1416 { 1417 } 1419 } 1420 try 1421 { 1422 naming = getServer(host, port, refEnv); 1424 } 1425 catch (Exception e) 1426 { 1427 serverEx = e; 1428 log.debug("Failed to connect to " + host + ":" + port, e); 1429 } 1430 } 1431 1432 Exception discoveryFailure = null; 1434 if (naming == null) 1435 { 1436 try 1437 { 1438 naming = discoverServer(refEnv); 1439 } 1440 catch (Exception e) 1441 { 1442 discoveryFailure = e; 1443 if (serverEx == null) 1444 serverEx = e; 1445 } 1446 if (naming == null) 1447 { 1448 StringBuffer buffer = new StringBuffer (50); 1449 buffer.append("Could not obtain connection to any of these urls: ").append(urls); 1450 if (discoveryFailure != null) 1451 buffer.append(" and discovery failed with error: ").append(discoveryFailure); 1452 CommunicationException ce = new CommunicationException (buffer.toString()); 1453 ce.setRootCause(serverEx); 1454 throw ce; 1455 } 1456 } 1457 } 1458 else 1459 { 1460 String jnpPartitionName = (String ) refEnv.get(JNP_PARTITION_NAME); 1468 if (jnpPartitionName != null) 1469 { 1470 naming = discoverServer(refEnv); 1473 if (naming == null) 1474 throw new ConfigurationException 1475 ("No valid context could be build for jnp.partitionName=" + jnpPartitionName); 1476 } 1477 else 1478 { 1479 naming = localServer; 1481 1482 if (naming == null) 1483 { 1484 naming = discoverServer(refEnv); 1485 if (naming == null) 1486 throw new ConfigurationException ("No valid Context.PROVIDER_URL was found"); 1488 } 1489 } 1490 } 1491 } 1492 } 1493 1494 private Name getAbsoluteName(Name n) 1495 throws NamingException 1496 { 1497 if (n.isEmpty()) 1498 return composeName(n, prefix); 1499 else if (n.get(0).toString().equals("")) return n.getSuffix(1); 1501 else return composeName(n, prefix); 1503 } 1504 1505 private Hashtable getEnv(Name n) 1506 throws InvalidNameException 1507 { 1508 Hashtable nameEnv = env; 1509 env.remove(JNP_PARSED_NAME); 1510 String serverInfo = parseNameForScheme(n, nameEnv); 1511 if (serverInfo != null) 1512 { 1513 nameEnv = (Hashtable ) env.clone(); 1515 nameEnv.put(Context.PROVIDER_URL, serverInfo); 1516 } 1517 return nameEnv; 1518 } 1519 1520 } 1522 | Popular Tags |