You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jb...@apache.org on 2006/03/03 04:22:31 UTC

svn commit: r382633 - in /incubator/tuscany/java/sca: core/src/main/java/org/apache/tuscany/core/webapp/ tomcat/src/test/java/org/apache/tuscany/tomcat/integration/ tomcat/src/test/resources/app2/WEB-INF/

Author: jboynes
Date: Thu Mar  2 19:22:30 2006
New Revision: 382633

URL: http://svn.apache.org/viewcvs?rev=382633&view=rev
Log:
migrate servlet filter to webapp package and update for standalone runtime

Added:
    incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java   (with props)
Modified:
    incubator/tuscany/java/sca/tomcat/src/test/java/org/apache/tuscany/tomcat/integration/TomcatStandaloneTestCase.java
    incubator/tuscany/java/sca/tomcat/src/test/resources/app2/WEB-INF/web.xml

Added: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java?rev=382633&view=auto
==============================================================================
--- incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java (added)
+++ incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java Thu Mar  2 19:22:30 2006
@@ -0,0 +1,115 @@
+/**
+ *
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.tuscany.core.webapp;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+
+import org.osoa.sca.SCA;
+import org.osoa.sca.ModuleContext;
+import org.osoa.sca.CurrentModuleContext;
+
+import org.apache.tuscany.core.context.AggregateContext;
+import org.apache.tuscany.core.context.EventContext;
+import org.apache.tuscany.core.context.webapp.TuscanyWebAppRuntime;
+import org.apache.tuscany.core.context.webapp.LazyHTTPSessionId;
+
+/**
+ * Notifies the {@link org.apache.tuscany.core.context.AggregateContext} of web request start and end events as well as setting up the
+ * current session context. The latter is done using lazy Servlet-based session retrieval. The filter fires a session
+ * start event, passing a <tt>LazyServletSessionId</tt> as the session id. The <tt>LazyServletSessionId</tt> is a
+ * wrapper for the servlet request which may be called by the <tt>ModuleContext</tt> to retrieve the session id
+ * lazily.
+ * 
+ * @version $Rev: 379957 $ $Date: 2006-02-22 14:58:24 -0800 (Wed, 22 Feb 2006) $
+ */
+public class TuscanyRequestFilter implements Filter {
+    private static final ContextBinder BINDER = new ContextBinder();
+
+    private AggregateContext moduleContext;
+
+    public TuscanyRequestFilter() {
+    }
+
+    public void init(FilterConfig filterConfig) throws ServletException {
+        ServletContext servletContext = filterConfig.getServletContext();
+        moduleContext = (AggregateContext) servletContext.getAttribute(TuscanyServletListener.MODULE_COMPONENT_NAME);
+    }
+
+    public void destroy() {
+    }
+
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+        ModuleContext oldContext = CurrentModuleContext.getContext();
+        try {
+            // Set the current module context
+            BINDER.setContext((ModuleContext) moduleContext);
+
+            // Handle a request
+            if (request instanceof HttpServletRequest) {
+                if (((HttpServletRequest) request).getSession(false) != null) {
+
+                    // A session is already active
+                    moduleContext.fireEvent(EventContext.SESSION_NOTIFY, ((HttpServletRequest) request).getSession(true));
+                } else {
+                    // Create a lazy wrapper since a session is not yet active
+                    moduleContext.fireEvent(EventContext.SESSION_NOTIFY, new LazyHTTPSessionId((HttpServletRequest) request));
+                }
+            } else {
+                moduleContext.fireEvent(EventContext.SESSION_NOTIFY, request);
+            }
+            // Start processing the request
+            moduleContext.fireEvent(EventContext.REQUEST_START, request);
+            // Dispatch to the next filter
+            filterChain.doFilter(request, response);
+        } catch (Exception e) {
+            throw new ServletException(e);
+
+        } finally {
+            try {
+                // End processing the request
+                moduleContext.fireEvent(EventContext.REQUEST_END, request);
+            } catch (Exception e) {
+                throw new ServletException(e);
+            }
+            BINDER.setContext(oldContext);
+        }
+
+    }
+
+    private static class ContextBinder extends SCA {
+        public void setContext(ModuleContext context) {
+            setModuleContext(context);
+        }
+
+        public void start() {
+            throw new AssertionError();
+        }
+
+        public void stop() {
+            throw new AssertionError();
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/webapp/TuscanyRequestFilter.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Modified: incubator/tuscany/java/sca/tomcat/src/test/java/org/apache/tuscany/tomcat/integration/TomcatStandaloneTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/tomcat/src/test/java/org/apache/tuscany/tomcat/integration/TomcatStandaloneTestCase.java?rev=382633&r1=382632&r2=382633&view=diff
==============================================================================
--- incubator/tuscany/java/sca/tomcat/src/test/java/org/apache/tuscany/tomcat/integration/TomcatStandaloneTestCase.java (original)
+++ incubator/tuscany/java/sca/tomcat/src/test/java/org/apache/tuscany/tomcat/integration/TomcatStandaloneTestCase.java Thu Mar  2 19:22:30 2006
@@ -5,6 +5,8 @@
 import org.apache.catalina.core.StandardHost;
 import org.apache.catalina.startup.ContextConfig;
 import org.apache.catalina.Valve;
+import org.apache.catalina.Wrapper;
+
 import org.apache.tuscany.tomcat.TuscanyValve;
 import org.apache.tuscany.tomcat.TuscanyHost;
 
@@ -27,11 +29,7 @@
         ctx.addLifecycleListener(new ContextConfig());
         ctx.setName("testContext");
         ctx.setDocBase(app2.getAbsolutePath());
-/*
-        StandardWrapper wrapper = new StandardWrapper();
-        wrapper.setServletClass(TestServlet.class.getName());
-        ctx.addChild(wrapper);
-*/
+
         host.addChild(ctx);
         boolean found = false;
         for (Valve valve: ctx.getPipeline().getValves()) {
@@ -43,8 +41,8 @@
         assertFalse("TuscanyValve in pipeline", found);
 
         request.setContext(ctx);
-//        request.setWrapper(wrapper);
-//        host.invoke(request, response);
+        request.setWrapper((Wrapper) ctx.findChild("TestServlet"));
+        host.invoke(request, response);
 
         host.removeChild(ctx);
     }

Modified: incubator/tuscany/java/sca/tomcat/src/test/resources/app2/WEB-INF/web.xml
URL: http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/tomcat/src/test/resources/app2/WEB-INF/web.xml?rev=382633&r1=382632&r2=382633&view=diff
==============================================================================
--- incubator/tuscany/java/sca/tomcat/src/test/resources/app2/WEB-INF/web.xml (original)
+++ incubator/tuscany/java/sca/tomcat/src/test/resources/app2/WEB-INF/web.xml Thu Mar  2 19:22:30 2006
@@ -5,9 +5,25 @@
 <web-app>
     <context-param>
         <param-name>org.apache.tuscany.core.webapp.ModuleComponentName</param-name>
-        <param-value>testStandalone</param-value>
+        <param-value>testContext</param-value>
     </context-param>
     <listener>
         <listener-class>org.apache.tuscany.core.webapp.TuscanyServletListener</listener-class>
     </listener>
+    <servlet>
+        <servlet-name>TestServlet</servlet-name>
+        <servlet-class>org.apache.tuscany.tomcat.integration.TestServlet</servlet-class>
+    </servlet>
+    <servlet-mapping>
+        <servlet-name>TestServlet</servlet-name>
+        <url-pattern>/servlet</url-pattern>
+    </servlet-mapping>
+    <filter>
+        <filter-name>TuscanyFilter</filter-name>
+        <filter-class>org.apache.tuscany.core.webapp.TuscanyRequestFilter</filter-class>
+    </filter>
+    <filter-mapping>
+        <filter-name>TuscanyFilter</filter-name>
+        <servlet-name>TestServlet</servlet-name>
+    </filter-mapping>
 </web-app>