abdera-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmsn...@apache.org
Subject svn commit: r441582 - in /incubator/abdera/java/trunk: examples/src/main/java/org/apache/abdera/examples/appserver/ server/src/main/java/org/apache/abdera/protocol/server/ server/src/main/java/org/apache/abdera/protocol/server/servlet/ server/src/main/...
Date Fri, 08 Sep 2006 17:25:45 GMT
Author: jmsnell
Date: Fri Sep  8 10:25:44 2006
New Revision: 441582

URL: http://svn.apache.org/viewvc?view=rev&rev=441582
Log:
RequestHandlers should be stateless and thread safe.  
In order to make things a bit more efficient, introduce a pooling model for 
RequestHandlers and provide an AbstractRequestHandlerFactory implementation
that provides a simple stack-based pool.  There's no max-instance limit on this
so folks in high-transaction environments should be careful.

Added:
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandlerFactory.java
Modified:
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandler.java
    incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandlerFactory.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandlerFactory.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
    incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandler.java

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandler.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandler.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandler.java
Fri Sep  8 10:25:44 2006
@@ -46,6 +46,21 @@
 import org.apache.abdera.util.Constants;
 import org.apache.abdera.util.MimeTypeHelper;
 
+/**
+ * <p>RequestHandlers are the components that actually do the work in the APP
+ * server.  That is, the RequestHandler is responsible for interfacing with
+ * the back end data storage and translating the APP GET, POST, PUT, DELETE
+ * operations into operations on the data store, and for parsing the Atom 
+ * posts into something the datastore can understand.</p>
+ * 
+ * <p>The AbstractRequestHandler helper class makes things a bit easier by
+ * providing hooks for checking whether or not the requested resource has
+ * been modified (to support If-Modified-Since), checking to see if the resource
+ * exists (to support 404 or 410 responses), validating the request (to support
+ * 400 Bad Request responses), etc.</p>
+ * 
+ * <p>RequestHandler implementations SHOULD be thread safe</p>
+ */
 public class SimpleRequestHandler 
   extends AbstractRequestHandler {
 

Modified: incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandlerFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandlerFactory.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandlerFactory.java
(original)
+++ incubator/abdera/java/trunk/examples/src/main/java/org/apache/abdera/examples/appserver/SimpleRequestHandlerFactory.java
Fri Sep  8 10:25:44 2006
@@ -18,19 +18,18 @@
 package org.apache.abdera.examples.appserver;
 
 import org.apache.abdera.protocol.server.AbderaServer;
-import org.apache.abdera.protocol.server.RequestContext;
 import org.apache.abdera.protocol.server.RequestHandler;
 import org.apache.abdera.protocol.server.RequestHandlerFactory;
-import org.apache.abdera.protocol.server.exceptions.AbderaServerException;
+import org.apache.abdera.protocol.server.util.AbstractRequestHandlerFactory;
 
-public class SimpleRequestHandlerFactory 
+public class SimpleRequestHandlerFactory
+  extends AbstractRequestHandlerFactory
   implements RequestHandlerFactory {
 
-  public RequestHandler newRequestHandler(
-    AbderaServer abderaServer,
-    RequestContext requestContext)
-      throws AbderaServerException {
-    return new SimpleRequestHandler(abderaServer);
+  @Override
+  protected RequestHandler newRequestHandlerInstance(
+    AbderaServer abderaServer) {
+      return new SimpleRequestHandler(abderaServer);
   }
 
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandler.java
Fri Sep  8 10:25:44 2006
@@ -19,10 +19,19 @@
 
 import org.apache.abdera.protocol.server.exceptions.AbderaServerException;
 
+/**
+ * RequestHandlers SHOULD be thread safe and stateless
+ */
 public interface RequestHandler {
 
   ResponseContext invoke(
     RequestContext requestContext) 
       throws AbderaServerException;
+  
+  /**
+   * RequestHandlers that do have reason to store request specific state MUST
+   * clean that internal state when the clean method is invoked.
+   */
+  void clean();
   
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandlerFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandlerFactory.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandlerFactory.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/RequestHandlerFactory.java
Fri Sep  8 10:25:44 2006
@@ -22,8 +22,9 @@
 public interface RequestHandlerFactory {
   
   RequestHandler newRequestHandler(
-    AbderaServer abderaServer,
-    RequestContext requestContext) 
+    AbderaServer abderaServer) 
       throws AbderaServerException;
+  
+  void releaseRequestHandler(RequestHandler handler);
   
 }

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/servlet/AbderaServlet.java
Fri Sep  8 10:25:44 2006
@@ -110,10 +110,11 @@
     RequestContext requestContext = getRequestContext(request);
     ResponseContext responseContext = null;
     RequestHandler handler = null;
+    RequestHandlerFactory factory = null;
     try {
-      RequestHandlerFactory factory = getRequestHandlerFactory();
+      factory = getRequestHandlerFactory();
       if (factory != null)
-        handler = factory.newRequestHandler(abderaServer,requestContext);
+        handler = factory.newRequestHandler(abderaServer);
       if (handler != null) {
         responseContext = handler.invoke(requestContext);
       } else {
@@ -121,10 +122,13 @@
           AbderaServerException.Code.NOTFOUND, 
           "Handler Not Found", "");
       }
-    } catch (AbderaServerException exception) {
+    } catch (AbderaServerException exception) {  
       responseContext = exception;
     } catch (Throwable t) {
       responseContext = new AbderaServerException(t);
+    } finally {
+      if (factory != null && handler != null)
+        factory.releaseRequestHandler(handler);
     }
     doOutput(response, responseContext); 
   }
@@ -152,8 +156,7 @@
               response.setHeader(entry.getKey(), value.toString());
           }
         }
-      }
-      
+      }  
       if (context.hasEntity())
         context.writeTo(response.getOutputStream());
     } else {

Modified: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandler.java?view=diff&rev=441582&r1=441581&r2=441582
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandler.java
(original)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandler.java
Fri Sep  8 10:25:44 2006
@@ -27,6 +27,10 @@
   
   public AbstractRequestHandler() {}
   
+  public void clean() {
+    // by default, there is nothing to clean
+  }
+  
   public ResponseContext invoke(
     RequestContext requestContext)
       throws AbderaServerException {
@@ -129,6 +133,5 @@
         getResourceType(requestContext)));
     throw notallowed;
   }
-  
   
 }

Added: incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandlerFactory.java
URL: http://svn.apache.org/viewvc/incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandlerFactory.java?view=auto&rev=441582
==============================================================================
--- incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandlerFactory.java
(added)
+++ incubator/abdera/java/trunk/server/src/main/java/org/apache/abdera/protocol/server/util/AbstractRequestHandlerFactory.java
Fri Sep  8 10:25:44 2006
@@ -0,0 +1,54 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements.  The ASF licenses this file to You
+* 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.  For additional information regarding
+* copyright in this work, please see the NOTICE file in the top level
+* directory of this distribution.
+*/
+package org.apache.abdera.protocol.server.util;
+
+import java.util.Stack;
+
+import org.apache.abdera.protocol.server.AbderaServer;
+import org.apache.abdera.protocol.server.RequestHandler;
+import org.apache.abdera.protocol.server.RequestHandlerFactory;
+import org.apache.abdera.protocol.server.exceptions.AbderaServerException;
+
+public abstract class AbstractRequestHandlerFactory 
+  implements RequestHandlerFactory {
+
+  private static Stack<RequestHandler> requestHandlerPool = new Stack<RequestHandler>();
+  
+  public RequestHandler newRequestHandler(
+    AbderaServer abderaServer) 
+      throws AbderaServerException {
+    RequestHandler rh = getRequestHandler(abderaServer);
+    return rh;
+  }
+  
+  private synchronized RequestHandler getRequestHandler(
+    AbderaServer abderaServer) {
+      if (!requestHandlerPool.empty()) 
+        return requestHandlerPool.pop();
+      return newRequestHandlerInstance(abderaServer);
+  }
+
+  public synchronized void releaseRequestHandler(RequestHandler handler) {
+    handler.clean(); // tell the handler to clean any internal state
+    requestHandlerPool.push(handler);
+  }
+  
+  protected abstract RequestHandler newRequestHandlerInstance( 
+    AbderaServer abderaServer);
+  
+}



Mime
View raw message