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 javax.naming.Context;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import javax.servlet.ServletConfig;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServlet;
033: import javax.servlet.http.HttpServletRequest;
034: import javax.servlet.http.HttpServletResponse;
035: import javax.sql.DataSource;
036:
037: import org.jboss.test.web.interfaces.ReferenceTest;
038: import org.jboss.test.web.interfaces.StatelessSession;
039: import org.jboss.test.web.interfaces.StatelessSessionHome;
040: import org.jboss.test.web.util.Util;
041:
042: /** A servlet that accesses an EJB inside its init and destroy methods
043: to test web component startup ordering with respect to ebj components.
044:
045: @author Scott.Scott@jboss.org
046: @version $Revision: 57211 $
047: */
048: public class EJBOnStartupServlet extends HttpServlet {
049: org.apache.log4j.Category log = org.apache.log4j.Category
050: .getInstance(getClass());
051:
052: StatelessSessionHome home;
053:
054: public void init(ServletConfig config) throws ServletException {
055: String param = config.getInitParameter("failOnError");
056: boolean failOnError = true;
057: if (param != null
058: && Boolean.valueOf(param).booleanValue() == false)
059: failOnError = false;
060: try {
061: // Access the Util.configureLog4j() method to test classpath resource
062: URL propsURL = Util.configureLog4j();
063: log.debug("log4j.properties = " + propsURL);
064: // Access an EJB to see that they have been loaded first
065: InitialContext ctx = new InitialContext();
066: home = (StatelessSessionHome) ctx
067: .lookup("java:comp/env/ejb/OptimizedEJB");
068: log.debug("EJBOnStartupServlet is initialized");
069: } catch (Exception e) {
070: log.debug("failed", e);
071: ClassLoader loader = Thread.currentThread()
072: .getContextClassLoader();
073: try {
074: log.debug(Util.displayClassLoaders(loader));
075: } catch (NamingException ne) {
076: log.debug("failed", ne);
077: }
078: if (failOnError == true)
079: throw new ServletException(
080: "Failed to init EJBOnStartupServlet", e);
081: }
082: }
083:
084: public void destroy() {
085: try {
086: InitialContext ctx = new InitialContext();
087: StatelessSessionHome home = (StatelessSessionHome) ctx
088: .lookup("java:comp/env/ejb/OptimizedEJB");
089: log.debug("EJBOnStartupServlet is destroyed");
090: } catch (Exception e) {
091: log.debug("failed", e);
092: }
093: }
094:
095: protected void processRequest(HttpServletRequest request,
096: HttpServletResponse response) throws ServletException,
097: IOException {
098: try {
099: StatelessSession bean = home.create();
100: bean.noop(new ReferenceTest(), true);
101: bean.remove();
102: } catch (Exception e) {
103: throw new ServletException("Failed to call OptimizedEJB", e);
104: }
105: response.setContentType("text/html");
106: PrintWriter out = response.getWriter();
107: out.println("<html>");
108: out.println("<head><title>EJBOnStartupServlet</title></head>");
109: out.println("<body>Was initialized<br>");
110: out.println("Tests passed<br>Time:" + Util.getTime()
111: + "</body>");
112: out.println("</html>");
113: out.close();
114: }
115:
116: protected void doGet(HttpServletRequest request,
117: HttpServletResponse response) throws ServletException,
118: IOException {
119: processRequest(request, response);
120: }
121:
122: protected void doPost(HttpServletRequest request,
123: HttpServletResponse response) throws ServletException,
124: IOException {
125: processRequest(request, response);
126: }
127: }
|