1 19 package org.objectweb.carol.cmi; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.rmi.AlreadyBoundException ; 24 import java.rmi.NotBoundException ; 25 import java.rmi.Remote ; 26 import java.rmi.RemoteException ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 32 import org.objectweb.carol.util.configuration.TraceCarol; 33 34 public final class ClusterRegistryImpl implements ClusterRegistryInternal { 35 39 private HashMap lreg = new HashMap (); 40 public static final String REG_PREFIX = "REG_"; 41 42 private static Object lock = new Object (); 44 private boolean started = false; 45 46 private ClusterRegistryImpl() throws RemoteException { 47 synchronized (lock) { 48 if (started) throw new RemoteException ("Can't start multiple cluster registries in the same JVM"); 50 started = true; 51 } 52 } 53 54 public static ClusterRegistryKiller start(int port) 55 throws RemoteException { 56 if (TraceCarol.isDebugCmiRegistry()) 57 TraceCarol.debugCmiRegistry("registry starting on port " + port); 58 ClusterRegistryImpl creg = new ClusterRegistryImpl(); 59 ClusterRegistryInternal stub = 60 (ClusterRegistryInternal) LowerOrb.exportRegistry(creg, port); 61 ClusterRegistryKiller k = new ClusterRegistryKiller(creg, port); 62 return k; 63 } 64 65 public Object lookup(String n) throws NotBoundException , RemoteException { 66 Object obj; 67 synchronized (lreg) { 68 obj = lreg.get(n); 69 } 70 if (obj != null) { 71 if (TraceCarol.isDebugCmiRegistry()) 72 TraceCarol.debugCmiRegistry("local lookup of " + n); 73 return obj; 74 } 75 try { 76 if (TraceCarol.isDebugCmiRegistry()) 77 TraceCarol.debugCmiRegistry("global lookup of " + n); 78 ClusterStubData csd = DistributedEquiv.getGlobal(REG_PREFIX + n); 79 if (csd != null) { 80 if (TraceCarol.isDebugCmiRegistry()) 81 TraceCarol.debugCmiRegistry("returned a cluster stub"); 82 ByteArrayOutputStream b = new ByteArrayOutputStream (); 83 b.write(CLUSTERED); 84 CmiOutputStream os = new CmiOutputStream(b); 85 csd.write(os); 86 os.flush(); 87 return b.toByteArray(); 88 } 89 } catch (ConfigException e) { 90 throw new RemoteException (e.toString()); 91 } catch (IOException e) { 92 throw new RemoteException (e.toString()); 93 } 94 throw new NotBoundException (n); 95 } 96 97 public void bindSingle(String n, Remote obj) 98 throws AlreadyBoundException , RemoteException { 99 104 Object cur; 105 if (TraceCarol.isDebugCmiRegistry()) 106 TraceCarol.debugCmiRegistry("Local bind of " + n); 107 synchronized (lreg) { 108 cur = lreg.get(n); 109 if (cur != null) 110 throw new AlreadyBoundException (n); 111 lreg.put(n, obj); 112 } 113 } 114 115 public void rebindSingle(String n, Remote obj) throws RemoteException { 116 121 if (TraceCarol.isDebugCmiRegistry()) 122 TraceCarol.debugCmiRegistry("Local rebind of " + n); 123 synchronized (lreg) { 124 lreg.put(n, obj); 125 } 126 } 127 128 public void bindCluster(String n, byte[] obj) 129 throws AlreadyBoundException , RemoteException { 130 Object cur; 131 if (TraceCarol.isDebugCmiRegistry()) 132 TraceCarol.debugCmiRegistry("Global bind of " + n); 133 synchronized (lreg) { 134 try { 135 if (!DistributedEquiv.exportObject(REG_PREFIX + n, obj)) { 136 throw new AlreadyBoundException (n); 137 } 138 } catch (ConfigException e) { 139 throw new RemoteException (e.toString()); 140 } 141 } 142 } 143 144 public void unbind(String n) 145 throws NotBoundException , RemoteException { 146 Object obj; 147 synchronized (lreg) { 148 obj = lreg.remove(n); 149 if (obj != null) { 150 if (TraceCarol.isDebugCmiRegistry()) { 151 TraceCarol.debugCmiRegistry("Local unbind of " + n); 152 } 153 return; 154 } 155 if (TraceCarol.isDebugCmiRegistry()) 156 TraceCarol.debugCmiRegistry("Global unbind of " + n); 157 try { 158 if (!DistributedEquiv.unexportObject(REG_PREFIX + n)) { 159 throw new NotBoundException (n); 160 } 161 } catch (ConfigException e) { 162 throw new RemoteException (e.toString()); 163 } 164 } 165 } 166 167 public void rebindCluster(String n, byte[] obj) throws RemoteException { 168 Object cur; 169 ClusterConfig cc = null; 170 if (TraceCarol.isDebugCmiRegistry()) 171 TraceCarol.debugCmiRegistry("Global rebind of " + n); 172 173 synchronized (lreg) { 174 try { 175 String name = REG_PREFIX + n; 176 DistributedEquiv.unexportObject(name); 177 DistributedEquiv.exportObject(name, obj); 178 } catch (ConfigException e) { 179 throw new RemoteException (e.toString()); 180 } 181 } 182 } 183 184 public String [] list() throws RemoteException { 185 Set s = new TreeSet (); 186 try { 187 Set global = DistributedEquiv.keySet(); 188 Iterator it = global.iterator(); 189 while (it.hasNext()) { 190 Object o = it.next(); 191 if (o instanceof String ) { 192 String str = (String ) o; 193 if (str.startsWith(REG_PREFIX)) { 194 s.add(str.substring(4)); 195 } 196 } 197 } 198 } catch (ConfigException e) { 199 throw new RemoteException (e.toString()); 200 } 201 synchronized (lreg) { 202 Set local = lreg.keySet(); 203 Iterator it = local.iterator(); 204 while (it.hasNext()) { 205 s.add(it.next()); 206 } 207 } 208 int n = s.size(); 209 Iterator it = s.iterator(); 210 String [] tab = new String [n]; 211 for (int i = 0; i < n; i++) { 212 tab[i] = (String ) it.next(); 213 } 214 return tab; 215 } 216 217 public void test() throws RemoteException { 218 } 219 220 public static void main(String args[]) throws Exception { 221 DistributedEquiv.start(); 222 start(Integer.parseInt(args[0])); 223 System.out.println("Cluster service started"); 224 while (true) { 225 try { 226 Thread.sleep(Long.MAX_VALUE); 227 } catch (InterruptedException e) { 228 } 229 } 230 } 231 } 232 | Popular Tags |