1 22 package org.jboss.test.web.servlets; 23 24 import java.io.IOException ; 25 import java.io.PrintWriter ; 26 import java.net.URL ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 30 import javax.jms.JMSException ; 31 import javax.jms.Queue ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.Topic ; 34 import javax.mail.Session ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServlet ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.sql.DataSource ; 43 44 import org.jboss.test.cts.interfaces.CtsBmpHome; 45 import org.jboss.test.web.interfaces.StatelessSessionHome; 46 import org.jboss.test.web.interfaces.StatelessSessionLocalHome; 47 48 import org.jboss.test.web.util.Util; 49 50 55 public class ENCServlet extends HttpServlet 56 { 57 org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass()); 58 59 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 60 throws ServletException , IOException 61 { 62 testENC(); 63 response.setContentType("text/html"); 64 PrintWriter out = response.getWriter(); 65 out.println("<html>"); 66 out.println("<head><title>ENCServlet</title></head>"); 67 out.println("<body>Tests passed<br>Time:" + Util.getTime() + "</body>"); 68 out.println("</html>"); 69 out.close(); 70 } 71 72 protected void doGet(HttpServletRequest request, HttpServletResponse response) 73 throws ServletException , IOException 74 { 75 processRequest(request, response); 76 } 77 78 protected void doPost(HttpServletRequest request, HttpServletResponse response) 79 throws ServletException , IOException 80 { 81 processRequest(request, response); 82 } 83 84 private void testENC() throws ServletException 85 { 86 try 87 { 88 Context initCtx = new InitialContext (); 90 Hashtable env = initCtx.getEnvironment(); 91 Iterator keys = env.keySet().iterator(); 92 log.info("InitialContext.env:"); 93 while( keys.hasNext() ) 94 { 95 Object key = keys.next(); 96 log.info("Key: "+key+", value: "+env.get(key)); 97 } 98 Context myEnv = (Context ) initCtx.lookup("java:comp/env"); 99 testEjbRefs(initCtx, myEnv); 100 testJdbcDataSource(initCtx, myEnv); 101 testMail(initCtx, myEnv); 102 testJMS(initCtx, myEnv); 103 testURL(initCtx, myEnv); 104 testEnvEntries(initCtx, myEnv); 105 testMessageDestinationRefs(initCtx, myEnv); 106 } 107 catch (NamingException e) 108 { 109 log.debug("Lookup failed", e); 110 throw new ServletException ("Lookup failed, ENC tests failed", e); 111 } 112 catch (JMSException e) 113 { 114 log.debug("JMS access failed", e); 115 throw new ServletException ("JMS access failed, ENC tests failed", e); 116 } 117 catch (RuntimeException e) 118 { 119 log.debug("Runtime error", e); 120 throw new ServletException ("Runtime error, ENC tests failed", e); 121 } 122 } 123 124 private void testEnvEntries(Context initCtx, Context myEnv) throws NamingException 125 { 126 Integer i = (Integer ) myEnv.lookup("Ints/i0"); 128 log.debug("Ints/i0 = " + i); 129 i = (Integer ) initCtx.lookup("java:comp/env/Ints/i1"); 130 log.debug("Ints/i1 = " + i); 131 Float f = (Float ) myEnv.lookup("Floats/f0"); 132 log.debug("Floats/f0 = " + f); 133 f = (Float ) initCtx.lookup("java:comp/env/Floats/f1"); 134 log.debug("Floats/f1 = " + f); 135 String s = (String ) myEnv.lookup("Strings/s0"); 136 log.debug("Strings/s0 = " + s); 137 s = (String ) initCtx.lookup("java:comp/env/Strings/s1"); 138 log.debug("Strings/s1 = " + s); 139 s = (String ) initCtx.lookup("java:comp/env/ejb/catalog/CatalogDAOClass"); 140 log.debug("ejb/catalog/CatalogDAOClass = " + s); 141 } 142 143 private void testEjbRefs(Context initCtx, Context myEnv) throws NamingException 144 { 145 Object ejb = myEnv.lookup("ejb/bean0"); 147 if ((ejb instanceof StatelessSessionHome) == false) 148 throw new NamingException ("ejb/bean0 is not a StatelessSessionHome"); 149 log.debug("ejb/bean0 = " + ejb); 150 ejb = initCtx.lookup("java:comp/env/ejb/bean1"); 151 if ((ejb instanceof StatelessSessionHome) == false) 152 throw new NamingException ("ejb/bean1 is not a StatelessSessionHome"); 153 log.debug("ejb/bean1 = " + ejb); 154 ejb = initCtx.lookup("java:comp/env/ejb/bean2"); 155 if ((ejb instanceof StatelessSessionHome) == false) 156 throw new NamingException ("ejb/bean2 is not a StatelessSessionHome"); 157 log.debug("ejb/bean2 = " + ejb); 158 ejb = initCtx.lookup("java:comp/env/ejb/bean3"); 160 if ((ejb instanceof StatelessSessionHome) == false) 161 throw new NamingException ("ejb/bean3 is not a StatelessSessionHome"); 162 log.debug("ejb/bean3 = " + ejb); 163 164 165 ejb = initCtx.lookup("java:comp/env/ejb/UnsecuredEJB"); 166 if ((ejb instanceof StatelessSessionHome) == false) 167 throw new NamingException ("ejb/UnsecuredEJB is not a StatelessSessionHome"); 168 log.debug("ejb/UnsecuredEJB = " + ejb); 169 ejb = initCtx.lookup("java:comp/env/ejb/SecuredEJB"); 170 if ((ejb instanceof StatelessSessionHome) == false) 171 throw new NamingException ("ejb/SecuredEJB is not a StatelessSessionHome"); 172 log.debug("ejb/SecuredEJB = " + ejb); 173 ejb = initCtx.lookup("java:comp/env/ejb/CtsBmp"); 174 if ((ejb instanceof CtsBmpHome) == false) 175 throw new NamingException ("ejb/CtsBmp is not a CtsBmpHome"); 176 log.debug("ejb/CtsBmp = " + ejb); 177 ejb = initCtx.lookup("java:comp/env/ejb/RelativeBean"); 178 if ((ejb instanceof StatelessSessionHome) == false ) 179 throw new NamingException ("ejb/RelativeBean is not a StatelessSessionHome"); 180 log.debug("ejb/RelativeBean = " + ejb); 181 182 ejb = initCtx.lookup("java:comp/env/ejb/local/bean0"); 184 if ((ejb instanceof StatelessSessionLocalHome) == false) 185 throw new NamingException ("ejb/local/bean0 is not a StatelessSessionLocalHome"); 186 log.debug("ejb/local/bean0 = " + ejb); 187 ejb = initCtx.lookup("java:comp/env/ejb/local/bean1"); 188 if ((ejb instanceof StatelessSessionLocalHome) == false) 189 throw new NamingException ("ejb/local/bean1 is not a StatelessSessionLocalHome"); 190 log.debug("ejb/local/bean1 = " + ejb); 191 192 ejb = initCtx.lookup("java:comp/env/ejb/local/bean3"); 194 if ((ejb instanceof StatelessSessionLocalHome) == false) 195 throw new NamingException ("ejb/local/bean3 is not a StatelessSessionLocalHome"); 196 log.debug("ejb/local/bean3 = " + ejb); 197 198 ejb = initCtx.lookup("java:comp/env/ejb/local/OptimizedEJB"); 199 if ((ejb instanceof StatelessSessionLocalHome) == false) 200 throw new NamingException ("ejb/local/OptimizedEJB is not a StatelessSessionLocalHome"); 201 log.debug("ejb/local/OptimizedEJB = " + ejb); 202 ejb = initCtx.lookup("java:comp/env/ejb/local/RelativeBean"); 203 if ((ejb instanceof StatelessSessionLocalHome) == false ) 204 throw new NamingException ("ejb/local/RelativeBean is not a StatelessSessionLocalHome"); 205 log.debug("ejb/local/RelativeBean = " + ejb); 206 } 207 208 private void testJdbcDataSource(Context initCtx, Context myEnv) throws NamingException 209 { 210 DataSource ds = (DataSource ) myEnv.lookup("jdbc/DefaultDS"); 212 log.debug("jdbc/DefaultDS = " + ds); 213 } 214 215 private void testMail(Context initCtx, Context myEnv) throws NamingException 216 { 217 Session session = (Session ) myEnv.lookup("mail/DefaultMail"); 219 log.debug("mail/DefaultMail = " + session); 220 } 221 222 private void testJMS(Context initCtx, Context myEnv) throws NamingException 223 { 224 QueueConnectionFactory qf = (QueueConnectionFactory ) myEnv.lookup("jms/QueFactory"); 226 log.debug("jms/QueFactory = " + qf); 227 } 228 229 private void testURL(Context initCtx, Context myEnv) throws NamingException 230 { 231 URL home1 = (URL ) myEnv.lookup("url/JBossHome"); 233 log.debug("url/JBossHome = " + home1); 234 URL home2 = (URL ) initCtx.lookup("java:comp/env/url/JBossHome"); 235 log.debug("url/JBossHome = " + home2); 236 if( home1.equals(home2) == false ) 237 throw new NamingException ("url/JBossHome != java:comp/env/url/JBossHome"); 238 } 239 240 private void testMessageDestinationRefs(Context initCtx, Context myEnv) throws NamingException , JMSException 241 { 242 Object obj = myEnv.lookup("mdr/ConsumesLink"); 243 log.debug("mdr/ConsumesLink = " + obj); 244 if ((obj instanceof Queue ) == false) 245 throw new RuntimeException ("mdr/ConsumesLink is not a javax.jms.Queue"); 246 Queue queue = (Queue ) obj; 247 if ("QUEUE.testQueue".equals(queue.getQueueName())) 248 throw new RuntimeException ("Excepted QUEUE.testQueue, got " + queue); 249 250 obj = myEnv.lookup("mdr/ProducesLink"); 251 log.debug("mdr/ProducesLink = " + obj); 252 if ((obj instanceof Topic ) == false) 253 throw new RuntimeException ("mdr/ProducesLink is not a javax.jms.Topic"); 254 Topic topic = (Topic ) obj; 255 if ("TOPIC.testTopic".equals(topic.getTopicName())) 256 throw new RuntimeException ("Excepted TOPIC.testTopic got " + topic); 257 258 obj = myEnv.lookup("mdr/ConsumesProducesJNDIName"); 259 log.debug("mdr/ConsumesProducesJNDIName = " + obj); 260 if ((obj instanceof Queue ) == false) 261 throw new RuntimeException ("mdr/ConsumesProducesJNDIName is not a javax.jms.Queue"); 262 queue = (Queue ) obj; 263 if ("QUEUE.A".equals(queue.getQueueName())) 264 throw new RuntimeException ("Excepted QUEUE.A, got " + queue); 265 } 266 } 267
| Popular Tags
|