package com.aiwsolutions.springaware.dm;

import com.aiwsolutions.springaware.bundle.BundleService;
import com.aiwsolutions.springaware.bundle.ExportService;
import com.aiwsolutions.springaware.exception.InvalidBundleServiceException;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import java.util.Dictionary;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by camhoang on 9/22/14.
 */
public class ServiceExposer {

    private final BundleContext bundleContext;
    private volatile ConcurrentHashMap<String, ServiceRegistration> serviceMap;

    public ServiceExposer(BundleContext bundleContext) {
        this.bundleContext = bundleContext;
        this.serviceMap = new ConcurrentHashMap<>();
    }

    public void expose(BundleService bean) {
        Class[] implementedInterfaces = bean.getClass().getInterfaces();
        String serviceInterface = null;

        for (Class inf : implementedInterfaces) {
            if (inf.getName().equals(bean.getServiceInterface().getName()))
                serviceInterface = inf.getName();
        }
        if (serviceInterface == null)
            throw new InvalidBundleServiceException("Bean does not implement a service interface");

        registerService(serviceInterface, bean, null);
    }

    public void unregisterServices() {
        for (Map.Entry<String, ServiceRegistration> entry : serviceMap.entrySet()) {
            entry.getValue().unregister();
        }
    }

    public void exposeAnnotationService(Object value) {
        ExportService serviceDescription = value.getClass().getAnnotation(ExportService.class);
        if (serviceDescription != null) {
            registerService(serviceDescription.value().getName(), value, null);
        }
    }

    private void registerService(String interfaceName, Object bean, Dictionary properties) {
        ServiceRegistration reg = bundleContext.registerService(interfaceName,
                bean,
                properties);
        serviceMap.put(bean.getClass().getName(), reg);
    }
}

@@ -836,479 +836,77 @@
     
-Class%5B%5D implementedInterfaces = bean.getClass().getInterfaces();%0A        String serviceInterface = null;%0A%0A        for (Class inf : implementedInterfaces) %7B%0A            if (inf.getName().equals(bean.getServiceInterface().getName()))%0A                serviceInterface = inf.getName();%0A        %7D%0A        if (serviceInterface == null)%0A            throw new InvalidBundleServiceException(%22Bean does not implement a service interface%22);%0A%0A        registerService(serviceInterface
+registerService(getServiceInterface(bean, bean.getServiceInterface())
 , be
@@ -1154,21 +1154,20 @@
 (Object 
-value
+bean
 ) %7B%0A    
@@ -1205,21 +1205,20 @@
 ption = 
-value
+bean
 .getClas
@@ -1269,131 +1269,678 @@
     
-if (serviceDescription != null) %7B%0A            registerService(serviceDescription.value().getName(), value, null);%0A        %7D
+registerService(getServiceInterface(bean, serviceDescription.value()), bean, null);%0A    %7D%0A%0A    private String getServiceInterface(Object bean, Class exportedInterface) %7B%0A        Class%5B%5D implementedInterfaces = bean.getClass().getInterfaces();%0A        String serviceInterface = null;%0A%0A        for (Class inf : implementedInterfaces) %7B%0A            if (inf.equals(exportedInterface))%0A                serviceInterface = inf.getName();%0A        %7D%0A        if (serviceInterface == null)%0A            throw new InvalidBundleServiceException(%22Unable to export%22 + bean.getClass().getName() + %22: Bean does not implement exported service interface.%22);%0A        return serviceInterface;
 %0A   

Check for implementing exported interface