KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > command > CommandProcessor


1 package org.sapia.ubik.rmi.server.command;
2
3 import org.sapia.ubik.net.ServerAddress;
4 import org.sapia.ubik.rmi.server.Log;
5 import org.sapia.ubik.rmi.server.VmId;
6
7
8 /**
9  * An instance of this class serves as an entry-point for command
10  * objects.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <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>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class CommandProcessor {
20   private ExecQueue _in;
21
22   /**
23    * Constructor for CommandProcessor.
24    */

25   public CommandProcessor(int maxThreads) throws IllegalStateException JavaDoc {
26     try {
27       _in = new InQueue(maxThreads);
28     } catch (Exception JavaDoc e) {
29       e.printStackTrace();
30       throw new IllegalStateException JavaDoc(e.getMessage());
31     }
32   }
33
34   /**
35    * Processes this command in the same thread as the caller's.
36    *
37    * @param cmd a <code>Command</code>.
38    * @return the passed in command return value.
39    */

40   public Object JavaDoc processSyncCommand(Command cmd) {
41     try {
42       return cmd.execute();
43     } catch (Throwable JavaDoc t) {
44       return t;
45     }
46   }
47
48   /**
49    * Processes the given command asynchronously.
50    *
51    * @param cmdId a command's unique identifier.
52    * @param from the <code>ServerAddress</code> from which this command originates.
53    * @param cmd the command to execute.
54    */

55   public void processAsyncCommand(String JavaDoc cmdId, VmId caller,
56     ServerAddress from, Command cmd) {
57     _in.add(new AsyncCommand(cmdId, caller, from, cmd));
58   }
59
60   /**
61    * Sets this processor's "response sender". A response sender is, as the name
62    * implies, in charge of returing a command's return value to the originator of
63    * the command.
64    *
65    * @param a <code>ResponseSender</code>.
66    */

67   public void setResponseSender(ResponseSender sender) {
68     OutQueue.setResponseSender(sender);
69   }
70
71   /**
72    * Shuts down this instance. Blocks until no commands are left to execute,
73    * or until the given timeout is reached.
74    * <p>
75    * This method also internally calls shutdownAll() on the <code>OutQueue</code>
76    * singleton.
77    *
78    * @param timeout a timout, in millis.
79    * @throws InterruptedException if the calling thread is interrupted while
80    * blocking within this method.
81    */

82   public void shutdown(long timeout) throws InterruptedException JavaDoc {
83     Log.warning(getClass(), "Shutting down incoming command queue");
84     _in.shutdown(timeout);
85     Log.warning(getClass(), "Shutting down outgoing response queue");
86     OutQueue.shutdownAll(timeout);
87   }
88 }
89
Popular Tags