/**
 * Copyright (C) FuseSource, Inc.
 * http://fusesource.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 org.fusesource.fabric.agent.resolver;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.osgi.framework.Constants;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.resource.Wiring;
import org.osgi.service.repository.Repository;
import org.osgi.service.resolver.HostedCapability;
import org.osgi.service.resolver.ResolveContext;

/**
*/
public class ResolveContextImpl extends ResolveContext {

    private final Set<Resource> mandatory;
    private final Set<Resource> optional;
    private final Repository repository;
    private final Map<Resource, Wiring> wirings;
    private final boolean resolveOptional;

    public ResolveContextImpl(Set<Resource> mandatory,
                              Set<Resource> optional,
                              Repository repository,
                              boolean resolveOptional) {
        this.mandatory = mandatory;
        this.optional = optional;
        this.repository = repository;
        this.wirings = new HashMap<Resource, Wiring>();
        this.resolveOptional = resolveOptional;
    }

    @Override
    public Collection<Resource> getMandatoryResources() {
        return mandatory;
    }

    @Override
    public Collection<Resource> getOptionalResources() {
        return optional;
    }

    @Override
    public List<Capability> findProviders(Requirement requirement) {
        List<Capability> caps = new ArrayList<Capability>();
        Map<Requirement, Collection<Capability>> resMap =
                repository.findProviders(Collections.singleton(requirement));
        Collection<Capability> res = resMap != null ? resMap.get(requirement) : null;
        if (res != null) {
            caps.addAll(res);
        }
        Collections.sort(caps, new CandidateComparator());
        return caps;
    }
    @Override
    public int insertHostedCapability(List capabilities, HostedCapability hostedCapability) {
        // TODO: implement ?
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean isEffective(Requirement requirement) {
        return resolveOptional ||
                !Constants.RESOLUTION_OPTIONAL.equals(requirement.getDirectives().get(Constants.RESOLUTION_DIRECTIVE));
    }
    @Override
    public Map<Resource, Wiring> getWirings() {
        return wirings;
    }
}

@@ -1457,16 +1457,104 @@
 ional;%0A%0A
+    private final CandidateComparator candidateComparator = new CandidateComparator();%0A%0A
     publ
@@ -2655,21 +2655,17 @@
 t(caps, 
-new C
+c
 andidate
@@ -2674,18 +2674,16 @@
 mparator
-()
 );%0A     
@@ -2824,78 +2824,379 @@
     
-// TODO: implement ?%0A        throw new UnsupportedOperationException()
+for (int i=0; i %3C capabilities.size(); i++) %7B%0A            Capability cap = (Capability) capabilities.get(i);%0A            if (candidateComparator.compare(hostedCapability, cap) %3C= 0) %7B%0A                capabilities.add(i, hostedCapability);%0A                return i;%0A            %7D%0A        %7D%0A        capabilities.add(hostedCapability);%0A        return capabilities.size() - 1
 ;%0A  

Fix fragment support in fabric-agent.