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 2016/09/01 08:17:51 UTC

svn commit: r1758726 - in /felix/sandbox/http-base-r7/src: main/java/org/apache/felix/http/base/internal/whiteboard/ test/java/org/apache/felix/http/base/internal/whiteboard/

Author: cziegeler
Date: Thu Sep  1 08:17:50 2016
New Revision: 1758726

URL: http://svn.apache.org/viewvc?rev=1758726&view=rev
Log:
FELIX-5297 : [RFC 223] 5.1 Whiteboard Services and Http Service

Modified:
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandler.java
    felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
    felix/sandbox/http-base-r7/src/test/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandlerTest.java

Modified: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandler.java?rev=1758726&r1=1758725&r2=1758726&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandler.java (original)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandler.java Thu Sep  1 08:17:50 2016
@@ -16,12 +16,20 @@
  */
 package org.apache.felix.http.base.internal.whiteboard;
 
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_SERVLET_CONTEXT_FAILURE;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_UNKNOWN;
+import static org.osgi.service.http.runtime.dto.DTOConstants.FAILURE_REASON_VALIDATION_FAILED;
+
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.felix.http.base.internal.logger.SystemLogger;
 import org.apache.felix.http.base.internal.runtime.AbstractInfo;
 import org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder;
 
@@ -42,13 +50,61 @@ public class FailureStateHandler {
         this.serviceFailures.clear();
     }
 
-    public void add(final AbstractInfo<?> info, final int reason)
+    public void addFailure(final AbstractInfo<?> info, final int reason, final Exception ex)
+    {
+        this.addFailure(info, 0, reason, ex);
+    }
+
+    public void addFailure(final AbstractInfo<?> info, final int reason)
     {
-        this.add(info, 0, reason);
+        this.addFailure(info, 0, reason);
     }
 
-    public void add(final AbstractInfo<?> info, final long contextId, final int reason)
+    public void addFailure(final AbstractInfo<?> info, final long contextId, final int reason)
     {
+        this.addFailure(info, contextId, reason, null);
+    }
+
+    public void addFailure(final AbstractInfo<?> info, final long contextId, final int reason, final Exception ex)
+    {
+        final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
+        final String serviceInfo;
+        if ( info.getServiceReference() == null ) {
+            serviceInfo = "with id " + info.getServiceId();
+        } else {
+            serviceInfo = String.valueOf(info.getServiceId()) +
+                    " (bundle " + info.getServiceReference().getBundle().getSymbolicName()
+                    + " reference " + info.getServiceReference() + ")";
+        }
+        if ( reason == FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING )
+        {
+            SystemLogger.debug("Ignoring unmatching " + type + " service " + serviceInfo);
+        }
+        else if ( reason == FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE )
+        {
+            SystemLogger.debug("Ignoring shadowed " + type + " service " + serviceInfo);
+        }
+        else if ( reason == FAILURE_REASON_SERVICE_NOT_GETTABLE )
+        {
+            SystemLogger.error("Ignoring ungettable " + type + " service " + serviceInfo, ex);
+        }
+        else if ( reason == FAILURE_REASON_VALIDATION_FAILED )
+        {
+            SystemLogger.debug("Ignoring invalid " + type + " service " + serviceInfo);
+        }
+        else if ( reason == FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING )
+        {
+            SystemLogger.debug("Ignoring unmatched " + type + " service " + serviceInfo);
+        }
+        else if ( reason == FAILURE_REASON_SERVLET_CONTEXT_FAILURE )
+        {
+            SystemLogger.debug("Servlet context " + String.valueOf(contextId) + " failure: Ignoring " + type + " service " + serviceInfo);
+        }
+        else if ( reason == FAILURE_REASON_UNKNOWN)
+        {
+            SystemLogger.error("Exception while registering " + type + " service " + serviceInfo, ex);
+        }
+
         FailureStatus status = serviceFailures.get(info);
         if ( status == null )
         {

Modified: felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java?rev=1758726&r1=1758725&r2=1758726&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java (original)
+++ felix/sandbox/http-base-r7/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java Thu Sep  1 08:17:50 2016
@@ -285,7 +285,7 @@ public final class WhiteboardManager
 
     /**
      * Handle session id changes
-     * @param session The session where the id changed
+     * @param event The session event
      * @param oldSessionId The old session id
      * @param contextIds The context ids using that session
      */
@@ -317,22 +317,28 @@ public final class WhiteboardManager
         final List<WhiteboardServiceInfo<?>> services = new ArrayList<WhiteboardServiceInfo<?>>();
         for(final Map.Entry<WhiteboardServiceInfo<?>, List<WhiteboardContextHandler>> entry : this.servicesMap.entrySet())
         {
-            if ( entry.getKey().getContextSelectionFilter().match(handler.getContextInfo().getServiceReference()) )
+            final WhiteboardServiceInfo<?> info = entry.getKey();
+
+            if ( info.getContextSelectionFilter().match(handler.getContextInfo().getServiceReference()) )
             {
-                entry.getValue().add(handler);
-                if ( entry.getValue().size() == 1 )
-                {
-                    this.failureStateHandler.remove(entry.getKey());
-                }
-                if ( entry.getKey() instanceof ListenerInfo && ((ListenerInfo)entry.getKey()).isListenerType(ServletContextListener.class.getName()) )
-                {
-                    // servlet context listeners will be registered directly
-                    this.registerWhiteboardService(handler, entry.getKey());
-                }
-                else
+                final int reason = checkForServletRegistrationInHttpServiceContext(handler, info);
+                if ( reason == -1 )
                 {
-                    // registration of other services will be delayed
-                    services.add(entry.getKey());
+                    entry.getValue().add(handler);
+                    if ( entry.getValue().size() == 1 )
+                    {
+                        this.failureStateHandler.remove(info);
+                    }
+                    if ( info instanceof ListenerInfo && ((ListenerInfo)info).isListenerType(ServletContextListener.class.getName()) )
+                    {
+                        // servlet context listeners will be registered directly
+                        this.registerWhiteboardService(handler, info);
+                    }
+                    else
+                    {
+                        // registration of other services will be delayed
+                        services.add(info);
+                    }
                 }
             }
         }
@@ -376,9 +382,7 @@ public final class WhiteboardManager
                 }
                 if ( entry.getValue().isEmpty() )
                 {
-                    final String type = entry.getKey().getClass().getSimpleName().substring(0, entry.getKey().getClass().getSimpleName().length() - 4);
-                    SystemLogger.debug("Ignoring unmatching " + type + " service " + entry.getKey().getServiceReference());
-                    this.failureStateHandler.add(entry.getKey(), FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
+                    this.failureStateHandler.addFailure(entry.getKey(), FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
                 }
             }
         }
@@ -433,16 +437,12 @@ public final class WhiteboardManager
                                 final WhiteboardContextHandler oldHead = handlerList.get(1);
                                 this.deactivate(oldHead);
 
-                                final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                                SystemLogger.debug("Ignoring shadowed " + type + " service " + info.getServiceReference());
-                                this.failureStateHandler.add(oldHead.getContextInfo(), FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+                                this.failureStateHandler.addFailure(oldHead.getContextInfo(), FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
                             }
                         }
                         else
                         {
-                            final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                            SystemLogger.error("Ignoring ungettable " + type + " service " + info.getServiceReference(), null);
-                            this.failureStateHandler.add(handler.getContextInfo(), DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE);
+                            this.failureStateHandler.addFailure(handler.getContextInfo(), DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE);
                         }
                     }
                     else
@@ -451,17 +451,13 @@ public final class WhiteboardManager
                         Collections.sort(handlerList);
                         this.contextMap.put(info.getName(), handlerList);
 
-                        final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                        SystemLogger.debug("Ignoring shadowed " + type + " service " + info.getServiceReference());
-                        this.failureStateHandler.add(handler.getContextInfo(), FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+                        this.failureStateHandler.addFailure(handler.getContextInfo(), FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
                     }
                 }
             }
             else
             {
-                final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                SystemLogger.debug("Ignoring invalid " + type + " service " + info.getServiceReference());
-                this.failureStateHandler.add(info, FAILURE_REASON_VALIDATION_FAILED);
+                this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
             }
             return true;
         }
@@ -522,9 +518,7 @@ public final class WhiteboardManager
                             {
                                 handlerList.remove(0);
 
-                                final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                                SystemLogger.error("Ignoring ungettable " + type + " service " + info.getServiceReference(), null);
-                                this.failureStateHandler.add(newHead.getContextInfo(), DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE);
+                                this.failureStateHandler.addFailure(newHead.getContextInfo(), DTOConstants.FAILURE_REASON_SERVICE_NOT_GETTABLE);
                             }
                         }
                     }
@@ -616,9 +610,7 @@ public final class WhiteboardManager
                     }
                     else
                     {
-                        final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                        SystemLogger.debug("Ignoring invalid " + type + " service " + info.getServiceReference());
-                        this.failureStateHandler.add(info, FAILURE_REASON_VALIDATION_FAILED);
+                        this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
                     }
                     return true;
                 }
@@ -628,24 +620,26 @@ public final class WhiteboardManager
                     this.servicesMap.put(info, handlerList);
                     if (handlerList.isEmpty())
                     {
-                        final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                        SystemLogger.debug("Ignoring unmatched " + type + " service " + info.getServiceReference());
-                        this.failureStateHandler.add(info, FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
+                        this.failureStateHandler.addFailure(info, FAILURE_REASON_NO_SERVLET_CONTEXT_MATCHING);
                     }
                     else
                     {
                         for(final WhiteboardContextHandler h : handlerList)
                         {
-                            this.registerWhiteboardService(h, info);
-                            if ( info instanceof ListenerInfo && ((ListenerInfo)info).isListenerType(ServletContextListener.class.getName()) )
+                            final int result = this.checkForServletRegistrationInHttpServiceContext(h, info);
+                            if ( result == -1)
                             {
-                                final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo)info);
-                                if ( handler != null )
+                                this.registerWhiteboardService(h, info);
+                                if ( info instanceof ListenerInfo && ((ListenerInfo)info).isListenerType(ServletContextListener.class.getName()) )
                                 {
-                                    final ServletContextListener listener = (ServletContextListener)handler.getListener();
-                                    if ( listener != null )
+                                    final ListenerHandler handler = h.getRegistry().getEventListenerRegistry().getServletContextListener((ListenerInfo)info);
+                                    if ( handler != null )
                                     {
-                                        EventListenerRegistry.contextInitialized(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
+                                        final ServletContextListener listener = (ServletContextListener)handler.getListener();
+                                        if ( listener != null )
+                                        {
+                                            EventListenerRegistry.contextInitialized(handler.getListenerInfo(), listener, new ServletContextEvent(handler.getContext()));
+                                        }
                                     }
                                 }
                             }
@@ -655,9 +649,7 @@ public final class WhiteboardManager
             }
             else
             {
-                final String type = info.getClass().getSimpleName().substring(0, info.getClass().getSimpleName().length() - 4);
-                SystemLogger.debug("Ignoring invalid " + type + " service " + info.getServiceReference());
-                this.failureStateHandler.add(info, FAILURE_REASON_VALIDATION_FAILED);
+                this.failureStateHandler.addFailure(info, FAILURE_REASON_VALIDATION_FAILED);
             }
             return true;
         }
@@ -665,6 +657,29 @@ public final class WhiteboardManager
     }
 
     /**
+     * Check if a registration for a servlet or resource is tried against the http context
+     * of the http service
+     * @param h The handler
+     * @param info The info
+     * @return {@code -1} if everything is ok, error code otherwise
+     */
+    private int checkForServletRegistrationInHttpServiceContext(final WhiteboardContextHandler h,
+            final WhiteboardServiceInfo<?> info)
+    {
+        if ( info instanceof ServletInfo || info instanceof ResourceInfo )
+        {
+            if ( h.getContextInfo().getServiceId() == HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID )
+            {
+                this.failureStateHandler.addFailure(info, h.getContextInfo().getServiceId(), DTOConstants.FAILURE_REASON_VALIDATION_FAILED);
+
+                return DTOConstants.FAILURE_REASON_VALIDATION_FAILED;
+            }
+        }
+
+        return -1;
+    }
+
+    /**
      * Remove whiteboard service from the registry.
      *
      * @param info The service id of the whiteboard service
@@ -810,15 +825,12 @@ public final class WhiteboardManager
             }
             if ( failureCode != -1 )
             {
-                final String type = info.getClass().getSimpleName().substring(0,info.getClass().getSimpleName().length() - 4);
-                SystemLogger.debug("Ignoring " + type + " service " + info.getServiceReference());
-                this.failureStateHandler.add(info, handler.getContextInfo().getServiceId(), failureCode);
+                this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), failureCode);
             }
         }
         catch (final Exception e)
         {
-            this.failureStateHandler.add(info, handler.getContextInfo().getServiceId(), FAILURE_REASON_UNKNOWN);
-            SystemLogger.error("Exception while registering whiteboard service " + info.getServiceReference(), e);
+            this.failureStateHandler.addFailure(info, handler.getContextInfo().getServiceId(), FAILURE_REASON_UNKNOWN, e);
         }
     }
 
@@ -905,17 +917,7 @@ public final class WhiteboardManager
         final FailedDTOHolder failedDTOHolder = new FailedDTOHolder();
 
         final Collection<ServletContextDTO> contextDTOs = new ArrayList<ServletContextDTO>();
-/*
-        // add the context for the http service
-        final ServletContextHelperInfo info = new ServletContextHelperInfo(Integer.MAX_VALUE,
-                HttpServiceFactory.HTTP_SERVICE_CONTEXT_SERVICE_ID,
-                HttpServiceFactory.HTTP_SERVICE_CONTEXT_NAME, "/", null);
-        final ServletContextDTO dto = ServletContextDTOBuilder.build(info, webContext, -1);
-        if ( registry.getRuntimeInfo(dto, failedDTOHolder) )
-        {
-            contextDTOs.add(dto);
-        }
-*/
+
         // get sort list of context handlers
         final List<WhiteboardContextHandler> contextHandlerList = new ArrayList<WhiteboardContextHandler>();
         synchronized ( this.contextMap )

Modified: felix/sandbox/http-base-r7/src/test/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandlerTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/http-base-r7/src/test/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandlerTest.java?rev=1758726&r1=1758725&r2=1758726&view=diff
==============================================================================
--- felix/sandbox/http-base-r7/src/test/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandlerTest.java (original)
+++ felix/sandbox/http-base-r7/src/test/java/org/apache/felix/http/base/internal/whiteboard/FailureStateHandlerTest.java Thu Sep  1 08:17:50 2016
@@ -51,7 +51,7 @@ public class FailureStateHandlerTest {
         final ServletInfo info = new ServletInfo("test", "/test", Collections.<String, String> emptyMap());
 
         final FailureStateHandler handler = new FailureStateHandler();
-        handler.add(info, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+        handler.addFailure(info, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
 
         final FailedDTOHolder holder = new FailedDTOHolder();
         handler.getRuntimeInfo(holder);
@@ -73,8 +73,8 @@ public class FailureStateHandlerTest {
         final ServletInfo info2 = new ServletInfo("test", "/test", Collections.<String, String> emptyMap());
 
         final FailureStateHandler handler = new FailureStateHandler();
-        handler.add(info1, 1L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
-        handler.add(info2, 2L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+        handler.addFailure(info1, 1L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+        handler.addFailure(info2, 2L, DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
 
         final FailedDTOHolder holder = new FailedDTOHolder();
         handler.getRuntimeInfo(holder);