001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.web.servlets;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.net.URL;
027: import java.util.Hashtable;
028: import java.util.Iterator;
029:
030: import javax.jms.JMSException;
031: import javax.jms.Queue;
032: import javax.jms.QueueConnectionFactory;
033: import javax.jms.Topic;
034: import javax.mail.Session;
035: import javax.naming.Context;
036: import javax.naming.InitialContext;
037: import javax.naming.NamingException;
038: import javax.servlet.ServletException;
039: import javax.servlet.http.HttpServlet;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042: import javax.sql.DataSource;
043:
044: import org.jboss.test.cts.interfaces.CtsBmpHome;
045: import org.jboss.test.web.interfaces.StatelessSessionHome;
046: import org.jboss.test.web.interfaces.StatelessSessionLocalHome;
047:
048: import org.jboss.test.web.util.Util;
049:
050: /** Tests of the server ENC naming context
051: *
052: * @author Scott.Stark@jboss.org
053: * @version $Revision: 57211 $
054: */
055: public class ENCServlet extends HttpServlet {
056: org.apache.log4j.Category log = org.apache.log4j.Category
057: .getInstance(getClass());
058:
059: protected void processRequest(HttpServletRequest request,
060: HttpServletResponse response) throws ServletException,
061: IOException {
062: testENC();
063: response.setContentType("text/html");
064: PrintWriter out = response.getWriter();
065: out.println("<html>");
066: out.println("<head><title>ENCServlet</title></head>");
067: out.println("<body>Tests passed<br>Time:" + Util.getTime()
068: + "</body>");
069: out.println("</html>");
070: out.close();
071: }
072:
073: protected void doGet(HttpServletRequest request,
074: HttpServletResponse response) throws ServletException,
075: IOException {
076: processRequest(request, response);
077: }
078:
079: protected void doPost(HttpServletRequest request,
080: HttpServletResponse response) throws ServletException,
081: IOException {
082: processRequest(request, response);
083: }
084:
085: private void testENC() throws ServletException {
086: try {
087: // Obtain the enterprise bean�s environment naming context.
088: Context initCtx = new InitialContext();
089: Hashtable env = initCtx.getEnvironment();
090: Iterator keys = env.keySet().iterator();
091: log.info("InitialContext.env:");
092: while (keys.hasNext()) {
093: Object key = keys.next();
094: log.info("Key: " + key + ", value: " + env.get(key));
095: }
096: Context myEnv = (Context) initCtx.lookup("java:comp/env");
097: testEjbRefs(initCtx, myEnv);
098: testJdbcDataSource(initCtx, myEnv);
099: testMail(initCtx, myEnv);
100: testJMS(initCtx, myEnv);
101: testURL(initCtx, myEnv);
102: testEnvEntries(initCtx, myEnv);
103: testMessageDestinationRefs(initCtx, myEnv);
104: } catch (NamingException e) {
105: log.debug("Lookup failed", e);
106: throw new ServletException(
107: "Lookup failed, ENC tests failed", e);
108: } catch (JMSException e) {
109: log.debug("JMS access failed", e);
110: throw new ServletException(
111: "JMS access failed, ENC tests failed", e);
112: } catch (RuntimeException e) {
113: log.debug("Runtime error", e);
114: throw new ServletException(
115: "Runtime error, ENC tests failed", e);
116: }
117: }
118:
119: private void testEnvEntries(Context initCtx, Context myEnv)
120: throws NamingException {
121: // Basic env values
122: Integer i = (Integer) myEnv.lookup("Ints/i0");
123: log.debug("Ints/i0 = " + i);
124: i = (Integer) initCtx.lookup("java:comp/env/Ints/i1");
125: log.debug("Ints/i1 = " + i);
126: Float f = (Float) myEnv.lookup("Floats/f0");
127: log.debug("Floats/f0 = " + f);
128: f = (Float) initCtx.lookup("java:comp/env/Floats/f1");
129: log.debug("Floats/f1 = " + f);
130: String s = (String) myEnv.lookup("Strings/s0");
131: log.debug("Strings/s0 = " + s);
132: s = (String) initCtx.lookup("java:comp/env/Strings/s1");
133: log.debug("Strings/s1 = " + s);
134: s = (String) initCtx
135: .lookup("java:comp/env/ejb/catalog/CatalogDAOClass");
136: log.debug("ejb/catalog/CatalogDAOClass = " + s);
137: }
138:
139: private void testEjbRefs(Context initCtx, Context myEnv)
140: throws NamingException {
141: // EJB References
142: Object ejb = myEnv.lookup("ejb/bean0");
143: if ((ejb instanceof StatelessSessionHome) == false)
144: throw new NamingException(
145: "ejb/bean0 is not a StatelessSessionHome");
146: log.debug("ejb/bean0 = " + ejb);
147: ejb = initCtx.lookup("java:comp/env/ejb/bean1");
148: if ((ejb instanceof StatelessSessionHome) == false)
149: throw new NamingException(
150: "ejb/bean1 is not a StatelessSessionHome");
151: log.debug("ejb/bean1 = " + ejb);
152: ejb = initCtx.lookup("java:comp/env/ejb/bean2");
153: if ((ejb instanceof StatelessSessionHome) == false)
154: throw new NamingException(
155: "ejb/bean2 is not a StatelessSessionHome");
156: log.debug("ejb/bean2 = " + ejb);
157: //do lookup on bean specified without ejb-link
158: ejb = initCtx.lookup("java:comp/env/ejb/bean3");
159: if ((ejb instanceof StatelessSessionHome) == false)
160: throw new NamingException(
161: "ejb/bean3 is not a StatelessSessionHome");
162: log.debug("ejb/bean3 = " + ejb);
163:
164: ejb = initCtx.lookup("java:comp/env/ejb/UnsecuredEJB");
165: if ((ejb instanceof StatelessSessionHome) == false)
166: throw new NamingException(
167: "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(
172: "ejb/SecuredEJB is not a StatelessSessionHome");
173: log.debug("ejb/SecuredEJB = " + ejb);
174: ejb = initCtx.lookup("java:comp/env/ejb/CtsBmp");
175: if ((ejb instanceof CtsBmpHome) == false)
176: throw new NamingException("ejb/CtsBmp is not a CtsBmpHome");
177: log.debug("ejb/CtsBmp = " + ejb);
178: ejb = initCtx.lookup("java:comp/env/ejb/RelativeBean");
179: if ((ejb instanceof StatelessSessionHome) == false)
180: throw new NamingException(
181: "ejb/RelativeBean is not a StatelessSessionHome");
182: log.debug("ejb/RelativeBean = " + ejb);
183:
184: // EJB Local References
185: ejb = initCtx.lookup("java:comp/env/ejb/local/bean0");
186: if ((ejb instanceof StatelessSessionLocalHome) == false)
187: throw new NamingException(
188: "ejb/local/bean0 is not a StatelessSessionLocalHome");
189: log.debug("ejb/local/bean0 = " + ejb);
190: ejb = initCtx.lookup("java:comp/env/ejb/local/bean1");
191: if ((ejb instanceof StatelessSessionLocalHome) == false)
192: throw new NamingException(
193: "ejb/local/bean1 is not a StatelessSessionLocalHome");
194: log.debug("ejb/local/bean1 = " + ejb);
195:
196: //lookup of local-ejb-ref bean specified without ejb-link
197: ejb = initCtx.lookup("java:comp/env/ejb/local/bean3");
198: if ((ejb instanceof StatelessSessionLocalHome) == false)
199: throw new NamingException(
200: "ejb/local/bean3 is not a StatelessSessionLocalHome");
201: log.debug("ejb/local/bean3 = " + ejb);
202:
203: ejb = initCtx.lookup("java:comp/env/ejb/local/OptimizedEJB");
204: if ((ejb instanceof StatelessSessionLocalHome) == false)
205: throw new NamingException(
206: "ejb/local/OptimizedEJB is not a StatelessSessionLocalHome");
207: log.debug("ejb/local/OptimizedEJB = " + ejb);
208: ejb = initCtx.lookup("java:comp/env/ejb/local/RelativeBean");
209: if ((ejb instanceof StatelessSessionLocalHome) == false)
210: throw new NamingException(
211: "ejb/local/RelativeBean is not a StatelessSessionLocalHome");
212: log.debug("ejb/local/RelativeBean = " + ejb);
213: }
214:
215: private void testJdbcDataSource(Context initCtx, Context myEnv)
216: throws NamingException {
217: // JDBC DataSource
218: DataSource ds = (DataSource) myEnv.lookup("jdbc/DefaultDS");
219: log.debug("jdbc/DefaultDS = " + ds);
220: }
221:
222: private void testMail(Context initCtx, Context myEnv)
223: throws NamingException {
224: // JavaMail Session
225: Session session = (Session) myEnv.lookup("mail/DefaultMail");
226: log.debug("mail/DefaultMail = " + session);
227: }
228:
229: private void testJMS(Context initCtx, Context myEnv)
230: throws NamingException {
231: // JavaMail Session
232: QueueConnectionFactory qf = (QueueConnectionFactory) myEnv
233: .lookup("jms/QueFactory");
234: log.debug("jms/QueFactory = " + qf);
235: }
236:
237: private void testURL(Context initCtx, Context myEnv)
238: throws NamingException {
239: // URLs
240: URL home1 = (URL) myEnv.lookup("url/JBossHome");
241: log.debug("url/JBossHome = " + home1);
242: URL home2 = (URL) initCtx.lookup("java:comp/env/url/JBossHome");
243: log.debug("url/JBossHome = " + home2);
244: if (home1.equals(home2) == false)
245: throw new NamingException(
246: "url/JBossHome != java:comp/env/url/JBossHome");
247: }
248:
249: private void testMessageDestinationRefs(Context initCtx,
250: Context myEnv) throws NamingException, JMSException {
251: Object obj = myEnv.lookup("mdr/ConsumesLink");
252: log.debug("mdr/ConsumesLink = " + obj);
253: if ((obj instanceof Queue) == false)
254: throw new RuntimeException(
255: "mdr/ConsumesLink is not a javax.jms.Queue");
256: Queue queue = (Queue) obj;
257: if ("QUEUE.testQueue".equals(queue.getQueueName()))
258: throw new RuntimeException("Excepted QUEUE.testQueue, got "
259: + queue);
260:
261: obj = myEnv.lookup("mdr/ProducesLink");
262: log.debug("mdr/ProducesLink = " + obj);
263: if ((obj instanceof Topic) == false)
264: throw new RuntimeException(
265: "mdr/ProducesLink is not a javax.jms.Topic");
266: Topic topic = (Topic) obj;
267: if ("TOPIC.testTopic".equals(topic.getTopicName()))
268: throw new RuntimeException("Excepted TOPIC.testTopic got "
269: + topic);
270:
271: obj = myEnv.lookup("mdr/ConsumesProducesJNDIName");
272: log.debug("mdr/ConsumesProducesJNDIName = " + obj);
273: if ((obj instanceof Queue) == false)
274: throw new RuntimeException(
275: "mdr/ConsumesProducesJNDIName is not a javax.jms.Queue");
276: queue = (Queue) obj;
277: if ("QUEUE.A".equals(queue.getQueueName()))
278: throw new RuntimeException("Excepted QUEUE.A, got " + queue);
279: }
280: }
|