1 25 26 package org.objectweb.earsample.servlets; 27 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.net.URL ; 31 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.rmi.PortableRemoteObject ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServlet ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 import javax.transaction.UserTransaction ; 40 41 import org.objectweb.earsample.beans.secusb.Op; 42 import org.objectweb.earsample.beans.secusb.OpHome; 43 import org.objectweb.earsample.beans.secusb.OpLocal; 44 import org.objectweb.earsample.beans.secusb.OpLocalHome; 45 46 50 public class ServletOp extends HttpServlet { 51 52 55 private static final int FIRST_BUY_AMOUNT = 10; 56 57 60 private static final int SECOND_BUY_AMOUNT = 20; 61 62 65 private static final int THIRD_BUY_AMOUNT = 50; 66 67 68 79 public void doGet(HttpServletRequest request, HttpServletResponse response) 80 throws IOException , ServletException { 81 82 response.setContentType("text/html"); 83 PrintWriter out = response.getWriter(); 84 out.println("<html>"); 85 out.println("<head>"); 86 out.println("<link type=\"text/css\" HREF=\"../ow_jonas.css\" rel=\"stylesheet\" id=\"stylesheet\">"); 87 out.println("<title>"); 88 out.println("Ear Sample of Servlet accessing a protected EJB</title>"); 89 out.println("</head>"); 90 out.println("<body style=\"background : white; color : black;\">"); 91 out.println("<h1>Ear sample of Servlet accessing a protected EJB"); 92 out.println("</h1><img SRC=\"../img/tomcat.gif\" alt=\"Tomcat Logo\">"); 93 out.println("<img SRC=\"../img/jetty.gif\" alt=\"Jetty Logo\">"); 94 out.println("<img SRC=\"../img/ow_jonas_logo.gif\" alt=\"JOnAS Logo\">"); 95 out.println("<h3>Initial context / UserTransaction </h3>"); 96 out.println("<ul>"); 97 Context initialContext = null; 98 try { 99 initialContext = new InitialContext (); 100 out.println("<li>Initial context OK</li>"); 101 } catch (Exception e) { 102 out.print("<li>Cannot get initial context for JNDI: "); 103 out.println(e + "</li>"); 104 return; 105 } 106 107 UserTransaction utx = null; 109 try { 110 utx = (UserTransaction ) initialContext.lookup("java:comp/UserTransaction"); 111 out.println("<li>Get java:comp/UserTransaction OK</li>"); 112 } catch (Exception e) { 113 out.println("<li>Cannot lookup java:comp/UserTransaction: " + e + "</li>"); 114 return; 115 } 116 out.println("</ul><br />"); 117 118 out.println("<h3>"); 119 out.println("Lookup on env-entry with java:comp/env/envEntryString"); 120 out.println("</h3>"); 121 out.println("<ul>"); 122 String envEntry = null; 123 try { 124 envEntry = (String ) initialContext.lookup("java:comp/env/envEntryString"); 125 out.println("<li>Env entry is : " + envEntry + "</li>"); 126 } catch (Exception e) { 127 out.println("<li>Cannot get env-entry on JNDI " + e + "</li>"); 128 return; 129 } 130 out.println("</ul><br />"); 131 132 133 out.println("</ul><br />"); 134 135 out.println("<h3>"); 136 out.println("Lookup on URL with java:comp/env/url/URL"); 137 out.println("</h3>"); 138 out.println("<ul>"); 139 URL jonasURL = null; 140 try { 141 jonasURL = (URL ) initialContext.lookup("java:comp/env/url/URL"); 142 out.println("<li>URL is : <a HREF=\"" + jonasURL.toString() + ">" + jonasURL.toString() + "</a></li>"); 143 } catch (Exception e) { 144 out.println("<li>Cannot get url on JNDI " + e + "</li>"); 145 return; 146 } 147 out.println("</ul><br />"); 148 149 out.println("<h3>Actions realized by the servlet and the EJB</h3>"); 150 out.println("<h3>With local interface</h3>"); 151 out.println("<ul>"); 152 OpLocalHome opLocalHome = null; 154 try { 155 opLocalHome = (OpLocalHome) 156 initialContext.lookup("java:comp/env/ejb/OpLocal"); 157 } catch (Exception e) { 158 out.println("<li>Cannot lookup java:comp/env/ejb/OpLocal: " + e + "</li>"); 159 return; 160 } 161 OpLocal opLocal = null; 163 try { 164 opLocal = opLocalHome.create("User1"); 165 out.println("<li>Create a bean</li>"); 166 } catch (Exception e) { 167 out.println("<li>Cannot create OpBean: " + e + "</li>"); 168 return; 169 } 170 try { 172 out.println("<li>Start a first transaction</li>"); 173 utx.begin(); 174 opLocal.buy(FIRST_BUY_AMOUNT); 175 out.println("<li>First request on the new bean</li>"); 176 opLocal.buy(SECOND_BUY_AMOUNT); 177 out.println("<li>Second request on the bean</li>"); 178 utx.commit(); 179 out.println("<li>Commit the transaction</li>"); 180 } catch (Exception e) { 181 out.println("<li>exception during 1st Tx: " + e + "</li>"); 182 return; 183 } 184 try { 186 out.println("<li>Start a second transaction</li>"); 187 utx.begin(); 188 opLocal.buy(THIRD_BUY_AMOUNT); 189 utx.rollback(); 190 out.println("<li>Rollback the transaction</li>"); 191 } catch (Exception e) { 192 out.println("<li>exception during 2nd Tx: " + e + "</li>"); 193 return; 194 } 195 int val = 0; 197 try { 198 val = opLocal.read(); 199 out.println("<li>Request outside any transaction</li>"); 200 } catch (Exception e) { 201 out.println("<li>Cannot read value on t1 : " + e + "</li>"); 202 return; 203 } 204 if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) { 205 out.println("<li>Bad value read: " + val + "</li>"); 206 return; 207 } 208 try { 210 opLocal.remove(); 211 } catch (Exception e) { 212 out.println("<li>Exception on buy: " + e + "</li>"); 213 return; 214 } 215 out.println("</ul><br />"); 216 217 out.println("<h3>With remote interface</h3>"); 218 out.println("<ul>"); 219 OpHome opHome = null; 221 try { 222 opHome = (OpHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Op"), OpHome.class); 223 } catch (Exception e) { 224 out.println("<li>Cannot lookup java:comp/env/ejb/Op: " + e + "</li>"); 225 return; 226 } 227 Op op = null; 229 try { 230 op = opHome.create("User1"); 231 out.println("<li>Create a bean</li>"); 232 } catch (Exception e) { 233 out.println("<li>Cannot create OpBean: " + e + "</li>"); 234 return; 235 } 236 try { 238 out.println("<li>Start a first transaction</li>"); 239 utx.begin(); 240 op.buy(FIRST_BUY_AMOUNT); 241 out.println("<li>First request on the new bean</li>"); 242 op.buy(SECOND_BUY_AMOUNT); 243 out.println("<li>Second request on the bean</li>"); 244 utx.commit(); 245 out.println("<li>Commit the transaction</li>"); 246 } catch (Exception e) { 247 out.println("<li>exception during 1st Tx: " + e + "</li>"); 248 return; 249 } 250 try { 252 out.println("<li>Start a second transaction</li>"); 253 utx.begin(); 254 op.buy(THIRD_BUY_AMOUNT); 255 utx.rollback(); 256 out.println("<li>Rollback the transaction</li>"); 257 } catch (Exception e) { 258 out.println("<li>exception during 2nd Tx: " + e + "</li>"); 259 return; 260 } 261 val = 0; 263 try { 264 val = op.read(); 265 out.println("<li>Request outside any transaction</li>"); 266 } catch (Exception e) { 267 out.println("<li>Cannot read value on t1 : " + e + "</li>"); 268 return; 269 } 270 if (val != FIRST_BUY_AMOUNT + SECOND_BUY_AMOUNT) { 271 out.println("<li>Bad value read: " + val + "</li>"); 272 return; 273 } 274 try { 276 op.remove(); 277 } catch (Exception e) { 278 out.println("<li>Exception on buy: " + e + "</li>"); 279 return; 280 } 281 out.println("</ul><br />"); 282 283 284 285 out.println("<h3>"); 286 out.println("Resource Adapter Properties"); 287 out.println("</h3>"); 288 out.println("<ul>"); 289 String eisName = null; 290 try { 291 eisName = (String ) initialContext.lookup("eisName"); 292 out.println("<li>EIS Name is : " + eisName + "</li>"); 293 } catch (Exception e) { 294 out.println("<li>Cannot get eis name on JNDI " + e + "</li>"); 295 return; 296 } 297 out.println("</ul><br />"); 298 299 300 out.println("<strong>Sample is OK.</strong><br />"); 301 out.println("</body>"); 302 out.println("</html>"); 303 } 304 } 305 | Popular Tags |