private void unregisterAdapterFactory()

in core/src/main/java/org/apache/sling/testing/mock/sling/MockAdapterManagerImpl.java [343:399]


    private void unregisterAdapterFactory(final ServiceReference reference) {
        synchronized (this.boundAdapterFactories) {
            boundAdapterFactories.remove(reference);
        }
        final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
        final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));

        if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
            return;
        }

        boolean factoriesModified = false;
        AdapterFactoryDescriptorMap adfMap = null;

        AdapterFactoryDescriptor removedDescriptor = null;
        for (final String adaptable : adaptables) {
            synchronized (this.descriptors) {
                adfMap = this.descriptors.get(adaptable);
            }
            if (adfMap != null) {
                synchronized (adfMap) {
                    AdapterFactoryDescriptor factoryDesc = adfMap.remove(reference);
                    if (factoryDesc != null) {
                        factoriesModified = true;
                        // A single ServiceReference should correspond to a single Adaption service being registered
                        // Since the code paths above does not fully guarantee it though, let's keep this check in place
                        if (removedDescriptor != null && removedDescriptor != factoryDesc) {
                            log.error(
                                    "When unregistering reference {} got duplicate service descriptors {} and {}. Unregistration of {} services may be incomplete.",
                                    new Object[] {reference, removedDescriptor, factoryDesc, Adaption.class.getName()});
                        }
                        removedDescriptor = factoryDesc;
                    }
                }
            }
        }

        // only remove cache if some adapter factories have actually been
        // removed
        if (factoriesModified) {
            this.factoryCache.clear();
        }

        // unregister adaption
        if (removedDescriptor != null) {
            removedDescriptor.getAdaption().unregister();
            if (log.isDebugEnabled()) {
                log.debug("Unregistered service {} with {} : {} and {} : {}", new Object[] {
                    Adaption.class.getName(),
                    SlingConstants.PROPERTY_ADAPTABLE_CLASSES,
                    Arrays.toString(adaptables),
                    SlingConstants.PROPERTY_ADAPTER_CLASSES,
                    Arrays.toString(adapters)
                });
            }
        }
    }