KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.sapia.ubik.rmi.server;
2
3 import org.sapia.taskman.TaskManager;
4 import org.sapia.ubik.rmi.Consts;
5 import org.sapia.ubik.rmi.interceptor.Event;
6 import org.sapia.ubik.rmi.interceptor.Interceptor;
7 import org.sapia.ubik.rmi.interceptor.InvalidInterceptorException;
8 import org.sapia.ubik.rmi.interceptor.MultiDispatcher;
9 import org.sapia.ubik.rmi.server.gc.ServerGC;
10 import org.sapia.ubik.rmi.server.invocation.RMICommandProcessor;
11
12
13 /**
14  * Implements the server-side behavior of RMI.
15  *
16  * @author Yanick Duchesne
17  * <dl>
18  * <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>
19  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
20  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
21  * </dl>
22  */

23 public class ServerRuntime {
24   static final int DEFAULT_MAX_CALLBACK_THREADS = 1;
25
26   /**
27    * Holds <code>OID</code>-to-object mappings. Keeps distributed object references
28    * so that the latter are spared from the VM GC while remote applications might still
29    * access to the said references.
30    */

31   public final ObjectTable objectTable = new ObjectTable();
32
33   /** Holds <code>OID</code>-to-object mappings. */
34   /**
35    * The dispatcher of events destined to be intercepted by <code>Interceptor</code> instances.
36    * Dispatches server-side events. This mechanism can conveniently be used by client apps
37    * to dispatch their own custom events.
38    */

39   public final MultiDispatcher dispatcher = new MultiDispatcher();
40
41   /**
42    * The object implementing the server-side distributed GC mechanism.
43    */

44   public final ServerGC gc;
45
46   /**
47    * The processor handling incoming <code>RMICommand</code> instances.
48    *
49    * @see RMICommand
50    */

51   public final RMICommandProcessor processor;
52
53   /**
54    * The single <code>Server</code> held by this instance.
55    */

56   public final ServerTable server = new ServerTable();
57
58   ServerRuntime(TaskManager taskman) {
59     int maxThreads = DEFAULT_MAX_CALLBACK_THREADS;
60
61     if (System.getProperty(Consts.SERVER_CALLBACK_MAX_THREADS) != null) {
62       try {
63         if ((System.getProperty(Consts.CALLBACK_ENABLED) != null) &&
64               System.getProperty(Consts.CALLBACK_ENABLED).equalsIgnoreCase("true")) {
65           maxThreads = Integer.parseInt(System.getProperty(
66                 Consts.SERVER_CALLBACK_MAX_THREADS));
67         } else {
68           maxThreads = 1;
69         }
70       } catch (NumberFormatException JavaDoc e) {
71         Log.warning(ServerRuntime.class,
72           "invalid value for system property: " +
73           Consts.SERVER_CALLBACK_MAX_THREADS + "; using default: " +
74           maxThreads);
75       }
76     }
77
78     processor = new RMICommandProcessor(maxThreads);
79     gc = new ServerGC(taskman);
80   }
81
82   /**
83    * Adds an interceptor of server-side events to this instance.
84    *
85    * @see Interceptor
86    * @see MultiDispatcher#addInterceptor(Class, Interceptor)
87    */

88   public synchronized void addInterceptor(Class JavaDoc eventClass, Interceptor it)
89     throws InvalidInterceptorException {
90     dispatcher.addInterceptor(eventClass, it);
91   }
92
93   /**
94    * Dispatches the given event to the underlying server-side interceptors.
95    *
96    * @see MultiDispatcher#dispatch(Event)
97    */

98   public void dispatchEvent(Event event) {
99     dispatcher.dispatch(event);
100   }
101
102   /**
103    * Shuts down this instance.
104    */

105   void shutdown(long timeout) throws InterruptedException JavaDoc {
106     Log.warning(ServerRuntime.class, "Shutting down command processor");
107     processor.shutdown(timeout);
108     Log.warning(ServerRuntime.class, "Shutting down server");
109     server.close();
110   }
111 }
112
Popular Tags