KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > StatelessStubTable


1 /*
2  * StatelessStubTable.java
3  *
4  * Created on October 31, 2005, 11:14 AM
5  */

6
7 package org.sapia.ubik.rmi.server;
8
9 import java.io.IOException JavaDoc;
10 import java.lang.ref.SoftReference JavaDoc;
11 import java.lang.reflect.Proxy JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
24  * This class manages stateless stubs on the client-side and insures that notications
25  * are properly dispatched.
26  *
27  * @author yduchesne
28  */

29 public class StatelessStubTable implements AsyncEventListener{
30   
31   private static Map JavaDoc _stubs = Collections.synchronizedMap(new HashMap JavaDoc());
32   private static StatelessStubTable _instance = new StatelessStubTable();
33   
34   /** Creates a new instance of StatelessStubTable */
35   StatelessStubTable() {
36   }
37   
38   /**
39    * @param ref a <code>RemoteRefStateless</code> that will be internally kept.
40    */

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 JavaDoc bound;
62       try {
63         bound = bEvt.getValue();
64       } catch (IOException JavaDoc e) {
65         Log.error(getClass(), "Error receiving bound object", e);
66
67         return;
68       } catch (ClassNotFoundException JavaDoc 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 JavaDoc siblings = (List JavaDoc)_stubs.get(other._domain);
88           if(siblings == null){
89             siblings = new ArrayList JavaDoc();
90             _stubs.put(other._domain, siblings);
91           }
92           addSiblings(siblings, other);
93         }
94
95       }
96     } catch (IOException JavaDoc e) {
97       Log.error(getClass(), e);
98     } catch (RuntimeException JavaDoc e) {
99       Log.error(getClass(), e);
100     }
101   }
102   
103   static synchronized void doRegister(RemoteRefStateless ref){
104     synchronized(_stubs){
105       List JavaDoc siblings = (List JavaDoc)_stubs.get(ref._domain);
106       if(siblings == null){
107         siblings = new ArrayList JavaDoc();
108         _stubs.put(ref._domain, siblings);
109       }
110       siblings.add(new SoftReference JavaDoc(ref));
111       addSiblings(siblings, ref);
112     }
113   }
114   
115   static List JavaDoc getSiblings(String JavaDoc domain){
116     return (List JavaDoc)_stubs.get(domain);
117   }
118   
119   private static void addSiblings(List JavaDoc siblings, RemoteRefStateless other){
120     for(int i = 0; i < siblings.size(); i++){
121       SoftReference JavaDoc ref = (SoftReference JavaDoc)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