KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > JtProxy


1 package Jt;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6
7
8 /**
9  * Jt Implementation of the Proxy pattern.
10  */

11
12
13 public class JtProxy extends JtObject {
14
15
16   private Object JavaDoc subject;
17
18   public JtProxy() {
19   }
20
21 /**
22   * Specifies the proxy's subject.
23   *
24   * @param subject subject
25   */

26
27   public void setSubject (Object JavaDoc subject) {
28      this.subject = subject;
29
30   }
31
32 /**
33   * Returns the proxy's subject.
34   */

35
36   public Object JavaDoc getSubject () {
37      return (subject);
38   }
39
40
41
42
43   /**
44     * Process object messages by forwarding them to the proxy's subject.
45
46     * @param event Jt Message
47     */

48
49   public Object JavaDoc processMessage (Object JavaDoc event) {
50
51    String JavaDoc msgid = null;
52    JtMessage e = (JtMessage) event;
53    Object JavaDoc content;
54
55      // Let the subject process the request
56

57      if (subject == null) {
58        handleError ("JtProxy.process: the subject attribute needs to be set");
59        return (null);
60      }
61
62      return (sendMessage (subject, event));
63
64
65   }
66
67   /**
68     * Unit Tests the messages processed by JtProxy.
69     */

70
71   public static void main(String JavaDoc[] args) {
72
73     JtObject main = new JtFactory ();
74     JtMessage msg;
75     Jt.examples.HelloWorld helloWorld;
76     JtProxy proxy;
77
78
79     // Create an instance of JtProxy
80

81     proxy = (JtProxy)
82       main.createObject ("Jt.JtProxy",
83       "proxy");
84     helloWorld = (Jt.examples.HelloWorld) main.createObject ("Jt.examples.HelloWorld",
85       "helloWorld");
86
87     main.setValue (proxy, "subject", helloWorld);
88
89
90     msg = new JtMessage("JtHello");
91     System.out.println ("Reply:" + main.sendMessage (proxy, msg));
92          
93
94   }
95
96 }
97
Popular Tags