You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2015/05/26 22:30:06 UTC

svn commit: r1681850 - in /felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal: handler/ registry/ whiteboard/

Author: cziegeler
Date: Tue May 26 20:30:06 2015
New Revision: 1681850

URL: http://svn.apache.org/r1681850
Log:
FELIX-4904 : Provide a way to associate whiteboard services with the default context of the http service

Added:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java   (with props)
Modified:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ContextHandler.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/PerContextHandlerRegistry.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ContextHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ContextHandler.java?rev=1681850&r1=1681849&r2=1681850&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ContextHandler.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ContextHandler.java Tue May 26 20:30:06 2015
@@ -16,42 +16,13 @@
  */
 package org.apache.felix.http.base.internal.handler;
 
-import javax.annotation.Nonnull;
-
 import org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry;
-import org.apache.felix.http.base.internal.runtime.FilterInfo;
-import org.apache.felix.http.base.internal.runtime.ListenerInfo;
 import org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo;
-import org.apache.felix.http.base.internal.runtime.ServletInfo;
-import org.apache.felix.http.base.internal.runtime.WhiteboardServiceInfo;
 
 public interface ContextHandler
 {
 
     ServletContextHelperInfo getContextInfo();
 
-    /**
-     * Create a servlet handler
-     * @param servletInfo The servlet info
-     * @return {@code null} if the servlet context could not be created, a handler otherwise
-     */
-    ServletHandler getServletContextAndCreateServletHandler(@Nonnull final ServletInfo servletInfo);
-
-    /**
-     * Create a filter handler
-     * @param info The filter info
-     * @return {@code null} if the servlet context could not be created, a handler otherwise
-     */
-    FilterHandler getServletContextAndCreateFilterHandler(@Nonnull final FilterInfo info);
-
-    /**
-     * Create a listener handler
-     * @param info The listener info
-     * @return {@code null} if the servlet context could not be created, a handler otherwise
-     */
-    ListenerHandler getServletContextAndCreateListenerHandler(@Nonnull final ListenerInfo info);
-
-    void ungetServletContext(@Nonnull final WhiteboardServiceInfo<?> info);
-
     PerContextHandlerRegistry getRegistry();
 }

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/PerContextHandlerRegistry.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/PerContextHandlerRegistry.java?rev=1681850&r1=1681849&r2=1681850&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/PerContextHandlerRegistry.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/PerContextHandlerRegistry.java Tue May 26 20:30:06 2015
@@ -20,18 +20,15 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.servlet.DispatcherType;
 
-import org.apache.felix.http.base.internal.handler.ContextHandler;
 import org.apache.felix.http.base.internal.handler.FilterHandler;
 import org.apache.felix.http.base.internal.handler.ListenerHandler;
 import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
 import org.apache.felix.http.base.internal.runtime.ListenerInfo;
-import org.apache.felix.http.base.internal.runtime.ResourceInfo;
 import org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 import org.apache.felix.http.base.internal.service.HttpServiceFactory;
-import org.osgi.service.http.runtime.dto.DTOConstants;
 import org.osgi.service.http.runtime.dto.ServletContextDTO;
 
 /**
@@ -150,125 +147,6 @@ public final class PerContextHandlerRegi
     }
 
     /**
-     * Register a servlet.
-     * @param contextInfo The servlet context helper info
-     * @param servletInfo The servlet info
-     */
-    public int registerServlet(@Nonnull final ContextHandler contextHandler,
-            @Nonnull final ServletInfo servletInfo)
-    {
-        final ServletHandler handler = contextHandler.getServletContextAndCreateServletHandler(servletInfo);
-        if ( handler == null )
-        {
-            return DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
-        }
-        this.servletRegistry.addServlet(handler);
-        this.errorPageRegistry.addServlet(handler);
-        return -1;
-    }
-
-    /**
-     * Unregister a servlet
-     * @param contextInfo The servlet context helper info
-     * @param servletInfo The servlet info
-     */
-    public void unregisterServlet(@Nonnull final ContextHandler contextHandler, @Nonnull final ServletInfo servletInfo)
-    {
-        this.servletRegistry.removeServlet(servletInfo, true);
-        this.errorPageRegistry.removeServlet(servletInfo, true);
-        contextHandler.ungetServletContext(servletInfo);
-    }
-
-    /**
-     * Register a filter
-     * @param contextInfo The servlet context helper info
-     * @param filterInfo The filter info
-     */
-    public int registerFilter(@Nonnull  final ContextHandler contextHandler,
-            @Nonnull final FilterInfo filterInfo)
-    {
-        final FilterHandler handler = contextHandler.getServletContextAndCreateFilterHandler(filterInfo);
-        if ( handler == null )
-        {
-            return DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
-        }
-        this.filterRegistry.addFilter(handler);
-        return -1;
-    }
-
-    /**
-     * Unregister a filter
-     * @param contextInfo The servlet context helper info
-     * @param filterInfo The filter info
-     */
-    public void unregisterFilter(@Nonnull final ContextHandler contextHandler, @Nonnull final FilterInfo filterInfo)
-    {
-        this.filterRegistry.removeFilter(filterInfo, true);
-        contextHandler.ungetServletContext(filterInfo);
-    }
-
-    /**
-     * Register listeners
-     *
-     * @param contextHandler The context handler
-     * @param info The listener info
-     * @return {@code -1} on successful registration, failure code otherwise
-     */
-    public int registerListeners(@Nonnull final ContextHandler contextHandler,
-            @Nonnull final ListenerInfo info)
-    {
-        final ListenerHandler handler = contextHandler.getServletContextAndCreateListenerHandler(info);
-        if ( handler == null )
-        {
-            return DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
-        }
-        this.eventListenerRegistry.addListeners(handler);
-        return -1;
-    }
-
-    /**
-     * Unregister listeners
-     *
-     * @param contextHandler The context handler
-     * @param info The listener info
-     */
-    public void unregisterListeners(@Nonnull final ContextHandler contextHandler, @Nonnull final ListenerInfo info)
-    {
-        this.eventListenerRegistry.removeListeners(info);
-        contextHandler.ungetServletContext(info);
-    }
-
-    /**
-     * Register a resource.
-     * @param contextInfo The servlet context helper info
-     * @param resourceInfo The resource info
-     */
-    public int registerResource(@Nonnull final ContextHandler contextHandler,
-            @Nonnull final ResourceInfo resourceInfo)
-    {
-        final ServletInfo servletInfo = new ServletInfo(resourceInfo);
-
-        final ServletHandler handler = contextHandler.getServletContextAndCreateServletHandler(servletInfo);
-        if ( handler == null )
-        {
-            return DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
-        }
-        this.servletRegistry.addServlet(handler);
-        return -1;
-    }
-
-    /**
-     * Unregister a resource.
-     * @param contextInfo The servlet context helper info
-     * @param resourceInfo The resource info
-     */
-    public void unregisterResource(@Nonnull final ContextHandler contextHandler, @Nonnull final ResourceInfo resourceInfo)
-    {
-        this.servletRegistry.removeServlet(new ServletInfo(resourceInfo), true);
-        contextHandler.ungetServletContext(resourceInfo);
-    }
-
-    /**
      * Get filter handlers for the request uri
      * @param servletHandler The servlet handler (might be null)
      * @param dispatcherType The dispatcher type
@@ -320,38 +198,62 @@ public final class PerContextHandlerRegi
     }
 
     /**
-     * Add a filter for the http service.
-     * @param handler The filter handler
-     */
-    public void registerFilter(@Nonnull final FilterHandler handler) {
-        this.filterRegistry.addFilter(handler);
-    }
-
-    /**
-     * Add a servlet for the http service.
+     * Add a servlet
      * @param handler The servlet handler
      */
-    public void registerServlet(@Nonnull final ServletHandler handler) {
+    public void registerServlet(@Nonnull final ServletHandler handler)
+    {
         this.servletRegistry.addServlet(handler);
         this.errorPageRegistry.addServlet(handler);
     }
 
     /**
-     * Remove a servlet for the http service.
+     * Remove a servlet
      * @param servletInfo The servlet info
      * @param destroy Destroy the servlet
      */
-    public void unregisterServlet(@Nonnull final ServletInfo servletInfo, final boolean destroy) {
+    public void unregisterServlet(@Nonnull final ServletInfo servletInfo, final boolean destroy)
+    {
         this.servletRegistry.removeServlet(servletInfo, destroy);
         this.errorPageRegistry.removeServlet(servletInfo, destroy);
     }
 
     /**
-     * Remove a filter for the http service.
+     * Add a filter
+     * @param handler The filter handler
+     */
+    public void registerFilter(@Nonnull final FilterHandler handler)
+    {
+        this.filterRegistry.addFilter(handler);
+    }
+
+    /**
+     * Remove a filter
      * @param info The filter info
      * @param destroy Destroy the filter
      */
-    public void unregisterFilter(@Nonnull final FilterInfo info, final boolean destroy) {
+    public void unregisterFilter(@Nonnull final FilterInfo info, final boolean destroy)
+    {
         this.filterRegistry.removeFilter(info, destroy);
     }
+
+    /**
+     * Register listeners
+     * @param listenerHandler
+     */
+    public void registerListeners(@Nonnull final ListenerHandler listenerHandler)
+    {
+        this.eventListenerRegistry.addListeners(listenerHandler);
+    }
+
+    /**
+     * Unregister listeners
+     *
+     * @param info The listener info
+     */
+    public void unregisterListeners(@Nonnull final ListenerInfo info)
+    {
+        this.eventListenerRegistry.removeListeners(info);
+    }
+
 }

Added: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java?rev=1681850&view=auto
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java (added)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java Tue May 26 20:30:06 2015
@@ -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.felix.http.base.internal.whiteboard;
+
+import org.apache.felix.http.base.internal.context.ExtServletContext;
+import org.apache.felix.http.base.internal.handler.ContextHandler;
+import org.apache.felix.http.base.internal.registry.PerContextHandlerRegistry;
+import org.apache.felix.http.base.internal.runtime.ServletContextHelperInfo;
+import org.osgi.framework.Bundle;
+
+public class HttpServiceContextHandler implements ContextHandler, Comparable<ContextHandler>
+{
+    /** The info object for the context. */
+    private final ServletContextHelperInfo info;
+
+    private final PerContextHandlerRegistry registry;
+
+    private final ExtServletContext webContext;
+
+    /** The http bundle. */
+    private final Bundle httpBundle;
+
+    public HttpServiceContextHandler(final ServletContextHelperInfo info,
+            final PerContextHandlerRegistry registry,
+            final ExtServletContext webContext,
+            final Bundle httpBundle)
+    {
+        this.info = info;
+        this.registry = registry;
+        this.webContext = webContext;
+        this.httpBundle = httpBundle;
+    }
+
+    @Override
+    public ServletContextHelperInfo getContextInfo()
+    {
+        return this.info;
+    }
+
+    @Override
+    public int compareTo(final ContextHandler o)
+    {
+        return this.info.compareTo(o.getContextInfo());
+    }
+
+    @Override
+    public PerContextHandlerRegistry getRegistry()
+    {
+        return this.registry;
+    }
+}

Propchange: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/HttpServiceContextHandler.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java?rev=1681850&r1=1681849&r2=1681850&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardContextHandler.java Tue May 26 20:30:06 2015
@@ -201,7 +201,6 @@ public final class WhiteboardContextHand
      * @param servletInfo The servlet info
      * @return {@code null} if the servlet context could not be created, a handler otherwise
      */
-    @Override
     public ServletHandler getServletContextAndCreateServletHandler(@Nonnull final ServletInfo servletInfo)
     {
         final ExtServletContext servletContext = this.getServletContext(servletInfo.getServiceReference().getBundle());
@@ -234,7 +233,6 @@ public final class WhiteboardContextHand
      * @param info The filter info
      * @return {@code null} if the servlet context could not be created, a handler otherwise
      */
-    @Override
     public FilterHandler getServletContextAndCreateFilterHandler(@Nonnull final FilterInfo info)
     {
         final ExtServletContext servletContext = this.getServletContext(info.getServiceReference().getBundle());
@@ -255,7 +253,6 @@ public final class WhiteboardContextHand
      * @param info The listener info
      * @return {@code null} if the servlet context could not be created, a handler otherwise
      */
-    @Override
     public ListenerHandler getServletContextAndCreateListenerHandler(@Nonnull final ListenerInfo info)
     {
         final ExtServletContext servletContext = this.getServletContext(info.getServiceReference().getBundle());
@@ -271,7 +268,6 @@ public final class WhiteboardContextHand
         return handler;
     }
 
-    @Override
     public void ungetServletContext(@Nonnull final WhiteboardServiceInfo<?> info)
     {
         this.ungetServletContext(info.getServiceReference().getBundle());

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java?rev=1681850&r1=1681849&r2=1681850&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java Tue May 26 20:30:06 2015
@@ -41,9 +41,10 @@ import javax.servlet.http.HttpSessionEve
 
 import org.apache.felix.http.base.internal.console.HttpServicePlugin;
 import org.apache.felix.http.base.internal.context.ExtServletContext;
-import org.apache.felix.http.base.internal.handler.ContextHandler;
+import org.apache.felix.http.base.internal.handler.FilterHandler;
 import org.apache.felix.http.base.internal.handler.HttpSessionWrapper;
 import org.apache.felix.http.base.internal.handler.ListenerHandler;
+import org.apache.felix.http.base.internal.handler.ServletHandler;
 import org.apache.felix.http.base.internal.logger.SystemLogger;
 import org.apache.felix.http.base.internal.registry.HandlerRegistry;
 import org.apache.felix.http.base.internal.runtime.AbstractInfo;
@@ -553,7 +554,7 @@ public final class WhiteboardManager
                     }
                     else
                     {
-                        for(final ContextHandler h : handlerList)
+                        for(final WhiteboardContextHandler h : handlerList)
                         {
                             this.registerWhiteboardService(h, info);
                             if ( info instanceof ListenerInfo && ((ListenerInfo)info).isListenerType(ServletContextListener.class.getName()) )
@@ -627,27 +628,61 @@ public final class WhiteboardManager
      * @param handler Context handler
      * @param info Whiteboard service info
      */
-    private void registerWhiteboardService(final ContextHandler handler, final WhiteboardServiceInfo<?> info)
+    private void registerWhiteboardService(final WhiteboardContextHandler handler, final WhiteboardServiceInfo<?> info)
     {
         try
         {
             int failureCode = -1;
             if ( info instanceof ServletInfo )
             {
-                failureCode = handler.getRegistry().registerServlet(handler, (ServletInfo)info);
+                final ServletHandler servletHandler = handler.getServletContextAndCreateServletHandler((ServletInfo)info);
+                if ( servletHandler == null )
+                {
+                    failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
+                }
+                else
+                {
+                    handler.getRegistry().registerServlet(servletHandler);
+                }
             }
             else if ( info instanceof FilterInfo )
             {
-                failureCode = handler.getRegistry().registerFilter(handler, (FilterInfo)info);
+                final FilterHandler filterHandler = handler.getServletContextAndCreateFilterHandler((FilterInfo)info);
+                if ( filterHandler == null )
+                {
+                    failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
+                }
+                else
+                {
+                    handler.getRegistry().registerFilter(filterHandler);
+                }
             }
             else if ( info instanceof ResourceInfo )
             {
-                failureCode = handler.getRegistry().registerResource(handler, (ResourceInfo)info);
+                final ServletInfo servletInfo = new ServletInfo((ResourceInfo)info);
+
+                final ServletHandler servleHandler = handler.getServletContextAndCreateServletHandler(servletInfo);
+                if ( servleHandler == null )
+                {
+                    failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
+                }
+                else
+                {
+                    handler.getRegistry().registerServlet(servleHandler);
+                }
             }
 
             else if ( info instanceof ListenerInfo )
             {
-                failureCode = handler.getRegistry().registerListeners(handler, (ListenerInfo) info);
+                final ListenerHandler listenerHandler = handler.getServletContextAndCreateListenerHandler((ListenerInfo)info);
+                if ( listenerHandler == null )
+                {
+                    failureCode = DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
+                }
+                else
+                {
+                    handler.getRegistry().registerListeners(listenerHandler);
+                }
             }
             else
             {
@@ -679,20 +714,24 @@ public final class WhiteboardManager
         {
             if ( info instanceof ServletInfo )
             {
-                handler.getRegistry().unregisterServlet(handler, (ServletInfo)info);
+                handler.getRegistry().unregisterServlet((ServletInfo)info, true);
+                handler.ungetServletContext(info);
             }
             else if ( info instanceof FilterInfo )
             {
-                handler.getRegistry().unregisterFilter(handler, (FilterInfo)info);
+                handler.getRegistry().unregisterFilter((FilterInfo)info, true);
+                handler.ungetServletContext(info);
             }
             else if ( info instanceof ResourceInfo )
             {
-                handler.getRegistry().unregisterResource(handler, (ResourceInfo)info);
+                handler.getRegistry().unregisterServlet(new ServletInfo((ResourceInfo)info), true);
+                handler.ungetServletContext(info);
             }
 
             else if ( info instanceof ListenerInfo )
             {
-                handler.getRegistry().unregisterListeners(handler, (ListenerInfo) info);
+                handler.getRegistry().unregisterListeners((ListenerInfo) info);
+                handler.ungetServletContext(info);
             }
         }
         catch (final Exception e)