KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > WebServer


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s):
23  */

24 package dotcom;
25
26 import javax.jms.*;
27 import javax.naming.*;
28
29 /**
30  * Launching the WebServer simulating ordering and
31  * sending orders to the other servers (through topicOrders).
32  * <br><br>
33  * This code must be executed the latest in order to
34  * have the subscribers ready.
35  *
36  * @author Maistre Frederic
37  *
38  * @see WebOrdering
39  */

40 public class WebServer
41 {
42   public static void main (String JavaDoc argv[]) throws Exception JavaDoc
43   {
44     // setting LAF in order to avoid the following exception :
45
// java.lang.Error: can't load javax.swing.plaf.metal.MetalLookAndFeel
46
javax.swing.UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
47       
48     // creating a thread to order items and send the orders to the other
49
// servers
50
WebOrdering webOrdering = new WebOrdering() ;
51     java.lang.Thread JavaDoc webThread = new java.lang.Thread JavaDoc(webOrdering) ;
52     webThread.start() ;
53   }
54 }
55
56
57 /**
58  * Thread launched by the main of WebServer.
59  *
60  * @author Maistre Frederic
61  *
62  * @see Admin
63  * @see WebServer
64  * @see Servers
65  * @see OrderMessage
66  * @see GUI
67  */

68 class WebOrdering implements Runnable JavaDoc, Servers
69 {
70   static Context ictx = null;
71   /** Connection to topicOrders. */
72   TopicConnection tc ;
73   /** Session for publishing to topicOrders. */
74   TopicSession tsession ;
75   /** Publisher sending OrderMessages. */
76   TopicPublisher tp ;
77   /** Order id. */
78   int orderId ;
79   /** Item chosen. */
80   String JavaDoc choice ;
81   /** User graphical interface. */
82   GUI webGUI ;
83   
84   /**
85    * Method called when starting the thread.
86    */

87   public void run()
88   {
89     try {
90       //Getting initial context
91
ictx = new InitialContext();
92       // connecting to agent agWebConnT
93
TopicConnectionFactory tcf = (TopicConnectionFactory) ictx.lookup("tcf");
94       // connecting to topicOrders
95
Topic topicOrders = (Topic) ictx.lookup("tOrders");
96       ictx.close();
97
98       // creating a TopicConnection
99
tc = tcf.createTopicConnection("web", "web");
100       // creating a TopicSession
101
tsession = tc.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
102       // creating a TopicPublisher (publishing in topicOrders)
103
tp = tsession.createPublisher(topicOrders);
104       
105       // initializing orderId and choice
106
orderId = 1 ;
107       choice = "Shoes" ;
108       
109       // creating and activating the GUI representing the WebServer
110
webGUI = new GUI("WebServer" , this, 50, 300) ;
111       webGUI.setVisible(true) ;
112       
113       System.out.println("WebServer is ready.") ;
114       
115     } catch (Exception JavaDoc exc) {
116       System.out.println("Exception caught in WebServer thread: " + exc);
117       exc.printStackTrace();
118     }
119   }
120
121   /**
122    * Method called when selecting a RadioButton.
123    */

124   public void choiceMethod(String JavaDoc choice) {
125     // setting the item choice
126
this.choice = choice ;
127   }
128   
129   /**
130    * Method called when pressing GUI's otherButton.
131    */

132   public void otherMethod() {
133     try {
134       // deactivate the GUI
135
webGUI.setVisible(false) ;
136       
137       // creating the OrderMessage
138
OrderMessage orderMsg = new OrderMessage(orderId, choice) ;
139       // creating an ObjectMessage encapsulating the OrderMessage
140
ObjectMessage msgSent = tsession.createObjectMessage() ;
141       msgSent.setObject(orderMsg) ;
142       // sending the ObjectMessage to topicOrders
143
tp.publish(msgSent);
144       
145       System.out.println("Message sent by WebServer to topicOrders: ");
146       System.out.println("Id: " + orderMsg.id);
147       System.out.println("Item: " + orderMsg.item) ;
148       
149       // incrementing orderId
150
orderId ++ ;
151       
152       // updating and reactivating GUI
153
webGUI.updateId(orderId) ;
154       webGUI.setVisible(true) ;
155      
156     } catch (Exception JavaDoc exc) {
157       System.out.println("Exception caught in WebServer otherMethod: " + exc);
158       exc.printStackTrace();
159     }
160   }
161   
162   /**
163    * Method called when pressing GUI's sendButton.
164    */

165   public void sendMethod() {
166     try {
167       // deactivate the GUI
168
webGUI.setVisible(false) ;
169       
170       // creating the OrderMessage
171
OrderMessage orderMsg = new OrderMessage(orderId, choice) ;
172       // creating an ObjectMessage encapsulating the OrderMessage
173
ObjectMessage msgSent = tsession.createObjectMessage() ;
174       msgSent.setObject(orderMsg) ;
175       // sending the ObjectMessage to topicOrders
176
tp.publish(msgSent);
177       
178       // commiting the sending(s)
179
tsession.commit() ;
180       
181       System.out.println("Message sent by WebServer to topicOrders: ");
182       System.out.println("Id: " + orderMsg.id);
183       System.out.println("Item: " + orderMsg.item) ;
184       System.out.println("Sending(s) commited!") ;
185       System.out.println() ;
186
187       // resetting orderId
188
orderId = 1 ;
189       
190       // reactivating the GUI
191
webGUI.updateId(orderId) ;
192       webGUI.setVisible(true) ;
193      
194     } catch (Exception JavaDoc exc) {
195       System.out.println("Exception caught in WebServer sendMethod: " + exc);
196       exc.printStackTrace();
197     }
198   }
199   
200   /**
201    * Method called when pressing GUI's cancelButton.
202    */

203   public void cancelMethod() {
204     try {
205       // deactivate the GUI
206
webGUI.setVisible(false) ;
207       
208       // rollinback the sending(s)
209
tsession.rollback() ;
210       
211       // resetting orderId
212
orderId = 1 ;
213       
214       // updating and reactivating GUI
215
webGUI.updateId(orderId) ;
216       webGUI.setVisible(true) ;
217       
218       System.out.println("Sending(s) rolledback!") ;
219       System.out.println() ;
220      
221     } catch (Exception JavaDoc exc) {
222       System.out.println("Exception caught in WebServer cancelMethod: " + exc);
223       exc.printStackTrace();
224     }
225   }
226   
227   /**
228    * Method called when pressing GUI's quitButton.
229    */

230   public void quitMethod() {
231     try {
232       // desactivate the GUI
233
webGUI.setVisible(false) ;
234       
235       // creating a QuitMessage
236
QuitMessage quitMsg = new QuitMessage() ;
237       // creating an ObjectMessage encapsulating the QuitMessage
238
ObjectMessage msgSent = tsession.createObjectMessage() ;
239       msgSent.setObject(quitMsg) ;
240       // sending the ObjectMessage to topicOrders
241
tp.publish(msgSent);
242       
243       // commiting the sending
244
tsession.commit() ;
245       
246       // closing session and connection
247
tsession.close() ;
248       tc.close() ;
249       
250       System.out.println("Session and connection closed by WebServer.") ;
251       System.exit(0) ;
252       
253     } catch (Exception JavaDoc exc) {
254       System.out.println("Exception caught in WebServer quitMethod: " + exc);
255       exc.printStackTrace();
256     }
257   }
258    
259   /**
260    * Method inherited from the Servers interface, not implemented.
261    */

262   public void okMethod() {}
263   /**
264    * Method inherited from the Servers interface, not implemented.
265    */

266   public void noMethod() {}
267   /**
268    * Method inherited from the Servers interface, not implemented.
269    */

270   public void closeMethod() {}
271 }
272
Popular Tags