KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtCommand


1
2
3 package Jt;
4 import java.util.*;
5 import java.lang.reflect.*;
6 import java.beans.*;
7 import java.io.*;
8
9 /**
10  * Jt Implementation of the Command pattern. This object inherits from JtThread.
11  * It can execute using a separate/independent thread and process messages (requests) asynchronously
12  * via a message queue.
13  */

14
15 public class JtCommand extends JtThread {
16   private JtList messageLog = new JtList ();
17   private boolean synchronous = true;
18
19
20   public JtCommand() {
21   }
22
23
24 /**
25   * Verifies if this object should process messages synchronously or asynchronously
26   * via a message queue and a separate thread.
27   */

28
29   public boolean getSynchronous () {
30     return (synchronous);
31   }
32
33 /**
34   * Specifies synchronous/asynchronous mode of processing messages.
35   */

36
37   public void setSynchronous (boolean synchronous) {
38     this.synchronous = synchronous;
39   }
40
41 /**
42   * Returns the message (request) log. This is basically the list of messages processed
43   * by this object.
44   */

45   public JtList getMessageLog () {
46     return (messageLog);
47   }
48
49
50 /**
51   * Changes the message (request) log.
52   */

53
54   public void setMessageLog (JtList messageLog) {
55
56     this.messageLog = messageLog;
57   }
58
59 /**
60   * Enqueues a message (request) if asynchronous mode is set. Otherwise process the message
61   * by calling processMesssage directly. sendMessage () calls this function when
62   * the target class is a subclass of JtThread.
63   */

64
65   synchronized public Object JavaDoc enqueueMessage (Object JavaDoc msg) // chec
66
{
67
68     if (synchronous)
69      return (processMessage (msg));
70     else
71      return (super.enqueueMessage (msg));
72
73   }
74
75
76 /**
77   * Log a message (request).
78   */

79
80   protected void logMessage (Object JavaDoc msg)
81   {
82     JtMessage m = new JtMessage ("JtADD");
83
84
85     if (msg == null)
86       return;
87
88     m.setMsgContent (msg);
89     messageLog.processMessage (m);
90
91   }
92
93   /**
94     * Process object messages. Invokes the superclass to process JtREMOVE, JtSTART and
95     * JtSTOP.
96     */

97
98   public Object JavaDoc processMessage (Object JavaDoc event) {
99
100    String JavaDoc msgid = null;
101    JtMessage e = (JtMessage) event;
102    Object JavaDoc content;
103    Object JavaDoc data;
104
105
106      if (e == null)
107     return null;
108
109      msgid = (String JavaDoc) e.getMsgId ();
110
111      if (msgid == null)
112     return null;
113
114      content = e.getMsgContent();
115      //data = e.getMsgData ();
116

117
118      if (msgid.equals ("JtREMOVE") ||
119          msgid.equals ("JtSTART") || msgid.equals ("JtSTOP")) {
120        return (super.processMessage (event));
121      }
122
123
124      handleError ("JtCommand.processMessage: invalid message id:" + msgid);
125      return (null);
126
127   }
128
129  
130   /**
131    * Unit tests the messages processed by JtCommand
132    */

133
134   public static void main(String JavaDoc[] args) {
135
136     JtObject main = new JtObject ();
137     JtMessage msg, msg1;
138     Integer JavaDoc count;
139     JtCommand command;
140
141
142
143     // Create an instance of JtCommand
144

145     command = (JtCommand) main.createObject ("Jt.JtCommand", "command");
146
147
148     main.sendMessage (command, new JtMessage ("JtSTART"));
149
150
151     main.removeObject ("command");
152
153
154   }
155
156 }
157
158
159
Free Books   Free Magazines  
Popular Tags