View Javadoc
1 // $Header: /cvsroot/daoexamples/daoexamples/src/java/daoexamples/moviedemo/DemoServlet.java,v 1.7 2003/08/13 04:04:23 sullis Exp $ 2 3 /* 4 * 5 * 6 * 7 * 8 */ 9 10 package daoexamples.moviedemo; 11 12 import javax.servlet.http.*; 13 import javax.transaction.*; 14 import java.io.*; 15 import java.util.*; 16 import daoexamples.exception.*; 17 import daoexamples.movie.*; 18 import org.apache.commons.logging.*; 19 20 /*** 21 * 22 * @author Sean C. Sullivan 23 * 24 */ 25 public class DemoServlet extends HttpServlet 26 { 27 static final private Log log = LogFactory.getLog(DemoServlet.class); 28 29 public void setupDemo() 30 { 31 32 Exception caught = null; 33 34 MovieDAO dao = null; 35 36 UserTransaction utx = null; 37 38 try 39 { 40 MovieUtil.setupMovieTable(); 41 42 dao = MovieDAOFactory.getMovieDAO_JTA(); 43 44 utx = MovieUtil.getUserTransaction(); 45 46 log.info("UserTransaction status: " 47 + utx.getStatus()); 48 49 utx.begin(); 50 51 log.info("UserTransaction status: " 52 + utx.getStatus()); 53 54 dao.createMovie("R", "1999", "Game Day"); 55 dao.createMovie("G", "1999", "Tarzan"); 56 dao.createMovie("R", "2005", "Blair Witch Project Reloaded"); 57 dao.createMovie("PG-13", "2005", "Raiders of the Lost Ark Reloaded"); 58 dao.createMovie("R", "2005", "Bridget Jones Diary Reloaded"); 59 60 utx.commit(); 61 62 log.info("UserTransaction status: " 63 + utx.getStatus()); 64 } 65 catch (Exception ex) 66 { 67 caught = ex; 68 log.error(ex); 69 if (utx != null) 70 { 71 try 72 { 73 utx.rollback(); 74 } 75 catch (javax.transaction.SystemException ex2) 76 { 77 log.error(ex2); 78 } 79 } 80 } 81 finally 82 { 83 dao.close(); 84 if (caught != null) 85 { 86 throw new DAORuntimeException(caught); 87 } 88 } 89 90 } 91 92 /*** 93 * 94 * 95 * @throws daoexamples.exception.DAORuntimeException 96 * 97 */ 98 public void executeMovieDAO_JTA() 99 { 100 Exception caught = null; 101 102 // 103 // 104 // This DAO expects the caller 105 // to demarcate the transaction using JTA 106 // 107 // 108 MovieDAO dao = MovieDAOFactory.getMovieDAO_JTA(); 109 110 MessagePublisher publisher = null; 111 112 UserTransaction utx = MovieUtil.getUserTransaction(); 113 114 try 115 { 116 Movie matrix; 117 Movie topgun; 118 Movie linux; 119 Movie batman; 120 121 // 122 // Execute the first transaction 123 // 124 125 utx.begin(); 126 127 matrix = dao.createMovie("R", "2003", "Matrix Reloaded"); 128 topgun = dao.createMovie("R", "2004", "Top Gun Reloaded"); 129 linux = dao.createMovie("R", "2004", "Linux Reloaded"); 130 131 utx.commit(); 132 133 134 // 135 // Execute a second transaction. 136 // 137 // This transaction accesses two resources: 138 // 1) a JMS Topic 139 // 2) a JDBC DataSource 140 // 141 142 utx.begin(); 143 144 batman = dao.createMovie("R", "2004", "Batman Reloaded"); 145 146 publisher = new MessagePublisher(); 147 148 publisher.publishTextMessage("Hello world"); 149 150 dao.updateMovie(topgun.getId(), 151 "PG-13", 152 topgun.getReleaseYear(), 153 topgun.getTitle()); 154 155 dao.deleteMovie(linux.getId()); 156 157 utx.commit(); 158 159 } 160 catch (Exception ex) 161 { 162 log.error(ex); 163 caught = ex; 164 if (utx != null) 165 { 166 try 167 { 168 utx.rollback(); 169 } 170 catch (SystemException sysex) 171 { 172 log.error(sysex); 173 } 174 } 175 } 176 finally 177 { 178 if (dao != null) 179 { 180 dao.close(); 181 dao = null; 182 } 183 if (publisher != null) 184 { 185 publisher.close(); 186 publisher = null; 187 } 188 if (caught != null) 189 { 190 throw new DAORuntimeException(caught); 191 } 192 } 193 } 194 195 public void executeMovieDAO() 196 { 197 MovieDAO dao; 198 199 // 200 // 201 // This DAO uses JDBC transactions. 202 // 203 // The DAO controls transaction demarcation. 204 // 205 // 206 dao = MovieDAOFactory.getMovieDAO(); 207 208 Movie spykids = null; 209 spykids = dao.createMovie( 210 "PG", "2003", "Spy Kids 3-D"); 211 212 Movie americanwedding = null; 213 americanwedding = dao.createMovie( 214 "R", "2003", "American Wedding"); 215 216 Movie johnnyenglish = null; 217 johnnyenglish = dao.createMovie( 218 "PG", "2003", "Johnny English"); 219 220 String strMovieId = spykids.getId(); 221 222 try 223 { 224 Movie foundMovie = null; 225 226 foundMovie = dao.findMovieById(strMovieId); 227 228 log.info(foundMovie); 229 } 230 catch (MovieNotFoundException ex) 231 { 232 log.info("Movie id " + strMovieId + " not found"); 233 } 234 235 236 Collection moviesFrom1999 = dao.findMoviesByYear("1999"); 237 238 if (moviesFrom1999.size() > 0) 239 { 240 StringBuffer msg = new StringBuffer(); 241 msg.append("Movies from 1999:\n"); 242 243 Iterator iter = moviesFrom1999.iterator(); 244 245 while (iter.hasNext()) 246 { 247 Movie m = (Movie) iter.next(); 248 msg.append("\t"); 249 msg.append(m); 250 msg.append("\n"); 251 } 252 253 log.info(msg.toString()); 254 } 255 else 256 { 257 log.info("There are no movies from 1999"); 258 } 259 260 } 261 262 public void doGet(final HttpServletRequest req, 263 final HttpServletResponse resp) 264 throws java.io.IOException 265 { 266 String strMsg = ""; 267 268 try 269 { 270 setupDemo(); 271 272 executeMovieDAO(); 273 274 executeMovieDAO_JTA(); 275 276 strMsg = "Success!"; 277 } 278 catch (Exception ex) 279 { 280 java.io.StringWriter sw = new StringWriter(); 281 PrintWriter pw = new PrintWriter(sw); 282 ex.printStackTrace(pw); 283 strMsg = sw.getBuffer().toString(); 284 } 285 286 String strHTML = buildResponsePage(strMsg); 287 288 resp.setContentType("text/html"); 289 resp.setContentLength(strHTML.length()); 290 resp.setHeader("Cache-Control", "no-cache"); 291 292 PrintWriter writer = resp.getWriter(); 293 writer.println(strHTML); 294 writer.flush(); 295 } 296 297 private String buildResponsePage(String strMsg) 298 { 299 StringBuffer sbHTML = new StringBuffer(); 300 301 sbHTML.append("<html>"); 302 sbHTML.append("<body>"); 303 sbHTML.append(new java.util.Date()); 304 sbHTML.append("<br>"); 305 sbHTML.append("<pre>"); 306 sbHTML.append(strMsg); 307 sbHTML.append("</pre>"); 308 sbHTML.append("</body>"); 309 sbHTML.append("</html>"); 310 311 return sbHTML.toString(); 312 313 } 314 }

This page was automatically generated by Maven