KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > examples > HelloWorld


1 package Jt.examples;
2
3 import Jt.*;
4 import java.io.*;
5
6
7 public class HelloWorld extends JtObject {
8
9   private String JavaDoc greetingMessage;
10
11   public HelloWorld() {
12   }
13
14   // Attributes
15

16
17   public void setGreetingMessage (String JavaDoc message) {
18      this.greetingMessage = message; // void operation
19

20   }
21
22   public String JavaDoc getGreetingMessage () {
23      return (greetingMessage);
24   }
25
26
27   // Process object messages
28

29   public Object JavaDoc processMessage (Object JavaDoc message) {
30
31    String JavaDoc msgid = null;
32    JtMessage msg = (JtMessage) message;
33    Object JavaDoc content;
34
35      if (msg == null)
36     return null;
37
38      msgid = (String JavaDoc) msg.getMsgId ();
39
40      if (msgid == null)
41     return null;
42
43      content = msg.getMsgContent();
44
45      // Process JtHello Message
46

47      if (msgid.equals ("JtHello")) {
48
49         handleTrace ("HelloWorld returning a greeting message: " + greetingMessage);
50              
51         return (greetingMessage);
52      }
53
54      if (msgid.equals ("JtREMOVE")) {
55         return (null);
56      }
57          
58      handleError ("HelloWorld.processMessage: invalid message id:" + msgid);
59      return (null);
60
61   }
62
63
64   // Test program
65

66   public static void main(String JavaDoc[] args) {
67
68     JtFactory main = new JtFactory (); // Jt Factory
69
JtMessage msg;
70     String JavaDoc greeting;
71     String JavaDoc reply;
72
73
74     // Create helloWorld (HelloWorld class)
75

76     main.createObject ("Jt.examples.HelloWorld", "helloWorld");
77
78  
79     // Set the value of the greetingMessage attribute
80
// If the greetingMessage hasn't be set (loaded from .Jtrc)
81
// use the default value
82

83     greeting = (String JavaDoc) main.getValue ("helloWorld", "greetingMessage");
84
85     if (greeting == null || greeting.equals (""))
86       main.setValue ("helloWorld", "greetingMessage", "Hello World ...");
87
88
89     // Create a Message ("JtHello")
90

91     msg = new JtMessage ("JtHello");
92
93
94
95     // Send the Message
96

97     main.handleTrace ("main:sending a message (JtHello) to the helloWorld object ...");
98     reply = (String JavaDoc) main.sendMessage ("helloWorld", msg);
99
100     // Print the reply message (Greeting)
101
System.out.println (reply);
102
103     // Remove helloWorld
104

105     main.removeObject ("helloWorld");
106
107         
108
109   }
110
111 }
112
113
114
115
Popular Tags