KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chat > ChatApplication


1 /*
2  * Andy John
3  * 2/99
4  * Chat room
5  * andy@enhydra.org
6  * www.enhydra.org
7  *
8  * Chat - A simple chat room Enhydra application.
9  *
10  * The purpose of this program is to demonstrate ways to use the Enhydra
11  * application framework. Jolt is used for two of the dynamic HTML
12  * pages. One of the pages is generated by a presentation object written
13  * by hand.
14  *
15  * This application uses what an algorithm I call "on hold push", because
16  * it gives you the effect of "push technology", but only uses standard
17  * HTTP. You instantly see when other people send in a message. But it
18  * will work through proxies and firewalls. It is even applicable to
19  * applets, which have additional security restrictions (the stand alone
20  * debugger uses this algorithm to show the requests made to the
21  * servlet that is being debugged). See the Discussion object for a
22  * description of on hold push.
23  *
24  * www.enhydra.org
25  */

26
27 package chat;
28
29 import com.lutris.appserver.server.*;
30 import com.lutris.appserver.server.httpPresentation.*;
31 import com.lutris.appserver.server.session.*;
32 import com.lutris.util.*;
33
34 import chat.spec.*;
35
36 /**
37  * The application object.
38  * Reads all the settings from the config file.
39  */

40 public class ChatApplication extends StandardApplication {
41
42     private String JavaDoc roomName = "Chat Room";
43     private String JavaDoc bgColor = "";
44     private String JavaDoc clearCommand = "";
45
46     /**
47      * Read all the chat room settings at startup time. Some of the
48      * settings are saved, and the other classes refer to this class when
49      * they need the values. Some of the settings are only used once to
50      * initialize parts of the program.
51      */

52     public void startup(Config appConfig) throws ApplicationException {
53         super.startup(appConfig); // Be sure to call this!
54
/*
55          * Get the title of this chat room from the config file.
56          */

57         try {
58             roomName = appConfig.getString("RoomName", "Chat Room");
59         } catch (ConfigException e) {
60             throw new ApplicationException("Bogus RoomName in config file.", e);
61         }
62         /*
63          * How long should messages be kept? Get from config file.
64          */

65         int messageLifetimeSecs;
66         try {
67             messageLifetimeSecs = appConfig.getInt("MessageLifetimeSecs", 300);
68         } catch (ConfigException e) {
69             throw new ApplicationException(
70                                 "Bogus MessageLifetimeSecs in config file.", e);
71         }
72         /*
73          * How often to delete messages that are too old?
74          */

75         int harvestIntervalSecs;
76         try {
77             harvestIntervalSecs = appConfig.getInt("HarvestIntervalSecs", 55);
78         } catch (ConfigException e) {
79             throw new ApplicationException("Bogus HarvestIntervalSecs in config file.", e);
80         }
81         /*
82          * Get the max number of messages allowed (if any) from the config
83          * file. You don't know how many hits you will get. If you allow
84          * data structures to grow without limit, your application may
85          * use up all the memory and kill the virtual machine.
86          */

87         int maxQueueSize;
88         try {
89             maxQueueSize = appConfig.getInt("MaxQueueSize", 200);
90         } catch (ConfigException e) {
91             throw new ApplicationException(
92                                        "Bogus MaxQueueSize in config file.", e);
93         }
94         /*
95          * Get the background color string (if any) from the config file.
96          */

97         try {
98             bgColor = appConfig.getString("BackgroundColor", "#99CCFF");
99         } catch (ConfigException e) {
100             throw new ApplicationException(
101                                     "Bogus BackgroundColor in config file.", e);
102         }
103         /*
104          * Get the clear command (if any) from the config file.
105          */

106         try {
107             clearCommand = appConfig.getString("ResetCommand", "");
108         } catch (ConfigException e) {
109             throw new ApplicationException(
110                                     "Bogus ResetCommand in config file.", e);
111         }
112         /*
113          * Configure the DiscussionImpl object.
114          * Maybe start the message deleting thread.
115          */

116         
117         DiscussionManager discussionManager = DiscussionManagerFactory.getDiscussionManager("chat.business.DiscussionManagerImpl");
118       
119  /*
120  * Catch Null pointer exception ( we canot make a instances of classes from business layer when we run chat_pres )
121  * We need to allow chat_pres to be functional
122  */

123   try{
124         discussionManager.setMaxQueueSize(maxQueueSize);
125       
126         if (harvestIntervalSecs < 1)
127             harvestIntervalSecs = 1;
128         if (messageLifetimeSecs > 0)
129         
130            discussionManager.startHarvester(messageLifetimeSecs, harvestIntervalSecs);
131  
132     }catch(NullPointerException JavaDoc e){}
133  
134     }
135
136  
137     public boolean requestPreprocessor(HttpPresentationComms comms)
138                    throws Exception JavaDoc {
139         return super.requestPreprocessor(comms);
140     }
141
142
143 public void shutdown(){
144    DiscussionManager discussionManager = DiscussionManagerFactory.getDiscussionManager("chat.business.DiscussionManagerImpl");
145     try{
146         discussionManager.stopHarvester();
147     }catch(NullPointerException JavaDoc e){}
148    }
149     /**
150      * The title of this chat room.
151      */

152     public String JavaDoc getRoomName() {
153         return roomName;
154     }
155
156
157     /**
158      * What color should the pages be? This is used by both the entry frame
159      * and the contents frame.
160      */

161     public String JavaDoc getBgColor() {
162         return bgColor;
163     }
164
165     /**
166      * What's the clear command? This will be "" if there is no command.
167      * If this string is sent in (in a message), then delete all the
168      * messages currently in the queue.
169      */

170     public String JavaDoc getClearCommand() {
171         return clearCommand;
172     }
173
174
175     /**
176      * This is only called if this application is being run in the
177      * Enhydra Multiserver.
178      * Displays usage stats that an administrator would care about.
179      * The number of listeners is the number of requets blocked
180      * on a read, waiting for messages to appear or be deleted (often
181      * web browsers do "keep alive" behind the scenes, even if the users
182      * hit "stop", this might not go to zero immediatly).
183      *
184      * @return HTML that is displayed in the status page of the Multiserver.
185      */

186     public String JavaDoc toHtml() {
187         String JavaDoc response;
188         
189       
190         
191          DiscussionManager discussionManager = DiscussionManagerFactory.getDiscussionManager("chat.business.DiscussionManagerImpl");
192          int n = discussionManager.getNumWaiting();
193         
194         if (n == 1)
195             response = "There is 1 person listening.";
196         else
197             response = "There are " + n + " people listening.";
198       
199        long l = discussionManager.getTotalReceived();
200        
201         if (l == 1)
202             response += "<br>A total of 1 message has been received so far.";
203         else
204             response += "<br>A total of " + l + " messages have been received.";
205    
206           l = discussionManager.getCurrentSize();
207         if (l == 1)
208             response += "<br>There is currently 1 message.";
209         else
210             response += "<br>There are currently " + l + " messages.";
211         return response;
212     }
213
214 }
215
216
Popular Tags