Author: cziegeler
Date: Fri Oct 19 07:02:59 2018
New Revision: 1844307

URL: http://svn.apache.org/viewvc?rev=1844307&view=rev
Log:
FELIX-5966 : NPE at RuntimeDTOBuilder.java:74

Modified:
    
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
    
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceRuntimeImpl.java
    
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/whiteboard/WhiteboardManager.java
    felix/trunk/http/itest/pom.xml

Modified: 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java?rev=1844307&r1=1844306&r2=1844307&view=diff
==============================================================================
--- 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
 (original)
+++ 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/RuntimeDTOBuilder.java
 Fri Oct 19 07:02:59 2018
@@ -42,18 +42,18 @@ public final class RuntimeDTOBuilder
 {
 
     private final RegistryRuntime registry;
-    private final ServiceReference<HttpServiceRuntime> serviceReference;
+    private final ServiceReferenceDTO serviceRefDTO;
 
-    public RuntimeDTOBuilder(final RegistryRuntime registry, final 
ServiceReference<HttpServiceRuntime> ref)
+    public RuntimeDTOBuilder(final RegistryRuntime registry, final 
ServiceReferenceDTO srDTO)
     {
         this.registry = registry;
-        this.serviceReference = ref;
+        this.serviceRefDTO = srDTO;
     }
 
     public RuntimeDTO build()
     {
         final RuntimeDTO runtimeDTO = new RuntimeDTO();
-        runtimeDTO.serviceDTO = createServiceDTO();
+        runtimeDTO.serviceDTO = this.serviceRefDTO;
         runtimeDTO.servletContextDTOs = createContextDTOs();
         runtimeDTO.preprocessorDTOs = createPreprocessorDTOs();
 
@@ -68,35 +68,6 @@ public final class RuntimeDTOBuilder
         return runtimeDTO;
     }
 
-    private ServiceReferenceDTO createServiceDTO()
-    {
-        final ServiceReferenceDTO dto = new ServiceReferenceDTO();
-        dto.bundle = this.serviceReference.getBundle().getBundleId();
-        dto.id = (Long) 
this.serviceReference.getProperty(Constants.SERVICE_ID);
-        final Map<String, Object> props = new HashMap<String, Object>();
-        for (String key : this.serviceReference.getPropertyKeys())
-        {
-            props.put(key, this.serviceReference.getProperty(key));
-        }
-        dto.properties = props;
-
-        final Bundle[] ubs = this.serviceReference.getUsingBundles();
-        if (ubs == null)
-        {
-            dto.usingBundles = new long[0];
-        }
-        else
-        {
-            dto.usingBundles = new long[ubs.length];
-            for (int j=0; j < ubs.length; j++)
-            {
-                dto.usingBundles[j] = ubs[j].getBundleId();
-            }
-        }
-
-        return dto;
-    }
-
     private ServletContextDTO[] createContextDTOs()
     {
         final Collection<ServletContextDTO> contexts = 
registry.getServletContextDTOs();

Modified: 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceRuntimeImpl.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceRuntimeImpl.java?rev=1844307&r1=1844306&r2=1844307&view=diff
==============================================================================
--- 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceRuntimeImpl.java
 (original)
+++ 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/service/HttpServiceRuntimeImpl.java
 Fri Oct 19 07:02:59 2018
@@ -28,8 +28,10 @@ import org.apache.felix.http.base.intern
 import org.apache.felix.http.base.internal.runtime.dto.RuntimeDTOBuilder;
 import org.apache.felix.http.base.internal.whiteboard.WhiteboardManager;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
+import org.osgi.framework.dto.ServiceReferenceDTO;
 import org.osgi.service.http.runtime.HttpServiceRuntime;
 import org.osgi.service.http.runtime.dto.RequestInfoDTO;
 import org.osgi.service.http.runtime.dto.RuntimeDTO;
@@ -52,8 +54,10 @@ public final class HttpServiceRuntimeImp
 
     private volatile long changeCount;
 
-    private volatile ServiceReference<HttpServiceRuntime> serviceReference;
+    private volatile ServiceRegistration<HttpServiceRuntime> serviceReg;
 
+    private volatile ServiceReferenceDTO serviceRefDTO;
+    
     private volatile Timer timer;
 
     private final long updateChangeCountDelay;
@@ -87,9 +91,34 @@ public final class HttpServiceRuntimeImp
     @Override
     public RuntimeDTO getRuntimeDTO()
     {
-        final RuntimeDTOBuilder runtimeDTOBuilder = new 
RuntimeDTOBuilder(contextManager.getRuntimeInfo(),
-                this.serviceReference);
-        return runtimeDTOBuilder.build();
+       if ( this.serviceRefDTO == null )
+       {
+               // it might happen that this code is executed in several threads
+               // but that's very unlikely and even if, fetching the service 
+               // reference several times is not a big deal
+               final ServiceRegistration<HttpServiceRuntime> reg = 
this.serviceReg;
+               if ( reg != null )
+               {
+                final long id = (long) 
reg.getReference().getProperty(Constants.SERVICE_ID);
+                final ServiceReferenceDTO[] dtos = 
reg.getReference().getBundle().adapt(ServiceReferenceDTO[].class);
+                for(final ServiceReferenceDTO dto : dtos) 
+                {
+                       if ( dto.id == id) 
+                       {
+                               this.serviceRefDTO = dto;
+                               break;
+                       }
+                }
+                       
+               }
+       }
+        if ( this.serviceRefDTO != null )
+        {
+            final RuntimeDTOBuilder runtimeDTOBuilder = new 
RuntimeDTOBuilder(contextManager.getRuntimeInfo(),
+                    this.serviceRefDTO);
+            return runtimeDTOBuilder.build();
+        }
+        throw new IllegalStateException("Service is already unregistered");
     }
 
     @Override
@@ -116,19 +145,43 @@ public final class HttpServiceRuntimeImp
         attributes = replacement;
     }
 
-    public Dictionary<String, Object> getAttributes()
+    public void register(final BundleContext bundleContext)
     {
-        return attributes;
+        this.serviceReg = 
bundleContext.registerService(HttpServiceRuntime.class,
+                this,
+                attributes);
     }
-
-    public void setServiceReference(
-            final ServiceReference<HttpServiceRuntime> reference)
+    
+    public void unregister()
     {
-        this.serviceReference = reference;
+       if ( this.serviceReg != null ) 
+       {
+               try
+               {
+                   this.serviceReg.unregister();
+               }
+               catch ( final IllegalStateException ise)
+               {
+                       // we just ignore it
+               }
+               this.serviceReg = null;                 
+       }
+       this.serviceRefDTO = null;
+    }
+    
+    public ServiceReference<HttpServiceRuntime> getServiceReference()
+    {
+       final ServiceRegistration<HttpServiceRuntime> reg = this.serviceReg;
+       if ( reg != null ) 
+       {
+               return reg.getReference();
+       }
+       return null;
     }
-
-    public void updateChangeCount(final 
ServiceRegistration<HttpServiceRuntime> reg)
+    
+    public void updateChangeCount()
     {
+       final ServiceRegistration<HttpServiceRuntime> reg = this.serviceReg;
         if ( reg != null )
         {
             boolean setPropsDirectly = false;
@@ -159,7 +212,7 @@ public final class HttpServiceRuntimeImp
                                 {
                                     try
                                     {
-                                        reg.setProperties(getAttributes());
+                                        reg.setProperties(attributes);
                                     }
                                     catch ( final IllegalStateException ise)
                                     {
@@ -177,7 +230,7 @@ public final class HttpServiceRuntimeImp
             {
                 try
                 {
-                    reg.setProperties(getAttributes());
+                    reg.setProperties(attributes);
                 }
                 catch ( final IllegalStateException ise)
                 {

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=1844307&r1=1844306&r2=1844307&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
 Fri Oct 19 07:02:59 2018
@@ -90,7 +90,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.runtime.HttpServiceRuntimeConstants;
 import org.osgi.service.http.runtime.dto.DTOConstants;
 import org.osgi.service.http.runtime.dto.PreprocessorDTO;
@@ -129,8 +128,6 @@ public final class WhiteboardManager
 
     private volatile ServiceRegistration<ServletContextHelper> 
defaultContextRegistration;
 
-    private volatile ServiceRegistration<HttpServiceRuntime> runtimeServiceReg;
-
     /**
      * Create a new whiteboard http manager
      *
@@ -160,10 +157,7 @@ public final class WhiteboardManager
 
         
this.serviceRuntime.setAttribute(HttpServiceRuntimeConstants.HTTP_SERVICE_ID,
                 
Collections.singletonList(this.httpServiceFactory.getHttpServiceServiceId()));
-        this.runtimeServiceReg = 
this.httpBundleContext.registerService(HttpServiceRuntime.class,
-                serviceRuntime,
-                this.serviceRuntime.getAttributes());
-        
this.serviceRuntime.setServiceReference(this.runtimeServiceReg.getReference());
+        this.serviceRuntime.register(this.httpBundleContext);
 
         this.webContext = containerContext;
 
@@ -239,7 +233,7 @@ public final class WhiteboardManager
         }
         this.trackers.clear();
 
-        this.serviceRuntime.setServiceReference(null);
+        this.serviceRuntime.unregister();
 
         this.preprocessorHandlers = Collections.emptyList();
         this.contextMap.clear();
@@ -252,12 +246,6 @@ public final class WhiteboardManager
             this.defaultContextRegistration.unregister();
             this.defaultContextRegistration = null;
         }
-
-        if ( this.runtimeServiceReg != null )
-        {
-            this.runtimeServiceReg.unregister();
-            this.runtimeServiceReg = null;
-        }
         this.webContext = null;
     }
 
@@ -910,7 +898,7 @@ public final class WhiteboardManager
             try
             {
                 final Filter f = this.httpBundleContext.createFilter(target);
-                return f.match(this.runtimeServiceReg.getReference());
+                return f.match(this.serviceRuntime.getServiceReference());
             }
             catch ( final InvalidSyntaxException ise)
             {
@@ -1027,6 +1015,6 @@ public final class WhiteboardManager
 
     private void updateRuntimeChangeCount()
     {
-        this.serviceRuntime.updateChangeCount(this.runtimeServiceReg);
+        this.serviceRuntime.updateChangeCount();
     }
 }

Modified: felix/trunk/http/itest/pom.xml
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/itest/pom.xml?rev=1844307&r1=1844306&r2=1844307&view=diff
==============================================================================
--- felix/trunk/http/itest/pom.xml (original)
+++ felix/trunk/http/itest/pom.xml Fri Oct 19 07:02:59 2018
@@ -92,6 +92,18 @@
                        
<artifactId>org.apache.felix.http.servlet-api</artifactId>
                        <version>${http.servlet.api.version}</version>
                </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.http</artifactId>
+            <version>1.2.1</version>
+           <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.service.http.whiteboard</artifactId>
+            <version>1.1.0</version>
+            <scope>provided</scope>
+        </dependency>
                <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>org.apache.felix.http.jetty</artifactId>


Reply via email to