KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > MDB_BrowseRegions


1 package edu.rice.rubis.beans;
2
3 import java.net.URLEncoder JavaDoc;
4 import javax.naming.*;
5 import javax.jms.*;
6 import javax.ejb.MessageDrivenBean JavaDoc;
7 import javax.ejb.MessageDrivenContext JavaDoc;
8 import javax.ejb.EJBException JavaDoc;
9 import java.sql.Connection JavaDoc;
10 import java.sql.PreparedStatement JavaDoc;
11 import java.sql.ResultSet JavaDoc;
12 import java.sql.SQLException JavaDoc;
13 import javax.sql.DataSource JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15
16 /**
17  * This message driven bean gets the list of regions from the database.
18  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
19  * @version 1.0
20  */

21
22 public class MDB_BrowseRegions implements MessageDrivenBean JavaDoc, MessageListener
23 {
24   private DataSource JavaDoc dataSource;
25   private MessageDrivenContext JavaDoc messageDrivenContext;
26   private TopicConnectionFactory connectionFactory;
27   private TopicConnection connection;
28   private Topic topic;
29   private TopicSession session;
30   private TopicPublisher replier;
31   private Context JavaDoc initialContext = null;
32
33
34   public MDB_BrowseRegions()
35   {
36
37   }
38
39
40   public void onMessage(Message request)
41   {
42     try
43     {
44       String JavaDoc correlationID = request.getJMSCorrelationID();
45
46       // get the list of regions
47
String JavaDoc html = listRegions();
48
49       // send the reply
50
TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo();
51       if (temporaryTopic != null)
52       {
53         // Retrieve the connection factory
54
connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName);
55         // create a connection
56
connection = connectionFactory.createTopicConnection();
57         // create a session: no transaction, auto ack
58
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
59         TextMessage reply = session.createTextMessage();
60         reply.setJMSCorrelationID(correlationID);
61         reply.setText(html);
62         replier = session.createPublisher(null); // unidentified publisher
63
connection.start();
64         replier.publish(temporaryTopic, reply);
65         replier.close();
66         session.close();
67         connection.stop();
68         connection.close();
69       }
70     }
71     catch (Exception JavaDoc e)
72     {
73       throw new EJBException JavaDoc("Message traitment failed: " +e);
74     }
75   }
76
77   /**
78    * Retrieve the list of regions from the database.
79    * @return a string in html format
80    * @since 1.1
81    */

82   public String JavaDoc listRegions() throws Exception JavaDoc
83   {
84     Connection JavaDoc conn = null;
85     PreparedStatement JavaDoc stmt = null;
86     StringBuffer JavaDoc htmlPage = new StringBuffer JavaDoc();
87
88     try
89     {
90       conn = dataSource.getConnection();
91       stmt = conn.prepareStatement("SELECT name FROM regions");
92       ResultSet JavaDoc rs = stmt.executeQuery();
93       // Build the html page
94
String JavaDoc regionName;
95       while(rs.next())
96       {
97         regionName = rs.getString("name");
98         htmlPage.append(printRegion(regionName));
99       }
100       stmt.close(); // close statement
101
conn.close(); // release connection
102
}
103     catch (SQLException JavaDoc e)
104     {
105       try
106       {
107         if (stmt != null) stmt.close();
108         if (conn != null) conn.close();
109       }
110       catch (Exception JavaDoc ignore)
111       {
112       }
113       throw new Exception JavaDoc("Failed to get regions " +e);
114     }
115     return htmlPage.toString();
116   }
117
118   /**
119    * Region related printed functions
120    *
121    * @param name the name of the region to display
122    * @return a string in html format
123    * @since 1.1
124    */

125
126   public String JavaDoc printRegion(String JavaDoc name)
127   {
128     return "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.BrowseCategories?region="+URLEncoder.encode(name)+"\">"+name+"</a><br>\n";
129
130   }
131
132   // ======================== MDB related methods ============================
133

134   /**
135    * Set the associated context. The container call this method
136    * after the instance creation.
137    * The enterprise Bean instance should store the reference to the context
138    * object in an instance variable.
139    * This method is called with no transaction context.
140    *
141    * @param MessageDrivenContext A MessageDrivenContext interface for the instance.
142    * @throws EJBException Thrown by the method to indicate a failure caused by
143    * a system-level error.
144    */

145   public void setMessageDrivenContext(MessageDrivenContext JavaDoc ctx)
146   {
147     messageDrivenContext = ctx;
148     if (dataSource == null)
149     {
150       // Finds DataSource from JNDI
151
try
152       {
153         initialContext = new InitialContext JavaDoc();
154         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
155       }
156       catch (Exception JavaDoc e)
157       {
158         throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
159       }
160     }
161   }
162
163   /**
164    * The Message driven bean must define an ejbCreate methods with no args.
165    *
166    */

167   public void ejbCreate()
168   {
169
170   }
171  
172   /**
173    * A container invokes this method before it ends the life of the message-driven object.
174    * This happens when a container decides to terminate the message-driven object.
175    *
176    * This method is called with no transaction context.
177    *
178    * @throws EJBException Thrown by the method to indicate a failure caused by
179    * a system-level error.
180    */

181   public void ejbRemove() {}
182  
183 }
184
Popular Tags