Author: mykee
Date: Thu Jul  1 16:21:12 2010
New Revision: 959710

URL: http://svn.apache.org/viewvc?rev=959710&view=rev
Log:
SLING-1576 Provide a Sling request listener independent from a specific HTTP 
service

Added:
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java
   (with props)
    
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java
   (with props)
    
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java
   (with props)
Modified:
    
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java

Added: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java?rev=959710&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java
 (added)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java
 Thu Jul  1 16:21:12 2010
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.
+ */
+
+package org.apache.sling.api.request;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+
+/**
+ * represents an event published by the Sling engine while
+ * dispatching a request.
+ *  
+ * @see org.apache.sling.api.request.SlingRequestListener
+*/
+public class SlingRequestEvent {
+       private ServletContext sc;
+       private ServletRequest request;
+       private EventType type; 
+       
+       /**
+        * type of the event  
+        */
+       public enum EventType { EVENT_INIT, EVENT_DESTROY };
+       
+       public SlingRequestEvent (ServletContext sc, ServletRequest request, 
EventType type )
+       {
+               this.sc = sc;
+               this.request = request;
+               this.type = type;
+       }
+       
+       /**
+        * Gets the actual servlet context object as <code>ServletContext</code>
+        * @return
+        */
+       public ServletContext getServletContext()
+       {
+               return sc;
+       }
+       
+       /**
+        * Gets the actual request object as <code>ServletRequest</code>
+        * @return the actual request object as <code>ServletRequest</code>
+        */
+       public ServletRequest getServletRequest()
+       {
+               return request;
+       }
+       
+       /**
+        * get the type of the event, eg. EVENT_INIT or EVENT_DESTROY
+        * @return the type of the event as <code>EventType</code>, 
+        * eg. EVENT_INIT or EVENT_DESTROY
+        */
+       public EventType getType ()
+       {
+               return type;
+       }
+}

Propchange: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestEvent.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java?rev=959710&view=auto
==============================================================================
--- 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java
 (added)
+++ 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java
 Thu Jul  1 16:21:12 2010
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.
+ */
+
+package org.apache.sling.api.request;
+
+/**
+ * Implementations of this service interface receive notifications about 
+ * changes to Sling request of the Sling application they are part of. 
+ * To receive notification events, the implementation class must be 
+ * registered as an OSGi service with the service name 
+ * org.apache.sling.api.request.SlingRequestListener. 
+ */
+public interface SlingRequestListener {
+       
+       static final String SERVICE_NAME = 
"org.apache.sling.api.request.SlingRequestListener"; 
+
+       /**
+        * This method is called from the Sling application for every
+        * <code>EventType</code> appearing during the dispatching of
+        * a Sling request  
+        * 
+        * @param sre the object representing the event
+        * 
+        * @see org.apache.sling.api.request.SlingRequestEvent.EventType
+        */
+       public void onEvent( SlingRequestEvent sre );
+}

Propchange: 
sling/trunk/bundles/api/src/main/java/org/apache/sling/api/request/SlingRequestListener.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java?rev=959710&r1=959709&r2=959710&view=diff
==============================================================================
--- 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
 (original)
+++ 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingMainServlet.java
 Thu Jul  1 16:21:12 2010
@@ -52,6 +52,7 @@ import org.apache.sling.api.adapter.Adap
 import org.apache.sling.api.request.RequestPathInfo;
 import org.apache.sling.api.request.RequestProgressTracker;
 import org.apache.sling.api.request.ResponseUtil;
+import org.apache.sling.api.request.SlingRequestEvent;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceNotFoundException;
 import org.apache.sling.api.resource.ResourceResolver;
@@ -62,6 +63,7 @@ import org.apache.sling.commons.osgi.Osg
 import org.apache.sling.engine.impl.filter.RequestSlingFilterChain;
 import org.apache.sling.engine.impl.filter.SlingComponentFilterChain;
 import org.apache.sling.engine.impl.filter.SlingFilterChainHelper;
+import org.apache.sling.engine.impl.helper.RequestListenerManager;
 import org.apache.sling.engine.impl.helper.SlingFilterConfig;
 import org.apache.sling.engine.impl.helper.SlingServletContext;
 import org.apache.sling.engine.impl.log.RequestLogger;
@@ -171,6 +173,8 @@ public class SlingMainServlet extends Ge
     private SlingFilterChainHelper requestFilterChain = new 
SlingFilterChainHelper();
 
     private SlingFilterChainHelper innerFilterChain = new 
SlingFilterChainHelper();
+    
+    private RequestListenerManager requestListenerManager; 
 
     private boolean allowTrace = DEFAULT_ALLOW_TRACE;
 
@@ -183,9 +187,12 @@ public class SlingMainServlet extends Ge
 
         if (req instanceof HttpServletRequest
             && res instanceof HttpServletResponse) {
-
+               
             HttpServletRequest request = (HttpServletRequest) req;
 
+               requestListenerManager.sendEvent( new SlingRequestEvent( 
getServletContext(), 
+                               request, SlingRequestEvent.EventType.EVENT_INIT 
) );
+
             // set the thread name according to the request
             String threadName = setThreadName(request);
 
@@ -238,6 +245,8 @@ public class SlingMainServlet extends Ge
 
             } finally {
 
+               requestListenerManager.sendEvent( new SlingRequestEvent( 
getServletContext(), 
+                               request, 
SlingRequestEvent.EventType.EVENT_DESTROY ) );
                 // reset the thread name
                 if (threadName != null) {
                     Thread.currentThread().setName(threadName);
@@ -621,6 +630,9 @@ public class SlingMainServlet extends Ge
                 initFilter(componentContext, serviceReference);
             }
         }
+        
+        // initialize requestListenerManager
+        requestListenerManager = new RequestListenerManager( bundleContext );
 
         // try to setup configuration printer
         try {

Added: 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java?rev=959710&view=auto
==============================================================================
--- 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java
 (added)
+++ 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java
 Thu Jul  1 16:21:12 2010
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  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.
+ */
+
+package org.apache.sling.engine.impl.helper;
+
+import org.apache.sling.api.request.SlingRequestEvent;
+import org.apache.sling.api.request.SlingRequestListener;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RequestListenerManager  {
+       
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    private ServiceTracker serviceTracker;
+
+       public RequestListenerManager ( BundleContext context )
+       {
+               serviceTracker = new ServiceTracker( context, 
SlingRequestListener.SERVICE_NAME, null );
+               serviceTracker.open();
+               
+       }
+       
+       public void sendEvent ( SlingRequestEvent event )
+       {
+               Object[] services = serviceTracker.getServices();
+               if ( services != null )
+               {
+                       for ( Object service : services )
+                       {
+                               if ( service instanceof SlingRequestListener )
+                               {
+                                       ( (SlingRequestListener) service 
).onEvent( event );
+                               }
+                               else
+                               {
+                                       log.error( "Implementation of service 
named " + SlingRequestListener.SERVICE_NAME + 
+                                                       " does not implement 
service interface " + SlingRequestListener.class.getName() + "." );
+                               }
+                       }
+               }
+       }
+       
+
+}

Propchange: 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/helper/RequestListenerManager.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to