1 19 package org.objectweb.carol.cmi; 20 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.rmi.AlreadyBoundException ; 25 import java.rmi.NotBoundException ; 26 import java.rmi.Remote ; 27 import java.rmi.RemoteException ; 28 29 import org.objectweb.carol.util.configuration.TraceCarol; 30 31 34 public class ClusterRegistryClient implements ClusterRegistry { 35 private ClusterRegistryInternal cr; 36 37 public ClusterRegistryClient(ClusterRegistryInternal cr) 39 throws RemoteException { 40 this.cr = cr; 41 } 42 43 public String [] list() throws RemoteException { 44 return cr.list(); 45 } 46 47 public void test() throws RemoteException { 48 cr.test(); 49 } 50 51 public Remote lookup(String name) 52 throws NotBoundException , RemoteException { 53 Object obj = cr.lookup(name); 54 if (obj instanceof Remote ) { 55 return (Remote )obj; 56 } 57 byte[] buf = (byte[])obj; 58 ByteArrayInputStream inStream = new ByteArrayInputStream (buf); 59 int type = inStream.read(); 60 try { 61 CmiInputStream in = new CmiInputStream(inStream); 62 if (type == ClusterRegistryInternal.CLUSTERED) { 63 return ClusterStubData.read(in, null).getClusterStub(); 64 } else { 65 return (Remote ) in.readObject(); 66 } 67 } catch (IOException e) { 68 throw new RemoteException (e.toString()); 69 } catch (ClassNotFoundException e) { 70 throw new RemoteException (e.toString()); 71 } 72 } 73 74 private byte[] serialize(Remote obj) throws RemoteException { 75 ByteArrayOutputStream outStream = new ByteArrayOutputStream (); 76 CmiOutputStream out; 77 try { 78 out = new CmiOutputStream(outStream); 79 out.writeObject(obj); 80 out.flush(); 81 return outStream.toByteArray(); 82 } catch (IOException e1) { 83 throw new RemoteException (e1.toString()); 84 } 85 } 86 87 public void bind(String name, Remote obj) 88 throws AlreadyBoundException , RemoteException { 89 ClusterConfig cc = null; 90 try { 91 cc = ClusterObject.getClusterConfig(obj); 92 if (TraceCarol.isDebugCmiRegistry()) 93 TraceCarol.debugCmiRegistry("Global bind of " + name); 94 } catch (Exception e) { 95 if (TraceCarol.isDebugCmiRegistry()) 96 TraceCarol.debugCmiRegistry("Local bind of " + name); 97 } 98 if (cc == null) { 99 cr.bindSingle(name, obj); 100 } else { 101 if (!cc.isGlobalAtBind()) { 102 throw new RemoteException ("not implemented"); 103 } 104 obj = LowerOrb.toStub(obj); 105 byte[] ser = serialize(obj); 106 cr.bindCluster(name, ser); 107 } 108 } 109 110 public void rebind(String name, Remote obj) throws RemoteException { 111 ClusterConfig cc = null; 112 try { 113 cc = ClusterObject.getClusterConfig(obj); 114 if (TraceCarol.isDebugCmiRegistry()) 115 TraceCarol.debugCmiRegistry("Global bind of " + name); 116 } catch (Exception e) { 117 if (TraceCarol.isDebugCmiRegistry()) 118 TraceCarol.debugCmiRegistry("Local bind of " + name); 119 } 120 if (cc == null) { 121 cr.rebindSingle(name, obj); 122 } else { 123 if (!cc.isGlobalAtBind()) { 124 throw new RemoteException ("not implemented"); 125 } 126 obj = LowerOrb.toStub(obj); 127 byte[] ser = serialize(obj); 128 cr.rebindCluster(name, ser); 129 } 130 } 131 132 public void unbind(String name) throws NotBoundException , RemoteException { 133 cr.unbind(name); 134 } 135 } 136 | Popular Tags |