1 package edu.rice.rubis.beans; 2 3 import java.net.URLEncoder ; 4 import javax.naming.*; 5 import javax.jms.*; 6 import javax.ejb.MessageDrivenBean ; 7 import javax.ejb.MessageDrivenContext ; 8 import javax.ejb.EJBException ; 9 import java.sql.Connection ; 10 import java.sql.PreparedStatement ; 11 import java.sql.ResultSet ; 12 import java.sql.SQLException ; 13 import javax.sql.DataSource ; 14 import javax.naming.InitialContext ; 15 16 21 22 public class MDB_BrowseRegions implements MessageDrivenBean , MessageListener 23 { 24 private DataSource dataSource; 25 private MessageDrivenContext messageDrivenContext; 26 private TopicConnectionFactory connectionFactory; 27 private TopicConnection connection; 28 private Topic topic; 29 private TopicSession session; 30 private TopicPublisher replier; 31 private Context 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 correlationID = request.getJMSCorrelationID(); 45 46 String html = listRegions(); 48 49 TemporaryTopic temporaryTopic = (TemporaryTopic) request.getJMSReplyTo(); 51 if (temporaryTopic != null) 52 { 53 connectionFactory = (TopicConnectionFactory) initialContext.lookup(BeanConfig.TopicConnectionFactoryName); 55 connection = connectionFactory.createTopicConnection(); 57 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); connection.start(); 64 replier.publish(temporaryTopic, reply); 65 replier.close(); 66 session.close(); 67 connection.stop(); 68 connection.close(); 69 } 70 } 71 catch (Exception e) 72 { 73 throw new EJBException ("Message traitment failed: " +e); 74 } 75 } 76 77 82 public String listRegions() throws Exception 83 { 84 Connection conn = null; 85 PreparedStatement stmt = null; 86 StringBuffer htmlPage = new StringBuffer (); 87 88 try 89 { 90 conn = dataSource.getConnection(); 91 stmt = conn.prepareStatement("SELECT name FROM regions"); 92 ResultSet rs = stmt.executeQuery(); 93 String regionName; 95 while(rs.next()) 96 { 97 regionName = rs.getString("name"); 98 htmlPage.append(printRegion(regionName)); 99 } 100 stmt.close(); conn.close(); } 103 catch (SQLException e) 104 { 105 try 106 { 107 if (stmt != null) stmt.close(); 108 if (conn != null) conn.close(); 109 } 110 catch (Exception ignore) 111 { 112 } 113 throw new Exception ("Failed to get regions " +e); 114 } 115 return htmlPage.toString(); 116 } 117 118 125 126 public String printRegion(String 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 134 145 public void setMessageDrivenContext(MessageDrivenContext ctx) 146 { 147 messageDrivenContext = ctx; 148 if (dataSource == null) 149 { 150 try 152 { 153 initialContext = new InitialContext (); 154 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 155 } 156 catch (Exception e) 157 { 158 throw new EJBException ("Cannot get JNDI InitialContext"); 159 } 160 } 161 } 162 163 167 public void ejbCreate() 168 { 169 170 } 171 172 181 public void ejbRemove() {} 182 183 } 184 | Popular Tags |