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

import org.ow2.chameleon.fuchsia.core.declaration.ImportDeclaration;
import org.ow2.chameleon.fuchsia.core.util.BadImportRegistration;

import java.util.HashSet;
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 barjo
 * @author Morgan Martinet
 */
public abstract class AbstractImporterComponent implements ImporterService {
    private final Set<ImportDeclaration> importDeclarations;

    private volatile boolean isValid = false;

    public AbstractImporterComponent() {
        importDeclarations = new HashSet<ImportDeclaration>();
    }

    /**
     * Abstract method, is called when the sub class must create the proxy or do other stuff, like APAM things... dun know
     * <p/>
     * TODO : Does it should be able to throw an exception if there's problem ? which one ?
     */
    protected abstract void createProxy(final ImportDeclaration importDeclaration);

    /**
     * Abstract method, is called when the sub class must destroy the proxy.
     * Do not forget to unregister the proxy, call: <code>proxy.unregister()</code>
     */
    protected abstract void destroyProxy(final ImportDeclaration importDeclaration);

    /**
     * Stop the proxy-creator, iPOJO Invalidate instance callback.
     * Must be override !
     */
    protected void stop() {

        synchronized (importDeclarations) {
            // destroy all the proxies
            for (ImportDeclaration importDeclaration : importDeclarations) {
                destroyProxy(importDeclaration);
            }
            // Clear the map
            importDeclarations.clear();
            isValid = false;
        }
    }

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

    /**
     * @return <code>true</code> if the {@link ImporterService} is in a valid state, <code>false</code> otherwise.
     */
    protected final boolean isValid() {
        return isValid;
    }

	/*---------------------------------*
     *  ImportService implementation *
	 *---------------------------------*/

    /**
     *
     * @param importDeclaration The {@link ImportDeclaration} of the service to be imported.
     * @throws BadImportRegistration
     */
    public void addImportDeclaration(ImportDeclaration importDeclaration) throws BadImportRegistration {
        if (!isValid) {
            throw new BadImportRegistration("This ImporterService is no more available. !");
        }

        synchronized (importDeclarations) {
            if (importDeclarations.contains(importDeclaration)) {
                // Already register
                // FIXME :  Clone Registration ??
                throw new UnsupportedOperationException("Duplicate ImportDeclaration are for the moment " +
                        "not supported by the AbstractImporterComponent");
            } else {
                //First registration, create the proxy
                createProxy(importDeclaration);
                importDeclarations.add(importDeclaration);
            }
        }
    }


    /**
     *
     * @param importDeclaration The {@link ImportDeclaration} of the service to stop to be imported.
     * @throws BadImportRegistration
     */
    public void removeImportDeclaration(ImportDeclaration importDeclaration) throws BadImportRegistration {
        if (!isValid) {
            throw new BadImportRegistration("This ImporterService is no more available. !");
        }
        synchronized (importDeclarations) {
            destroyProxy(importDeclaration);
            importDeclarations.remove(importDeclaration);
        }
    }
}

@@ -636,55 +636,8 @@
 s;%0A%0A
-    private volatile boolean isValid = false;%0A%0A
     
@@ -1768,37 +1768,8 @@
 ();%0A
-            isValid = false;%0A
     
@@ -1943,289 +1943,17 @@
     
-synchronized (importDeclarations) %7B%0A            isValid = true;%0A        %7D%0A    %7D%0A%0A    /**%0A     * @return %3Ccode%3Etrue%3C/code%3E if the %7B@link ImporterService%7D is in a valid state, %3Ccode%3Efalse%3C/code%3E otherwise.%0A     */%0A    protected final boolean isValid() %7B%0A        return isValid;%0A    %7D
+//%0A    %7D%0A
 %0A%0A%09/
@@ -2329,136 +2329,8 @@
 n %7B%0A
-        if (!isValid) %7B%0A            throw new BadImportRegistration(%22This ImporterService is no more available. !%22);%0A        %7D%0A%0A
     
@@ -3192,135 +3192,8 @@
 n %7B%0A
-        if (!isValid) %7B%0A            throw new BadImportRegistration(%22This ImporterService is no more available. !%22);%0A        %7D%0A
     

Remove isValid from AbstractImporterComponent