1 package org.sapia.ubik.rmi.naming.remote.proxy; 2 3 import java.io.Externalizable ; 4 import java.io.IOException ; 5 import java.io.ObjectInput ; 6 import java.io.ObjectOutput ; 7 import java.lang.ref.SoftReference ; 8 import java.lang.reflect.InvocationHandler ; 9 import java.lang.reflect.Proxy ; 10 import java.util.ArrayList ; 11 import java.util.Collections ; 12 import java.util.List ; 13 14 import javax.naming.Context ; 15 import javax.naming.NamingException ; 16 17 import org.sapia.ubik.mcast.DomainName; 18 import org.sapia.ubik.rmi.server.Stub; 19 import org.sapia.ubik.rmi.server.StubInvocationHandler; 20 21 22 31 public class BindingCache implements Externalizable { 32 private List _services = Collections.synchronizedList(new ArrayList ()); 33 34 public BindingCache() { 35 } 36 37 public synchronized void add(String domainName, String name, Object o) { 38 _services.add(new BoundRef(domainName, name, o)); 39 } 40 41 public synchronized List cachedRefs() { 42 return _services; 43 } 44 45 public void copyTo(Context ctx, DomainName domain) { 46 BoundRef ref; 47 48 for (int i = 0; i < _services.size(); i++) { 49 ref = (BoundRef) _services.get(i); 50 51 if (ref.obj == null) { 52 continue; 53 } 54 55 try { 56 if ((ref.domainName != null) && domain.contains(ref.domainName)) { 57 ctx.rebind(ref.name, ref.obj); 58 } 59 } catch (NamingException e) { 60 } 62 } 63 } 64 65 public void readExternal(ObjectInput in) 66 throws IOException , ClassNotFoundException { 67 _services = (List ) in.readObject(); 68 } 69 70 public void writeExternal(ObjectOutput out) throws IOException { 71 BoundRef ref; 72 73 synchronized (_services) { 74 for (int i = 0; i < _services.size(); i++) { 75 ref = (BoundRef) _services.get(i); 76 77 if (ref.isNull()) { 78 _services.remove(i); 79 --i; 80 } 81 } 82 } 83 84 out.writeObject(_services); 85 } 86 87 public static class BoundRef implements Externalizable { 88 public String name; 89 public Object obj; 90 public DomainName domainName; 91 92 95 public BoundRef() { 96 } 97 98 BoundRef(String domainName, String name, Object o) { 99 this.domainName = DomainName.parse(domainName); 100 this.name = name; 101 this.obj = new SoftReference (o); 102 } 103 104 public void readExternal(ObjectInput in) 105 throws IOException , ClassNotFoundException { 106 this.name = in.readUTF(); 107 this.obj = in.readObject(); 108 this.domainName = (DomainName) in.readObject(); 109 } 110 111 public void writeExternal(ObjectOutput out) throws IOException { 112 out.writeUTF(name); 113 114 Object toWrite; 115 116 if (obj instanceof SoftReference ) { 117 toWrite = ((SoftReference ) obj).get(); 118 } else { 119 toWrite = obj; 120 } 121 122 if (toWrite instanceof Stub && Proxy.isProxyClass(toWrite.getClass())) { 123 InvocationHandler handler = Proxy.getInvocationHandler(toWrite); 124 125 if (handler instanceof StubInvocationHandler) { 126 toWrite = ((StubInvocationHandler) handler).toStubContainer(toWrite); 127 } 128 } 129 130 out.writeObject(toWrite); 131 out.writeObject(domainName); 132 } 133 134 public boolean isNull() { 135 if (obj == null) { 136 return true; 137 } 138 139 if (obj instanceof SoftReference ) { 140 return ((SoftReference ) obj).get() == null; 141 } 142 143 return false; 144 } 145 } 146 } 147 | Popular Tags |