KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > servlets > BrowseRegions


1 package edu.rice.rubis.beans.servlets;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.jms.Session JavaDoc;
6 import javax.jms.TextMessage JavaDoc;
7 import javax.jms.Topic JavaDoc;
8 import javax.jms.TopicConnection JavaDoc;
9 import javax.jms.TopicConnectionFactory JavaDoc;
10 import javax.jms.TopicRequestor JavaDoc;
11 import javax.jms.TopicSession JavaDoc;
12 import javax.naming.Context JavaDoc;
13 import javax.naming.InitialContext JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19 /**
20  * Builds the html page with the list of all region in the database
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.0
23  */

24 public class BrowseRegions extends HttpServlet JavaDoc
25 {
26
27   /**
28    * Display the list of regions
29    *
30    * @param request a <code>HttpServletRequest</code> value
31    * @param response a <code>HttpServletResponse</code> value
32    * @exception IOException if an error occurs
33    * @exception ServletException if an error occurs
34    */

35   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
36   {
37     ServletPrinter sp = null;
38     String JavaDoc html;
39     sp = new ServletPrinter(response, "BrowseRegions");
40     sp.printHTMLheader("RUBiS: Available regions");
41  
42     Context JavaDoc initialContext = null;
43     try
44     {
45       initialContext = new InitialContext JavaDoc();
46     }
47     catch (Exception JavaDoc e)
48     {
49       sp.printHTML("Cannot get initial context for JNDI: " +e+"<br>");
50       return ;
51     }
52     TopicConnectionFactory JavaDoc topicFactory = null;
53     TopicConnection JavaDoc connection = null;
54     TopicSession JavaDoc session = null;
55     Topic JavaDoc topic = null;
56     try
57     {
58       // lookup the connection factory
59
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
60       // create a connection to the JMS provider
61
connection = topicFactory.createTopicConnection();
62       // lookup the destination
63
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicBrowseRegions");
64       // create a session
65
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
66
}
67     catch (Exception JavaDoc e)
68     {
69       sp.printHTML("Cannot connect to message bean MDB_BrowseRegions : " +e+"<br>");
70       return ;
71     }
72     try
73     {
74       // create a requestor to receive the reply
75
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
76       // create a message
77
TextMessage JavaDoc message = session.createTextMessage();
78       message.setText("pouet");
79       message.setJMSCorrelationID("region");
80       // send the message and receive the reply
81
connection.start(); // allows message to be delivered (default is connection stopped)
82
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
83       connection.stop();
84       // read the reply
85
html = reply.getText();
86       // close connection and session
87
requestor.close(); // also close the session
88
connection.close();
89     }
90     catch (Exception JavaDoc e)
91     {
92       sp.printHTML("Error contacting the message bean: " +e+"<br>");
93       return ;
94     }
95     try
96     {
97       if (html.equals(""))
98         sp.printHTML("<h2>Sorry, but there is no region available at this time. Database table is empty</h2><br>");
99       else
100       {
101         sp.printHTML("<h2>Currently available regions</h2><br>");
102         sp.printHTML(html);
103       }
104     }
105     catch (Exception JavaDoc e)
106     {
107       sp.printHTML("Cannot get the list of regions: " +e+"<br>");
108       return ;
109     }
110     sp.printHTMLfooter();
111   }
112
113 }
114
Popular Tags