public static ResolverHookProcessor getCurrentProcessor() {
    return processorAssociation.get();
} public interface XBundleRevision extends XResource, BundleRevision {

    /**
     * Get the associated {@link XBundle}
     */
    XBundle getBundle();

    /**
     * Get the {@link XBundleWiringSupport} associated with this resource
     */
    XBundleWiringSupport getWiringSupport();

    /**
     * Get the bundle canonical name
     *
     * [symbolicName:version]
     */
    String getCanonicalName();

    /**
     * Get the associated {@link ModuleIdentifier}
     * or null if the revision is not resolved
     */
    ModuleIdentifier getModuleIdentifier();

    /**
     * Get the associated {@link ModuleIdentifier}
     * or null if the revision is not resolved
     */
    ModuleClassLoader getModuleClassLoader();

    /**
     * @see {@link Bundle#getResource(String)}
     */
    URL getResource(String name);

    /**
     * @see {@link Bundle#getResources(String)}
     */
    Enumeration<URL> getResources(String name) throws IOException;

    /**
     * @see {@link Bundle#findEntries(String, String, boolean)}
     */
    Enumeration<URL> findEntries(String path, String filePattern, boolean recursive);

    /**
     * @see {@link Bundle#getEntry(String)}
     */
    URL getEntry(String path);

    /**
     * @see {@link Bundle#getEntryPaths(String)}
     */
    Enumeration<String> getEntryPaths(String path);

    /**
     * True if this revision is a fragment
     */
    boolean isFragment();
} public interface XCapability extends XElement, XAttributeSupport, XDirectiveSupport, Capability {

    /**
     * Get the resource declaring this capability.
     */
    XResource getResource();
    
    /**
     * Validate the capability
     */
    void validate();

    /**
     * Adapt this capability to another type
     */
    <T extends XCapability> T adapt(Class<T> clazz);
} public interface XEnvironment {

    void installResources(XResource... resources);

    void uninstallResources(XResource... resources);

    XResource getResourceById(long identifier);

    Iterator<XResource> getResources(Collection<String> types);

    Long nextResourceIdentifier(Long value, String symbolicName);

    List<Capability> findProviders(Requirement req);

    Map<Resource, Wiring> updateWiring(Map<Resource, List<Wire>> delta);

    Map<Resource, Wiring> getWirings();
} public interface XIdentityCapability extends XCapability {

    /**
     * The identity name of this resource.
     */
    String getName();

    /**
     * The version of this resource.
     */
    Version getVersion();

    /**
     * The resource type
     * @see IdentityNamespace#CAPABILITY_TYPE_ATTRIBUTE
     */
    String getType();

    /**
     * A flag to indicate that this resource is a singleton
     */
    boolean isSingleton();
} public interface XPackageCapability extends XCapability {

    /**
     * The package name
     */
    String getPackageName();

    /**
     * The package version.
     */
    Version getVersion();
} public interface XPackageRequirement extends XRequirement {

    /**
     * The package name
     */
    String getPackageName();

    /**
     * A flag indicating that this is a dynamic package requirement
     */
    boolean isDynamic();
} public interface XRequirement extends XElement, XAttributeSupport, XDirectiveSupport, Requirement {

    /**
     * Get the resource declaring this requirement.
     */
    XResource getResource();
    
    /**
     * Get the filter associated with this requirement.
     */
    Filter getFilter();

    /**
     * A flag indicating that this is an optional requirement.
     */
    boolean isOptional();

    /**
     * Validate the requirement
     */
    void validate();

    /**
     * Adapt this requirement to another type
     */
    <T extends XRequirement> T adapt(Class<T> clazz);

    /**
     * Matches this requirement against the given capability.
     */
    boolean matches(Capability cap);
} public interface XResource extends XElement, XAttributeSupport, Resource {

    /** The id attachment key */
    AttachmentKey<Long> RESOURCE_IDENTIFIER_KEY = AttachmentKey.create(Long.class);

    /**
     * Artifact coordinates may be defined by the simple groupId:artifactId:version form,
     * or the fully qualified form groupId:artifactId:type:version[:classifier]
     */
    String MAVEN_IDENTITY_NAMESPACE = "maven.identity";

    /**
     * Artifact coordinates are defined by {@link org.jboss.modules.ModuleIdentifier}
     */
    String MODULE_IDENTITY_NAMESPACE = "module.identity";

    /**
     * The attribute on the {@link XIdentityCapability} for the below types
     */
    String CAPABILITY_TYPE_ATTRIBUTE = IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE;

    /**
     * Some predefined resource types
     */
    String TYPE_BUNDLE = IdentityNamespace.TYPE_BUNDLE;
    String TYPE_FRAGMENT = IdentityNamespace.TYPE_FRAGMENT;
    String TYPE_UNKNOWN = IdentityNamespace.TYPE_UNKNOWN;
    String TYPE_ABSTRACT = "abstract";
    String TYPE_MODULE = "module";
    String TYPE_MAVEN = "maven";

    /**
     * Get the identity capability for this resource
     */
    XIdentityCapability getIdentityCapability();

    /**
     * Validate the resource
     */
    void validate();

    /**
     * Get the current resource state
     */
    State getState();

    /**
     * True if the resource is mutable
     */
    boolean isMutable();

    /**
     * True if the resource is abstract
     */
    boolean isAbstract();

    /**
     * Make the resource immutable
     */
    void setMutable(boolean mutable);

    /**
     * Get the {@link XWiringSupport} associated with this resource
     */
    XWiringSupport getWiringSupport();

    /**
     * Get the {@link XWiring} associated with this resource
     */
    XWiring getWiring(boolean checkEffective);

    /**
     * The resource state in the {@link XEnvironment}
     */
    enum State {
        INSTALLED, UNINSTALLED
    }
} enum State {
    INSTALLED, UNINSTALLED
} public interface XWiring extends Wiring {

    boolean isEffective();
    
    void addRequiredWire(Wire wire);

    void addProvidedWire(Wire wire);
} public interface XWiringSupport {

    XWiring getWiring(boolean checkEffective);

    void setWiring(XWiring wiring);

    boolean isEffective();

    void makeUneffective();

    void refresh();
}
import static org.jboss.osgi.resolver.ResolverLogger.LOGGER;
import static org.jboss.osgi.resolver.ResolverMessages.MESSAGES;
import static org.jboss.osgi.resolver.spi.ResolverHookProcessor.getCurrentProcessor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.jboss.osgi.resolver.XBundleRevision;
import org.jboss.osgi.resolver.XCapability;
import org.jboss.osgi.resolver.XEnvironment;
import org.jboss.osgi.resolver.XIdentityCapability;
import org.jboss.osgi.resolver.XPackageCapability;
import org.jboss.osgi.resolver.XPackageRequirement;
import org.jboss.osgi.resolver.XRequirement;
import org.jboss.osgi.resolver.XResource;
import org.jboss.osgi.resolver.XResource.State;
import org.jboss.osgi.resolver.XWiring;
import org.jboss.osgi.resolver.XWiringSupport;
import org.omg.CORBA.Environment;
import org.osgi.framework.namespace.HostNamespace;
import org.osgi.framework.wiring.BundleCapability;
import org.osgi.framework.wiring.BundleRequirement;
import org.osgi.resource.Capability;
import org.osgi.resource.Requirement;
import org.osgi.resource.Resource;
import org.osgi.resource.Wire;
import org.osgi.resource.Wiring;
                if (wiring != null && xcap.adapt(XPackageCapability.class) != null) {
                    String pkgname = xcap.adapt(XPackageCapability.class).getPackageName();
                    for (Wire wire : wiring.getRequiredResourceWires(cap.getNamespace())) {
                        XRequirement wirereq = (XRequirement) wire.getRequirement();
                        XPackageRequirement preq = wirereq.adapt(XPackageRequirement.class);
                        if (pkgname.equals(preq.getPackageName())) {
                            ignoreCapability = true;
                            break;
                        }
                    }
                }
                if (ignoreCapability) {
                    continue;
                }

                // A fragment can only provide a capability if it is either already attached
                // or if there is one possible hosts that it can attach to
                // i.e. one of the hosts in the range is not resolved already
                List<Requirement> hostreqs = capres.getRequirements(HostNamespace.HOST_NAMESPACE);
                if (wiring == null && !hostreqs.isEmpty()) {
                    boolean unresolvedHost = false;
                    XRequirement hostreq = (XRequirement) hostreqs.get(0);
                    Set<Capability> hostcaps = capabilityCache.get(CacheKey.create(hostreq));
                    if (hostcaps != null) {
                        for (Capability hostcap : hostcaps) {
                            if (hostreq.matches(hostcap)) {
                                XResource host = (XResource) hostcap.getResource();
                                if (host.getWiringSupport().getWiring(true) == null) {
                                    unresolvedHost = true;
                                    break;
                                }
                            }
                        }
                    }
                    ignoreCapability = !unresolvedHost;
                }

                if (!ignoreCapability) {
                    result.add(cap);
                }
            }
        }

        // Filter the matches by calling the registered {@link ResolverHook}s
        ResolverHookProcessor hookregs = getCurrentProcessor();
        if (hookregs != null && req instanceof BundleRequirement) {
            Collection<BundleCapability> bcaps = new ArrayList<BundleCapability>();
            for (Capability cap : result) {
                XResource res = (XResource) cap.getResource();
                if (res.getWiringSupport().getWiring(true) != null || hookregs.hasResource(res)) {
                    bcaps.add((BundleCapability) cap);
                }
            }
            bcaps = new RemoveOnlyCollection<BundleCapability>(bcaps);
            hookregs.filterMatches((BundleRequirement) req, bcaps);

            // Remove the filtered caps
            Iterator<Capability> iterator = result.iterator();
            while (iterator.hasNext()) {
                Capability cap = iterator.next();
                if (!bcaps.contains(cap)) {
                    iterator.remove();
                }
            }
        }

        LOGGER.tracef("Env provides: %s => %s", req, result);
        return result;
    }

    @Override
    public synchronized Map<Resource, Wiring> updateWiring(Map<Resource, List<Wire>> wiremap) {
        if (wiremap == null)
            throw MESSAGES.illegalArgumentNull("wiremap");

        Map<Resource, Wiring> result = new HashMap<Resource, Wiring>();
        for (Map.Entry<Resource, List<Wire>> entry : wiremap.entrySet()) {

            XResource requirer = (XResource) entry.getKey();
            List<Wire> reqwires = entry.getValue();
            XWiringSupport rwsupport = requirer.getWiringSupport();
            XWiring reqwiring = rwsupport.getWiring(true);
            if (reqwiring == null) {
                reqwiring = createWiring(requirer, reqwires, null);
                rwsupport.setWiring(reqwiring);
            } else {
                for (Wire wire : reqwires) {
                    reqwiring.addRequiredWire(wire);
                }
            }
            result.put(requirer, reqwiring);

            for (Wire wire : reqwires) {
                XResource provider = (XResource) wire.getProvider();
                XWiringSupport pwsupport = provider.getWiringSupport();
                XWiring provwiring = pwsupport.getWiring(true);
                if (provwiring == null) {
                    provwiring = createWiring(provider, null, null);
                    pwsupport.setWiring(provwiring);
                }
                provwiring.addProvidedWire(wire);
            }
        }

        return Collections.unmodifiableMap(result);
    }

    @Override
    public synchronized Map<Resource, Wiring> getWirings() {
        Map<Resource, Wiring> result = new HashMap<Resource, Wiring>();
        for (XResource res : resourceIndexCache.values()) {
            Wiring wiring = res.getWiringSupport().getWiring(true);
            if (wiring != null) {
                result.put(res, wiring);
            }
        }
        return Collections.unmodifiableMap(result);
    }

    private XWiring createWiring(XResource res, List<Wire> required, List<Wire> provided) {
        if (res instanceof XBundleRevision) {