/*
###############################################################################
#                                                                             #
#    Copyright 2016, AdeptJ (http://www.adeptj.com)                           #
#                                                                             #
#    Licensed 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 com.adeptj.modules.jaxrs.resteasy;

import org.jboss.resteasy.spi.Registry;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ResourceTracker is an OSGi ServiceTracker which tracks the services annotated with @Path JAX-RS annotation.
 * 
 * @author Rakesh.Kumar, AdeptJ.
 */
public class ResourceTracker extends ServiceTracker<Object, Object> {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ResourceTracker.class);

	private static final String JAXRS_RESOURCE_SERVICE_FILTER = "(&(objectClass=*)(osgi.jaxrs.resource.base=*))";

	private Registry registry;
    
	public ResourceTracker(BundleContext context, Registry registry) {
		super(context, anyServiceFilter(context, JAXRS_RESOURCE_SERVICE_FILTER), null);
		this.registry = registry;
	}

	@Override
	public Object addingService(ServiceReference<Object> reference) {
		Object resource = super.addingService(reference);
		LOGGER.info("Adding JAX-RS Resource: [{}]", resource);
		this.registry.addSingletonResource(resource);
		return resource;
	}

	@Override
	public void removedService(ServiceReference<Object> reference, Object service) {
		super.removedService(reference, service);
		LOGGER.info("Removing JAX-RS Resource: [{}]", service);
		this.registry.removeRegistrations(service.getClass());
	}

    private static Filter anyServiceFilter(BundleContext context, String filterExpr) {
        try {
            return context.createFilter(new StringBuilder("(&(").append(Constants.OBJECTCLASS).append("=")
                    .append("*").append(")").append(filterExpr).append(")").toString());
        } catch (InvalidSyntaxException ex) {
            // Filter expression is malformed, not RFC-1960 based Filter.
            throw new IllegalArgumentException("InvalidSyntaxException!!", ex);
        }
    }

}

@@ -1752,16 +1752,44 @@
 ctory;%0A%0A
+import java.util.Optional;%0A%0A
 /**%0A * R
@@ -1895,16 +1895,90 @@
 tation.%0A
+ *%0A * Note: All the registered JAX-RS resources are Singleton by default.%0A
  * %0A * @
@@ -2679,26 +2679,84 @@
  resource);%0A
-%09%09
+        Optional.ofNullable(resource).ifPresent(consumer -%3E 
 this.registr
@@ -2787,16 +2787,17 @@
 esource)
+)
 ;%0A%09%09retu
@@ -2809,25 +2809,602 @@
 source;%0A%09%7D%0A%0A
-%09
+    /**%0A     * Removes the given Resource from RESTEasy Registry and registers again.%0A     */%0A    @Override%0A    public void modifiedService(ServiceReference%3CObject%3E reference, Object service) %7B%0A        LOGGER.info(%22Service is modified, removing JAX-RS Resource: %5B%7B%7D%5D%22, service);%0A        Optional.ofNullable(service).ifPresent(consumer -%3E this.registry.removeRegistrations(service.getClass()));%0A        LOGGER.info(%22Adding JAX-RS Resource again: %5B%7B%7D%5D%22, service);%0A        Optional.ofNullable(service).ifPresent(consumer -%3E this.registry.addSingletonResource(service));%0A    %7D%0A%0A    
 @Override%0A%09p

put a null check on resource reference, overridden the modifiedService.