/**
 *  Copyright 2005-2014 Red Hat, Inc.
 *
 *  Red Hat licenses this file to you 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 io.fabric8.commands;

import io.fabric8.api.FabricService;
import io.fabric8.api.scr.ValidatingReference;
import io.fabric8.boot.commands.support.AbstractCommandComponent;
import io.fabric8.boot.commands.support.ContainerCompleter;
import io.fabric8.boot.commands.support.VersionCompleter;

import org.apache.felix.gogo.commands.Action;
import org.apache.felix.gogo.commands.basic.AbstractCommand;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.service.command.Function;

@Component(immediate = true)
@Service({ Function.class, AbstractCommand.class })
@org.apache.felix.scr.annotations.Properties({
        @Property(name = "osgi.command.scope", value = ContainerUpgrade.SCOPE_VALUE),
        @Property(name = "osgi.command.function", value = ContainerUpgrade.FUNCTION_VALUE)
})
public class ContainerUpgrade extends AbstractCommandComponent {

    public static final String SCOPE_VALUE = "fabric";
    public static final String FUNCTION_VALUE =  "container-upgrade";
    public static final String DESCRIPTION = "Upgrade containers to a new version";

    @Reference(referenceInterface = FabricService.class)
    private final ValidatingReference<FabricService> fabricService = new ValidatingReference<FabricService>();

    // Completers
    @Reference(referenceInterface = ContainerCompleter.class, bind = "bindContainerCompleter", unbind = "unbindContainerCompleter")
    private ContainerCompleter containerCompleter; // dummy field
    @Reference(referenceInterface = VersionCompleter.class, bind = "bindVersionCompleter", unbind = "unbindVersionCompleter", cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
    private VersionCompleter versionCompleter; // dummy field

    @Activate
    void activate() {
        activateComponent();
    }

    @Deactivate
    void deactivate() {
        deactivateComponent();
    }

    @Override
    public Action createNewAction() {
        assertValid();
        return new ContainerUpgradeAction(fabricService.get());
    }

    void bindFabricService(FabricService fabricService) {
        this.fabricService.bind(fabricService);
    }

    void unbindFabricService(FabricService fabricService) {
        this.fabricService.unbind(fabricService);
    }

    void bindContainerCompleter(ContainerCompleter completer) {
        bindCompleter(completer);
    }

    void unbindContainerCompleter(ContainerCompleter completer) {
        unbindCompleter(completer);
    }

    void bindVersionCompleter(VersionCompleter completer) {
        bindCompleter(completer);
    }

    void unbindVersionCompleter(VersionCompleter completer) {
        unbindCompleter(completer);
    }
}

@@ -662,16 +662,123 @@
 mands;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0A
 import i
@@ -1616,16 +1616,128 @@
 unction;
+%0Aimport org.apache.karaf.shell.console.Completer;%0Aimport org.apache.karaf.shell.console.completer.NullCompleter;
 %0A%0A@Compo
@@ -2683,31 +2683,16 @@
 mpleter;
- // dummy field
 %0A    @Re
@@ -2945,23 +2945,84 @@
 ter;
- // dummy field
+%0A%0A    private final List%3CCompleter%3E completers = new ArrayList%3CCompleter%3E();
 %0A%0A  
@@ -3613,37 +3613,109 @@
 ) %7B%0A        
-bindC
+synchronized (completers) %7B%0A            // container last (2nd)%0A            c
 ompleter
 (completer);
@@ -3694,32 +3694,37 @@
        completer
+s.add
 (completer);%0A   
@@ -3712,32 +3712,42 @@
 add(completer);%0A
+        %7D%0A
     %7D%0A%0A    void 
@@ -3807,39 +3807,33 @@
 ) %7B%0A        
-unbindC
+c
 ompleter
 (completer);
@@ -3812,32 +3812,40 @@
        completer
+s.remove
 (completer);%0A   
@@ -3916,30 +3916,103 @@
         
-bindCompleter(
+synchronized (completers) %7B%0A            // version first%0A            completers.add(0, 
 complete
@@ -4007,32 +4007,42 @@
 (0, completer);%0A
+        %7D%0A
     %7D%0A%0A    void 
@@ -4106,23 +4106,311 @@
     
-unbindCompleter
+completers.remove(completer);%0A    %7D%0A%0A    @Override%0A    public List%3CCompleter%3E getCompleters() %7B%0A        synchronized (completers) %7B%0A            if (completers.isEmpty()) %7B%0A                return Arrays.%3CCompleter%3EasList(new NullCompleter());%0A            %7D%0A            return Collections.unmodifiableList
 (com
@@ -4415,19 +4415,31 @@
 ompleter
+s
 );%0A    
-%7D
+    %7D%0A    %7D%0A
 %0A%7D%0A

Correct order of completers for fabric:container-upgrade