1 19 package org.objectweb.carol.cmi; 20 21 import java.net.MalformedURLException ; 22 import java.rmi.AlreadyBoundException ; 23 import java.rmi.NotBoundException ; 24 import java.rmi.Remote ; 25 import java.rmi.RemoteException ; 26 27 public final class Naming { 28 private Naming() { 29 } 30 31 private static ClusterRegistryInternal getRegistry(String host, int port) 32 throws RemoteException { 33 ClusterRegistryInternal r = 34 (ClusterRegistryInternal) LowerOrb.getRegistryStub( 35 "org.objectweb.carol.cmi.ClusterRegistryImpl", 36 host, 37 port); 38 return r; 39 } 40 41 public static ClusterRegistry getRegistry(NamingContextHostPort[] hp) 42 throws RemoteException { 43 int n = hp.length; 44 if (n == 0) return null; 45 ClusterRegistryInternal r = getRegistry(hp[0].host, hp[0].port); 46 if (n == 1) return new ClusterRegistryClient(r); 47 ClusterStubData csd = new ClusterStubData(r); 48 for (int i = 1; i < n; i++) { 49 csd.setStub(getRegistry(hp[i].host, hp[i].port)); 50 } 51 return new ClusterRegistryClient((ClusterRegistryInternal) csd.getClusterStub()); 52 } 53 54 public static ClusterRegistry getLocalRegistry(NamingContextHostPort[] hp) 55 throws MalformedURLException , RemoteException { 56 if (hp.length > 1) 57 throw new MalformedURLException ("Can not bind or unbind in multiple machines"); 58 ClusterRegistry creg = getRegistry(hp); 59 return creg; 60 } 61 62 public static Object lookup(String name) 63 throws MalformedURLException , NotBoundException , RemoteException { 64 NamingContext nc = new NamingContext(name); 65 ClusterRegistry reg = getRegistry(nc.hp); 66 if (nc.name.length() == 0) 67 return reg; 68 return reg.lookup(nc.name); 69 } 70 71 public static void bind(String name, Remote obj) 72 throws MalformedURLException , AlreadyBoundException , RemoteException { 73 NamingContext nc = new NamingContext(name); 74 ClusterRegistry reg = getLocalRegistry(nc.hp); 75 if (obj == null) 76 throw new NullPointerException ("cannot bind null object"); 77 reg.bind(nc.name, obj); 78 } 79 80 public static void rebind(String name, Remote obj) 81 throws MalformedURLException , RemoteException { 82 NamingContext nc = new NamingContext(name); 83 ClusterRegistry reg = getLocalRegistry(nc.hp); 84 if (obj == null) 85 throw new NullPointerException ("cannot bind null object"); 86 reg.rebind(nc.name, obj); 87 } 88 89 public static void unbind(String name) 90 throws MalformedURLException , NotBoundException , RemoteException { 91 NamingContext nc = new NamingContext(name); 92 ClusterRegistry reg = getLocalRegistry(nc.hp); 93 reg.unbind(nc.name); 94 } 95 96 public static String [] list(String name) 97 throws MalformedURLException , RemoteException { 98 NamingContext nc = new NamingContext(name); 99 ClusterRegistry reg = getRegistry(nc.hp); 100 101 String prefix = nc.scheme.equals("") ? "" : nc.scheme + ":"; 102 prefix += "//"; 103 int i = 0; 104 while (i < nc.hp.length) { 105 prefix += nc.hp[i].host; 106 if (nc.hp[i].port != LowerOrb.DEFAULT_CREG_PORT) 107 prefix += ":" + nc.hp[i].port; 108 i++; 109 if (i < nc.hp.length) 110 prefix += ","; 111 } 112 prefix += "/"; 113 114 String lst[] = reg.list(); 115 for (i = 0; i < lst.length; i++) 116 lst[i] = prefix + lst[i]; 117 return lst; 118 } 119 } 120 | Popular Tags |