| 1 6 7 package org.sapia.ubik.rmi.server; 8 9 import java.io.IOException ; 10 import java.lang.ref.SoftReference ; 11 import java.lang.reflect.Proxy ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.sapia.ubik.mcast.AsyncEventListener; 19 import org.sapia.ubik.mcast.EventChannel; 20 import org.sapia.ubik.mcast.RemoteEvent; 21 import org.sapia.ubik.rmi.naming.remote.archie.SyncPutEvent; 22 23 29 public class StatelessStubTable implements AsyncEventListener{ 30 31 private static Map _stubs = Collections.synchronizedMap(new HashMap ()); 32 private static StatelessStubTable _instance = new StatelessStubTable(); 33 34 35 StatelessStubTable() { 36 } 37 38 41 public static synchronized void registerStatelessRef(RemoteRefStateless ref){ 42 if(Log.isInfo()) 43 Log.info(StatelessStubTable.class, "Registering stateless stub"); 44 45 EventChannel channel = EventChannelSingleton.getEventChannelFor(ref._domain, ref._mcastAddress, ref._mcastPort); 46 47 if(!channel.containsAsyncListener(_instance)){ 48 channel.registerAsyncListener(SyncPutEvent.class.getName(), _instance); 49 } 50 ref.clean(); 51 doRegister(ref); 52 } 53 54 public void onAsyncEvent(RemoteEvent evt) { 55 try { 56 57 if(Log.isInfo()) 58 Log.info(getClass(), "Remote binding event received: " + evt.getType()); 59 SyncPutEvent bEvt = (SyncPutEvent) evt.getData(); 60 61 Object bound; 62 try { 63 bound = bEvt.getValue(); 64 } catch (IOException e) { 65 Log.error(getClass(), "Error receiving bound object", e); 66 67 return; 68 } catch (ClassNotFoundException e) { 69 e.printStackTrace(); 70 Log.error(getClass(), "Error receiving bound object", e); 71 72 return; 73 } 74 75 StubInvocationHandler handler = null; 76 77 if (bound instanceof StubContainer) { 78 handler = ((StubContainer) bound).getStubInvocationHandler(); 79 } else if (Proxy.isProxyClass(bound.getClass()) && bound instanceof Stub) { 80 handler = (StubInvocationHandler) Proxy.getInvocationHandler(bound); 81 } 82 83 if ((handler != null) && handler instanceof RemoteRefStateless) { 84 RemoteRefStateless other = (RemoteRefStateless) handler; 85 86 synchronized(_stubs){ 87 List siblings = (List )_stubs.get(other._domain); 88 if(siblings == null){ 89 siblings = new ArrayList (); 90 _stubs.put(other._domain, siblings); 91 } 92 addSiblings(siblings, other); 93 } 94 95 } 96 } catch (IOException e) { 97 Log.error(getClass(), e); 98 } catch (RuntimeException e) { 99 Log.error(getClass(), e); 100 } 101 } 102 103 static synchronized void doRegister(RemoteRefStateless ref){ 104 synchronized(_stubs){ 105 List siblings = (List )_stubs.get(ref._domain); 106 if(siblings == null){ 107 siblings = new ArrayList (); 108 _stubs.put(ref._domain, siblings); 109 } 110 siblings.add(new SoftReference (ref)); 111 addSiblings(siblings, ref); 112 } 113 } 114 115 static List getSiblings(String domain){ 116 return (List )_stubs.get(domain); 117 } 118 119 private static void addSiblings(List siblings, RemoteRefStateless other){ 120 for(int i = 0; i < siblings.size(); i++){ 121 SoftReference ref = (SoftReference )siblings.get(i); 122 RemoteRefStateless remoteRef = (RemoteRefStateless)ref.get(); 123 if(remoteRef == null){ 124 siblings.remove(i--); 125 } 126 else{ 127 if(remoteRef._name.equals(other._name) && !remoteRef._oid.equals(other._oid)){ 128 Log.info(StatelessStubTable.class, "Binding received for name: " + other._name); 129 if(remoteRef.clean()){ 130 other.addSibling(remoteRef); 131 } 132 remoteRef.addSibling(other); 133 } 134 } 135 } 136 } 137 } 138 | Popular Tags |