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.aop.scoped.ear.servlet;
023:
024: import java.io.IOException;
025: import java.io.PrintWriter;
026:
027: import javax.naming.InitialContext;
028: import javax.rmi.PortableRemoteObject;
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletException;
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.jboss.test.aop.scoped.ear.ejb.ExampleSession;
036: import org.jboss.test.aop.scoped.ear.ejb.ExampleSessionHome;
037: import org.jboss.test.aop.scoped.ear.interceptor.TestInterceptor;
038:
039: /**
040: *
041: * @author <a href="mailto:kabirkhan@bigfoot.com">Kabir Khan</a>
042: *
043: */
044: public class EarExampleServlet extends HttpServlet {
045:
046: String scope;
047:
048: public void init(ServletConfig cfg) throws ServletException {
049: super .init(cfg);
050: scope = super .getInitParameter("scope");
051: }
052:
053: public void service(HttpServletRequest request,
054: HttpServletResponse response) throws ServletException,
055: IOException {
056: try {
057: InitialContext ctx = new InitialContext();
058: Object obj = ctx.lookup("ExampleSession" + scope);
059:
060: System.out.println("Expected loader "
061: + ExampleSessionHome.class.getClassLoader());
062: System.out.println("Interfaces");
063: Class[] ifs = obj.getClass().getInterfaces();
064: for (int i = 0; i < ifs.length; i++) {
065: System.out.println(i + " " + ifs[i].getName() + " "
066: + ifs[i].getClassLoader());
067: }
068:
069: ExampleSessionHome home = (ExampleSessionHome) PortableRemoteObject
070: .narrow(obj, ExampleSessionHome.class);
071: ExampleSession exSess = home.create();
072:
073: TestInterceptor.intercepted = 0;
074: exSess.getValue("hello");
075: if (TestInterceptor.intercepted != 1) {
076: throw new ServletException(
077: "Wrong TestInterceptor.intercepted for bean, expected=1; actual="
078: + TestInterceptor.intercepted);
079: }
080:
081: TestInterceptor.intercepted = 0;
082: testServlet();
083: if (TestInterceptor.intercepted != 1) {
084: throw new ServletException(
085: "Wrong TestInterceptor.intercepted for servlet, expected=1; actual="
086: + TestInterceptor.intercepted);
087: }
088:
089: //This check must happen after methods calling the interceptor have occured since the interceptor chains are created lazily with generated advisors
090: String expected = "scope" + scope;
091: String actual = TestInterceptor.scope;
092: if (!actual.equals(expected)) {
093: throw new ServletException(
094: "Wrong TestInterceptor.scope, expected="
095: + expected + "; actual="
096: + TestInterceptor.scope);
097: }
098:
099: response.setContentType("text/html");
100: PrintWriter out = response.getWriter();
101: out.println("<html>");
102: out
103: .println("<head><title>EarExampleServlet</title></head>");
104: out.println("<body>Tests passed<br></body>");
105: out.println("</html>");
106: out.close();
107:
108: } catch (Exception e) {
109: e.printStackTrace();
110: throw new ServletException(e);
111: }
112: }
113:
114: private void testServlet() {
115:
116: }
117: }
|