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/02/14 14:04:14 UTC

svn commit: r1659791 - in /felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal: HttpServiceController.java runtime/HttpServiceRuntimeImpl.java whiteboard/ServletContextHelperManager.java whiteboard/WhiteboardHttpService.java

Author: cziegeler
Date: Sat Feb 14 13:04:13 2015
New Revision: 1659791

URL: http://svn.apache.org/r1659791
Log:
FELIX-4546 : Implement HttpServiceRuntime service

Removed:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/HttpServiceRuntimeImpl.java
Modified:
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java
    felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java?rev=1659791&r1=1659790&r2=1659791&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/HttpServiceController.java Sat Feb 14 13:04:13 2015
@@ -27,16 +27,12 @@ import org.apache.felix.http.base.intern
 import org.apache.felix.http.base.internal.handler.HandlerRegistry;
 import org.apache.felix.http.base.internal.handler.HttpServicePlugin;
 import org.apache.felix.http.base.internal.handler.HttpSessionWrapper;
-import org.apache.felix.http.base.internal.runtime.HttpServiceRuntimeImpl;
 import org.apache.felix.http.base.internal.service.HttpServiceFactory;
 import org.apache.felix.http.base.internal.service.listener.ServletContextAttributeListenerManager;
 import org.apache.felix.http.base.internal.service.listener.ServletRequestAttributeListenerManager;
 import org.apache.felix.http.base.internal.service.listener.ServletRequestListenerManager;
 import org.apache.felix.http.base.internal.whiteboard.WhiteboardHttpService;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.http.runtime.HttpServiceRuntime;
-import org.osgi.service.http.runtime.HttpServiceRuntimeConstants;
 
 public final class HttpServiceController
 {
@@ -45,21 +41,19 @@ public final class HttpServiceController
     private final Dispatcher dispatcher;
     private final HttpServicePlugin plugin;
     private final HttpServiceFactory httpServiceFactory;
-    private volatile WhiteboardHttpService whiteboardHttpService;
+    private final WhiteboardHttpService whiteboardHttpService;
 
-    private volatile ServiceRegistration<HttpServiceRuntime> runtimeServiceReg;
-    private final Hashtable<String, Object> runtimeServiceProps = new Hashtable<String, Object>();;
-
-    public HttpServiceController(BundleContext bundleContext)
+    public HttpServiceController(final BundleContext bundleContext)
     {
         this.bundleContext = bundleContext;
         this.registry = new HandlerRegistry();
         this.dispatcher = new Dispatcher(this.registry);
         this.plugin = new HttpServicePlugin(bundleContext, registry);
         this.httpServiceFactory = new HttpServiceFactory(this.bundleContext, this.registry);
+        this.whiteboardHttpService = new WhiteboardHttpService(this.bundleContext, this.registry, this.httpServiceFactory);
     }
 
-    public Dispatcher getDispatcher()
+    Dispatcher getDispatcher()
     {
         return this.dispatcher;
     }
@@ -104,17 +98,7 @@ public final class HttpServiceController
     public void setProperties(final Hashtable<String, Object> props)
     {
         this.httpServiceFactory.setProperties(props);
-
-        // runtime service gets the same props for now
-        this.runtimeServiceProps.clear();
-        this.runtimeServiceProps.putAll(props);
-
-        if (this.runtimeServiceReg != null)
-        {
-            this.runtimeServiceProps.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID_ATTRIBUTE,
-                    this.httpServiceFactory.getHttpServiceServiceId());
-            this.runtimeServiceReg.setProperties(this.runtimeServiceProps);
-        }
+        this.whiteboardHttpService.setProperties(props);
     }
 
     public void register(final ServletContext servletContext)
@@ -122,37 +106,22 @@ public final class HttpServiceController
         this.plugin.register();
 
         this.httpServiceFactory.start(servletContext);
+        this.whiteboardHttpService.start(servletContext);
 
-        this.runtimeServiceProps.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID_ATTRIBUTE,
-                this.httpServiceFactory.getHttpServiceServiceId());
-        this.runtimeServiceReg = this.bundleContext.registerService(HttpServiceRuntime.class,
-                new HttpServiceRuntimeImpl(),
-                this.runtimeServiceProps);
-
-        this.whiteboardHttpService = new WhiteboardHttpService(this.bundleContext,
-                servletContext,
-                this.registry,
-                this.runtimeServiceReg.getReference());
         this.dispatcher.setWhiteboardHttpService(this.whiteboardHttpService);
     }
 
     public void unregister()
     {
+        this.plugin.unregister();
+
         this.dispatcher.setWhiteboardHttpService(null);
-        if ( this.whiteboardHttpService != null )
-        {
-            this.whiteboardHttpService.close();
-            this.whiteboardHttpService = null;
-        }
 
-        if ( this.runtimeServiceReg != null )
+        if ( this.whiteboardHttpService != null )
         {
-            this.runtimeServiceReg.unregister();
-            this.runtimeServiceReg = null;
+            this.whiteboardHttpService.stop();
         }
 
-        this.plugin.unregister();
-
         if ( this.httpServiceFactory != null )
         {
             this.httpServiceFactory.stop();

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java?rev=1659791&r1=1659790&r2=1659791&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/ServletContextHelperManager.java Sat Feb 14 13:04:13 2015
@@ -17,6 +17,7 @@
 package org.apache.felix.http.base.internal.whiteboard;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Dictionary;
 import java.util.HashMap;
@@ -55,7 +56,6 @@ import org.osgi.framework.ServiceFactory
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.http.context.ServletContextHelper;
-import org.osgi.service.http.runtime.HttpServiceRuntime;
 import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
 
 public final class ServletContextHelperManager
@@ -74,8 +74,6 @@ public final class ServletContextHelperM
 
     private final Bundle bundle;
 
-    private final ServiceReference<HttpServiceRuntime> runtimeRef;
-
     private final Set<AbstractInfo<?>> invalidRegistrations = new ConcurrentSkipListSet<AbstractInfo<?>>();
 
     /**
@@ -84,13 +82,11 @@ public final class ServletContextHelperM
      */
     public ServletContextHelperManager(final BundleContext bundleContext,
             final ServletContext webContext,
-            final ServiceReference<HttpServiceRuntime> runtimeRef,
             final WhiteboardHttpService httpService)
     {
         this.httpService = httpService;
         this.webContext = webContext;
         this.bundle = bundleContext.getBundle();
-        this.runtimeRef = runtimeRef;
 
         final Dictionary<String, Object> props = new Hashtable<String, Object>();
         props.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME);
@@ -481,7 +477,7 @@ public final class ServletContextHelperM
             try
             {
                 final Filter f = this.bundle.getBundleContext().createFilter(target);
-                return f.match(this.runtimeRef);
+                return f.match(this.httpService.getServiceReference());
             }
             catch ( final InvalidSyntaxException ise)
             {
@@ -508,4 +504,18 @@ public final class ServletContextHelperM
         }
         return null;
     }
+
+    public Collection<ContextHandler> getContextHandlers()
+    {
+         final List<ContextHandler> handlers = new ArrayList<ContextHandler>();
+         synchronized ( this.contextMap )
+         {
+             for(final List<ContextHandler> handlerList : this.contextMap.values())
+             {
+                 final ContextHandler h = handlerList.get(0);
+                 handlers.add(h);
+             }
+         }
+         return handlers;
+    }
 }

Modified: felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java
URL: http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java?rev=1659791&r1=1659790&r2=1659791&view=diff
==============================================================================
--- felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java (original)
+++ felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardHttpService.java Sat Feb 14 13:04:13 2015
@@ -17,7 +17,11 @@
 package org.apache.felix.http.base.internal.whiteboard;
 
 import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import javax.annotation.Nonnull;
@@ -35,6 +39,7 @@ import org.apache.felix.http.base.intern
 import org.apache.felix.http.base.internal.runtime.FilterInfo;
 import org.apache.felix.http.base.internal.runtime.ResourceInfo;
 import org.apache.felix.http.base.internal.runtime.ServletInfo;
+import org.apache.felix.http.base.internal.service.HttpServiceFactory;
 import org.apache.felix.http.base.internal.whiteboard.tracker.FilterTracker;
 import org.apache.felix.http.base.internal.whiteboard.tracker.HttpSessionAttributeListenerTracker;
 import org.apache.felix.http.base.internal.whiteboard.tracker.HttpSessionListenerTracker;
@@ -48,20 +53,36 @@ import org.apache.felix.http.base.intern
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceObjects;
 import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.http.runtime.HttpServiceRuntime;
+import org.osgi.service.http.runtime.HttpServiceRuntimeConstants;
+import org.osgi.service.http.runtime.dto.ErrorPageDTO;
+import org.osgi.service.http.runtime.dto.FilterDTO;
+import org.osgi.service.http.runtime.dto.ListenerDTO;
+import org.osgi.service.http.runtime.dto.RequestInfoDTO;
+import org.osgi.service.http.runtime.dto.ResourceDTO;
+import org.osgi.service.http.runtime.dto.RuntimeDTO;
+import org.osgi.service.http.runtime.dto.ServletContextDTO;
+import org.osgi.service.http.runtime.dto.ServletDTO;
 import org.osgi.util.tracker.ServiceTracker;
 
-public final class WhiteboardHttpService
+public final class WhiteboardHttpService implements HttpServiceRuntime
 {
 
     private final HandlerRegistry handlerRegistry;
 
     private final BundleContext bundleContext;
 
-    private final ServletContextHelperManager contextManager;
+    private volatile ServletContextHelperManager contextManager;
 
     private final List<ServiceTracker<?, ?>> trackers = new ArrayList<ServiceTracker<?, ?>>();
 
+    private final Hashtable<String, Object> runtimeServiceProps = new Hashtable<String, Object>();;
+
+    private final HttpServiceFactory httpServiceFactory;
+
+    private volatile ServiceRegistration<HttpServiceRuntime> runtimeServiceReg;
+
     /**
      * Create a new whiteboard http service
      * @param bundleContext
@@ -69,13 +90,17 @@ public final class WhiteboardHttpService
      * @param handlerRegistry
      */
     public WhiteboardHttpService(final BundleContext bundleContext,
-            final ServletContext context,
             final HandlerRegistry handlerRegistry,
-            final ServiceReference<HttpServiceRuntime> runtimeRef)
+            final HttpServiceFactory httpServiceFactory)
     {
         this.handlerRegistry = handlerRegistry;
         this.bundleContext = bundleContext;
-        this.contextManager = new ServletContextHelperManager(bundleContext, context, runtimeRef, this);
+        this.httpServiceFactory = httpServiceFactory;
+    }
+
+    public void start(final ServletContext context)
+    {
+        this.contextManager = new ServletContextHelperManager(bundleContext, context, this);
 
         addTracker(new FilterTracker(bundleContext, contextManager));
         addTracker(new ServletTracker(bundleContext, this.contextManager));
@@ -90,16 +115,32 @@ public final class WhiteboardHttpService
 
         addTracker(new ServletRequestListenerTracker(bundleContext, this.contextManager));
         addTracker(new ServletRequestAttributeListenerTracker(bundleContext, this.contextManager));
+
+        this.runtimeServiceProps.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID_ATTRIBUTE,
+                this.httpServiceFactory.getHttpServiceServiceId());
+        this.runtimeServiceReg = this.bundleContext.registerService(HttpServiceRuntime.class,
+                this,
+                this.runtimeServiceProps);
     }
 
-    public void close()
+    public void stop()
     {
+        if ( this.runtimeServiceReg != null )
+        {
+            this.runtimeServiceReg.unregister();
+            this.runtimeServiceReg = null;
+        }
+
         for(final ServiceTracker<?, ?> t : this.trackers)
         {
             t.close();
         }
         this.trackers.clear();
-        this.contextManager.close();
+        if ( this.contextManager != null )
+        {
+            this.contextManager.close();
+            this.contextManager = null;
+        }
     }
 
     private void addTracker(ServiceTracker<?, ?> tracker)
@@ -108,6 +149,20 @@ public final class WhiteboardHttpService
         tracker.open();
     }
 
+    public void setProperties(final Hashtable<String, Object> props)
+    {
+        // runtime service gets the same props for now
+        this.runtimeServiceProps.clear();
+        this.runtimeServiceProps.putAll(props);
+
+        if (this.runtimeServiceReg != null)
+        {
+            this.runtimeServiceProps.put(HttpServiceRuntimeConstants.HTTP_SERVICE_ID_ATTRIBUTE,
+                    this.httpServiceFactory.getHttpServiceServiceId());
+            this.runtimeServiceReg.setProperties(this.runtimeServiceProps);
+        }
+    }
+
     /**
      * Register a servlet.
      * @param contextInfo The servlet context helper info
@@ -247,4 +302,80 @@ public final class WhiteboardHttpService
             }
         }
     }
+
+    public ServiceReference<HttpServiceRuntime> getServiceReference()
+    {
+        return this.runtimeServiceReg.getReference();
+    }
+
+    @Override
+    public RuntimeDTO getRuntimeDTO()
+    {
+        // create new DTO on every call
+        final RuntimeDTO runtime = new RuntimeDTO();
+
+        // attributes
+        runtime.attributes = new HashMap<String, String>();
+        for(final Map.Entry<String, Object> entry : this.runtimeServiceProps.entrySet())
+        {
+            runtime.attributes.put(entry.getKey(), entry.getValue().toString());
+        }
+
+        // servlet context DTOs
+        final List<ServletContextDTO> contextDTOs = new ArrayList<ServletContextDTO>();
+        for(final ContextHandler handler : this.contextManager.getContextHandlers())
+        {
+            final ServletContextDTO dto = new ServletContextDTO();
+
+            final ServletContext ctx = handler.getServletContext(this.bundleContext.getBundle());
+            try
+            {
+                dto.name = handler.getContextInfo().getName();
+                dto.contextPath = handler.getContextInfo().getPath();
+                dto.initParams = new HashMap<String, String>(handler.getContextInfo().getInitParameters());
+                dto.serviceId = handler.getContextInfo().getServiceId();
+
+                dto.contextName = ctx.getServletContextName();
+                dto.attributes = new HashMap<String, Object>();
+                final Enumeration<String> e = ctx.getAttributeNames();
+                while ( e.hasMoreElements() )
+                {
+                    final String name = e.nextElement();
+                    final Object value = ctx.getAttribute(name);
+                    if ( value != null )
+                    {
+                        // TODO - check for appropriate value types
+                    }
+                }
+
+                dto.errorPageDTOs = new ErrorPageDTO[0]; // TODO
+                dto.filterDTOs = new FilterDTO[0]; // TODO
+                dto.listenerDTOs = new ListenerDTO[0]; // TODO
+                dto.resourceDTOs = new ResourceDTO[0]; // TODO
+                dto.servletDTOs = new ServletDTO[0]; // TODO
+            }
+            finally
+            {
+                handler.ungetServletContext(this.bundleContext.getBundle());
+            }
+            contextDTOs.add(dto);
+        }
+        runtime.servletContextDTOs = contextDTOs.toArray(new ServletContextDTO[contextDTOs.size()]);
+
+        runtime.failedErrorPageDTOs = null; // TODO
+        runtime.failedFilterDTOs = null; // TODO
+        runtime.failedListenerDTOs = null; // TODO
+        runtime.failedResourceDTOs = null; // TODO
+        runtime.failedServletContextDTOs = null; // TODO
+        runtime.failedServletDTOs = null; // TODO
+
+        return runtime;
+    }
+
+    @Override
+    public RequestInfoDTO calculateRequestInfoDTO(final String path) {
+        // TODO
+        return null;
+    }
+
 }