KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import org.sapia.taskman.TaskManager;
7 import org.sapia.ubik.net.ServerAddress;
8 import org.sapia.ubik.rmi.Consts;
9 import org.sapia.ubik.rmi.interceptor.Event;
10 import org.sapia.ubik.rmi.interceptor.Interceptor;
11 import org.sapia.ubik.rmi.interceptor.InvalidInterceptorException;
12 import org.sapia.ubik.rmi.interceptor.MultiDispatcher;
13 import org.sapia.ubik.rmi.server.gc.ClientGC;
14 import org.sapia.ubik.rmi.server.invocation.InvocationDispatcher;
15
16
17 /**
18  * Implements Ubik RMI's client-side runtime environment. This class' main
19  * task is to manage remote references and client-side interceptors.
20  *
21  * @author Yanick Duchesne
22  * <dl>
23  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
24  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
25  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
26  * </dl>
27  */

28 public class ClientRuntime {
29   /**
30    * The dispatcher of method invocations to remote servers.
31    */

32   static final InvocationDispatcher invoker = new InvocationDispatcher();
33
34   /**
35    * Address of the singleton server that will receive call-backs.
36    *
37    * @see Singleton
38    */

39   private static Map JavaDoc _serverAddresses = new HashMap JavaDoc();
40
41   /**
42    * The dispatcher of events destined to be intercepted by <code>Interceptor</code> instances.
43    * Dispatches client-side events. This mechanism can conveniently be used by client apps
44    * to dispatch their own custom events.
45    *
46    * @see Interceptor
47    */

48   public final MultiDispatcher dispatcher = new MultiDispatcher();
49
50   /** The client-side part of the distributed garbage-collection mechanism */
51   final ClientGC gc;
52
53   ClientRuntime(TaskManager taskman) {
54     gc = new ClientGC(taskman);
55   }
56
57   /**
58    * Returns <code>true</code> if this "client" is call-back enabled (if
59    * it itself is a server supporting responses from remote servers in
60    * the for of call-backs.
61    *
62    * @see Consts#CALLBACK_ENABLED
63    *
64    * @return <code>true</code> if this server supports call-back mode.
65    */

66   boolean isCallback(String JavaDoc transportType) {
67     if (_serverAddresses.get(transportType) == null) {
68       doInit(transportType);
69     }
70
71     return _serverAddresses.get(transportType) != null;
72   }
73
74   void shutdown(long timeout) throws InterruptedException JavaDoc {
75     //Log.warning(ClientRuntime.class, "Shutting down GC");
76
}
77
78   /**
79    * Returns this client's call-back server address (corresponds to the
80    * this client's singleton server, which will process incoming responses).
81    *
82    * @see Consts#CALLBACK_ENABLED
83    *
84    * @return this client's <code>ServerAddress</code> for call-backs.
85    * @throws IllegalStateException if this client does not allow call-backs.
86    */

87   public ServerAddress getCallbackAddress(String JavaDoc transportType)
88     throws IllegalStateException JavaDoc {
89     if (_serverAddresses.get(transportType) == null) {
90       throw new IllegalStateException JavaDoc(
91         "no callback server was instantiated; make sure " +
92         "the following system property is set to 'true' upon VM startup: " +
93         Consts.CALLBACK_ENABLED);
94     }
95
96     return (ServerAddress) _serverAddresses.get(transportType);
97   }
98
99   /**
100    * Adds a client-side interceptor to this client.
101    *
102    * @see Interceptor
103    * @see MultiDispatcher#addInterceptor(Class, Interceptor)
104    */

105   public synchronized void addInterceptor(Class JavaDoc eventClass, Interceptor it)
106     throws InvalidInterceptorException {
107     dispatcher.addInterceptor(eventClass, it);
108   }
109
110   /**
111    * Dispatches the given event to registered interceptors.
112    *
113    * @see Interceptor
114    * @see MultiDispatcher#addInterceptor(Class, Interceptor)
115    */

116   public void dispatchEvent(Event event) {
117     dispatcher.dispatch(event);
118   }
119
120   /***
121    * Performs initialization of this instance.
122    */

123   synchronized void doInit(String JavaDoc transportType) {
124     if ((System.getProperty(Consts.CALLBACK_ENABLED) != null) &&
125           System.getProperty(Consts.CALLBACK_ENABLED).equalsIgnoreCase("true")) {
126       if (_serverAddresses.get(transportType) != null) {
127         return;
128       }
129
130       Log.warning(getClass(),
131         "Creating server to receive callbacks on transport: " + transportType);
132
133       ServerAddress serverAddress = null;
134
135       if (Hub.serverRuntime.server.isInit(transportType)) {
136         serverAddress = Hub.serverRuntime.server.getServerAddress(transportType);
137       } else {
138         try {
139           serverAddress = Hub.serverRuntime.server.init(transportType);
140         } catch (java.rmi.RemoteException JavaDoc e) {
141           Log.error(ClientRuntime.class, e);
142         }
143       }
144
145       _serverAddresses.put(transportType, serverAddress);
146     }
147   }
148 }
149
Popular Tags