package org.ow2.chameleon.fuchsia.core.component;

import org.ow2.chameleon.fuchsia.core.component.manager.DeclarationBindManager;
import org.ow2.chameleon.fuchsia.core.declaration.ImportDeclaration;
import org.ow2.chameleon.fuchsia.core.exceptions.BinderException;

import java.util.Set;

/**
 * Abstract implementation of an proxy-creator which provides an {@link ImporterService}.
 * Start must be call before registering the service !
 * Stop must be called while the service is no more available !
 *
 * @author Morgan Martinet
 */
public abstract class AbstractImporterComponent implements ImporterService, ImporterIntrospection {
    private final DeclarationBindManager<ImportDeclaration> declarationBindManager;

    public AbstractImporterComponent() {
        declarationBindManager = new DeclarationBindManager<ImportDeclaration>(this);
    }

    /**
     * Abstract method, called when a ImportDeclaration can be used by the implementation class.
     */
    protected abstract void useImportDeclaration(final ImportDeclaration importDeclaration) throws BinderException;

    /**
     * Abstract method, is called when the implementation class must stop to use an ImportDeclaration.
     */
    protected abstract void denyImportDeclaration(final ImportDeclaration importDeclaration) throws BinderException;

    /**
     * Stop the Importer component, iPOJO Invalidate instance callback.
     * Must be override !
     */
    protected void stop() {
        declarationBindManager.unbindAll();
    }

    /**
     * Start the endpoint-creator component, iPOJO Validate instance callback.
     * Must be override !
     */
    protected void start() {
        //
    }

    /**
     * @param importDeclaration The {@link ImportDeclaration} of the service to be imported.
     * @throws org.ow2.chameleon.fuchsia.core.exceptions.BinderException
     */
    public void addDeclaration(final ImportDeclaration importDeclaration) throws BinderException {
        declarationBindManager.addDeclaration(importDeclaration);
    }

    public void useDeclaration(ImportDeclaration declaration) throws BinderException {
        useImportDeclaration(declaration);
    }

    /**
     * @param importDeclaration The {@link ImportDeclaration} of the service to stop to be imported.
     * @throws org.ow2.chameleon.fuchsia.core.exceptions.BinderException
     */
    public void removeDeclaration(final ImportDeclaration importDeclaration) throws BinderException {
        declarationBindManager.removeDeclaration(importDeclaration);
    }

    public void denyDeclaration(ImportDeclaration declaration) throws BinderException {
        denyImportDeclaration(declaration);
    }

    public Set<ImportDeclaration> getImportDeclarations() {
        return declarationBindManager.getDeclarations();
    }

    @Override
    public String toString() {
        return getName();
    }
}

@@ -44,16 +44,60 @@
 onent;%0A%0A
+import org.osgi.framework.ServiceReference;%0A
 import o
@@ -168,16 +168,16 @@
 anager;%0A
-
 import o
@@ -304,16 +304,42 @@
 ption;%0A%0A
+import java.util.HashSet;%0A
 import j
@@ -783,16 +783,156 @@
 Manager;
+%0A    private final Set%3CImportDeclaration%3E waitingImportDeclarationsToHandle;%0A    private ServiceReference%3CImporterService%3E serviceReference;
 %0A%0A    pu
@@ -1052,16 +1052,94 @@
 (this);%0A
+        waitingImportDeclarationsToHandle = new HashSet%3CImportDeclaration%3E();%0A
     %7D%0A%0A 
@@ -3083,24 +3083,1319 @@
 s();%0A    %7D%0A%0A
+%0A    public ServiceReference%3CImporterService%3E getServiceReference() %7B%0A        return serviceReference;%0A    %7D%0A%0A    public void setServiceReference(ServiceReference%3CImporterService%3E serviceReference) %7B%0A        synchronized (waitingImportDeclarationsToHandle) %7B%0A            this.serviceReference = serviceReference;%0A            for (ImportDeclaration importDeclaration : waitingImportDeclarationsToHandle) %7B%0A                importDeclaration.handle(serviceReference);%0A            %7D%0A            waitingImportDeclarationsToHandle.clear();%0A        %7D%0A    %7D%0A%0A    public void handleImportDeclaration(ImportDeclaration importDeclaration) %7B%0A        synchronized (waitingImportDeclarationsToHandle) %7B%0A            if (this.serviceReference == null) %7B%0A                waitingImportDeclarationsToHandle.add(importDeclaration);%0A                return;%0A            %7D%0A        %7D%0A        importDeclaration.handle(serviceReference);%0A    %7D%0A%0A    public void unhandleImportDeclaration(ImportDeclaration importDeclaration) %7B%0A        synchronized (waitingImportDeclarationsToHandle) %7B%0A            if (this.serviceReference == null) %7B%0A                waitingImportDeclarationsToHandle.remove(importDeclaration);%0A                return;%0A            %7D%0A        %7D%0A        importDeclaration.unhandle(serviceReference);%0A%0A    %7D%0A%0A
     @Overrid
@@ -4459,10 +4459,11 @@
 ;%0A    %7D%0A
-
 %7D%0A
+%0A

Add utilities functions in AbstractImporterComponent to fix issue in importers