Search in sources :

Example 1 with ResourceProviderTracker

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker in project sling by apache.

the class FactoryPreconditions method checkPreconditions.

public boolean checkPreconditions(final String unavailableName, final String unavailableServicePid) {
    synchronized (this) {
        final List<RequiredProvider> localRequiredProviders = this.requiredProviders;
        final ResourceProviderTracker localTracker = this.tracker;
        boolean canRegister = localTracker != null;
        if (localRequiredProviders != null && localTracker != null) {
            for (final RequiredProvider rp : localRequiredProviders) {
                canRegister = false;
                for (final ResourceProviderHandler h : localTracker.getResourceProviderStorage().getAllHandlers()) {
                    final ServiceReference ref = h.getInfo().getServiceReference();
                    final Object servicePid = ref.getProperty(Constants.SERVICE_PID);
                    if (unavailableServicePid != null && unavailableServicePid.equals(servicePid)) {
                        // ignore this service
                        continue;
                    }
                    if (unavailableName != null && unavailableName.equals(h.getInfo().getName())) {
                        // ignore this service
                        continue;
                    }
                    if (rp.name != null && rp.name.equals(h.getInfo().getName())) {
                        canRegister = true;
                        break;
                    } else if (rp.filter != null && rp.filter.match(ref)) {
                        canRegister = true;
                        break;
                    } else if (rp.pid != null && rp.pid.equals(servicePid)) {
                        canRegister = true;
                        break;
                    } else if (rp.pid != null && rp.pid.equals(ref.getProperty(LegacyResourceProviderWhiteboard.ORIGINAL_SERVICE_PID))) {
                        canRegister = true;
                        break;
                    }
                }
                if (!canRegister) {
                    break;
                }
            }
        }
        return canRegister;
    }
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) ServiceReference(org.osgi.framework.ServiceReference)

Example 2 with ResourceProviderTracker

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker in project sling by apache.

the class ResourceResolverFactoryActivator method activate.

// ---------- SCR Integration ---------------------------------------------
/**
     * Activates this component (called by SCR before)
     */
@Activate
protected void activate(final BundleContext bundleContext, final ResourceResolverFactoryConfig config) {
    this.bundleContext = bundleContext;
    this.config = config;
    final BidiMap virtuals = new TreeBidiMap();
    for (int i = 0; config.resource_resolver_virtual() != null && i < config.resource_resolver_virtual().length; i++) {
        final String[] parts = Mapping.split(config.resource_resolver_virtual()[i]);
        virtuals.put(parts[0], parts[2]);
    }
    virtualURLMap = virtuals;
    final List<Mapping> maps = new ArrayList<>();
    for (int i = 0; config.resource_resolver_mapping() != null && i < config.resource_resolver_mapping().length; i++) {
        maps.add(new Mapping(config.resource_resolver_mapping()[i]));
    }
    final Mapping[] tmp = maps.toArray(new Mapping[maps.size()]);
    // check whether direct mappings are allowed
    if (config.resource_resolver_allowDirect()) {
        final Mapping[] tmp2 = new Mapping[tmp.length + 1];
        tmp2[0] = Mapping.DIRECT;
        System.arraycopy(tmp, 0, tmp2, 1, tmp.length);
        mappings = tmp2;
    } else {
        mappings = tmp;
    }
    // from configuration if available
    searchPath = config.resource_resolver_searchpath();
    if (searchPath != null && searchPath.length > 0) {
        for (int i = 0; i < searchPath.length; i++) {
            // ensure leading slash
            if (!searchPath[i].startsWith("/")) {
                searchPath[i] = "/" + searchPath[i];
            }
            // ensure trailing slash
            if (!searchPath[i].endsWith("/")) {
                searchPath[i] += "/";
            }
        }
    }
    if (searchPath == null) {
        searchPath = new String[] { "/" };
    }
    // the root of the resolver mappings
    mapRoot = config.resource_resolver_map_location();
    mapRootPrefix = mapRoot + '/';
    final String[] paths = config.resource_resolver_map_observation();
    this.observationPaths = new Path[paths.length];
    for (int i = 0; i < paths.length; i++) {
        this.observationPaths[i] = new Path(paths[i]);
    }
    // vanity path white list
    this.vanityPathWhiteList = null;
    String[] vanityPathPrefixes = config.resource_resolver_vanitypath_whitelist();
    if (vanityPathPrefixes != null) {
        final List<String> prefixList = new ArrayList<>();
        for (final String value : vanityPathPrefixes) {
            if (value.trim().length() > 0) {
                if (value.trim().endsWith("/")) {
                    prefixList.add(value.trim());
                } else {
                    prefixList.add(value.trim() + "/");
                }
            }
        }
        if (prefixList.size() > 0) {
            this.vanityPathWhiteList = prefixList.toArray(new String[prefixList.size()]);
        }
    }
    // vanity path black list
    this.vanityPathBlackList = null;
    vanityPathPrefixes = config.resource_resolver_vanitypath_blacklist();
    if (vanityPathPrefixes != null) {
        final List<String> prefixList = new ArrayList<>();
        for (final String value : vanityPathPrefixes) {
            if (value.trim().length() > 0) {
                if (value.trim().endsWith("/")) {
                    prefixList.add(value.trim());
                } else {
                    prefixList.add(value.trim() + "/");
                }
            }
        }
        if (prefixList.size() > 0) {
            this.vanityPathBlackList = prefixList.toArray(new String[prefixList.size()]);
        }
    }
    // check for required property
    Set<String> requiredResourceProvidersLegacy = getStringSet(config.resource_resolver_required_providers());
    Set<String> requiredResourceProviderNames = getStringSet(config.resource_resolver_required_providernames());
    boolean hasLegacyRequiredProvider = false;
    if (requiredResourceProvidersLegacy != null) {
        hasLegacyRequiredProvider = requiredResourceProvidersLegacy.remove(ResourceResolverFactoryConfig.LEGACY_REQUIRED_PROVIDER_PID);
        if (!requiredResourceProvidersLegacy.isEmpty()) {
            logger.error("ResourceResolverFactory is using deprecated required providers configuration (resource.resolver.required.providers" + "). Please change to use the property resource.resolver.required.providernames for values: " + requiredResourceProvidersLegacy);
        } else {
            requiredResourceProvidersLegacy = null;
        }
    }
    if (hasLegacyRequiredProvider) {
        final boolean hasRequiredProvider;
        if (requiredResourceProviderNames != null) {
            hasRequiredProvider = !requiredResourceProviderNames.add(ResourceResolverFactoryConfig.REQUIRED_PROVIDER_NAME);
        } else {
            hasRequiredProvider = false;
            requiredResourceProviderNames = Collections.singleton(ResourceResolverFactoryConfig.REQUIRED_PROVIDER_NAME);
        }
        if (hasRequiredProvider) {
            logger.warn("ResourceResolverFactory is using deprecated required providers configuration (resource.resolver.required.providers" + ") with value '" + ResourceResolverFactoryConfig.LEGACY_REQUIRED_PROVIDER_PID + ". Please remove this configuration property. " + ResourceResolverFactoryConfig.REQUIRED_PROVIDER_NAME + " is already contained in the property resource.resolver.required.providernames.");
        } else {
            logger.warn("ResourceResolverFactory is using deprecated required providers configuration (resource.resolver.required.providers" + ") with value '" + ResourceResolverFactoryConfig.LEGACY_REQUIRED_PROVIDER_PID + ". Please remove this configuration property and add " + ResourceResolverFactoryConfig.REQUIRED_PROVIDER_NAME + " to the property resource.resolver.required.providernames.");
        }
    }
    // for testing: if we run unit test, both trackers are set from the outside
    if (this.resourceProviderTracker == null) {
        this.resourceProviderTracker = new ResourceProviderTracker();
        this.changeListenerWhiteboard = new ResourceChangeListenerWhiteboard();
        this.preconds.activate(this.bundleContext, requiredResourceProvidersLegacy, requiredResourceProviderNames, resourceProviderTracker);
        this.changeListenerWhiteboard.activate(this.bundleContext, this.resourceProviderTracker, searchPath);
        this.resourceProviderTracker.activate(this.bundleContext, this.eventAdmin, new ChangeListener() {

            @Override
            public void providerAdded() {
                if (factoryRegistration == null) {
                    checkFactoryPreconditions(null, null);
                }
            }

            @Override
            public void providerRemoved(final String name, final String pid, final boolean stateful, final boolean isUsed) {
                if (factoryRegistration != null) {
                    if (isUsed && (stateful || config.resource_resolver_providerhandling_paranoid())) {
                        unregisterFactory();
                    }
                    checkFactoryPreconditions(name, pid);
                }
            }
        });
    } else {
        this.preconds.activate(this.bundleContext, requiredResourceProvidersLegacy, requiredResourceProviderNames, resourceProviderTracker);
        this.checkFactoryPreconditions(null, null);
    }
}
Also used : Path(org.apache.sling.api.resource.path.Path) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) ArrayList(java.util.ArrayList) BidiMap(org.apache.commons.collections4.BidiMap) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap) Mapping(org.apache.sling.resourceresolver.impl.mapping.Mapping) ChangeListener(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener) TreeBidiMap(org.apache.commons.collections4.bidimap.TreeBidiMap) ResourceChangeListenerWhiteboard(org.apache.sling.resourceresolver.impl.observation.ResourceChangeListenerWhiteboard) Activate(org.osgi.service.component.annotations.Activate)

Example 3 with ResourceProviderTracker

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker in project sling by apache.

the class FactoryPreconditionsTest method testPIDs.

@Test
public void testPIDs() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, new HashSet<>(Arrays.asList("pid1", "pid3")), null, tracker);
    final List<ResourceProviderHandler> handlers1 = getResourceProviderHandlers(new String[] { "pid2" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers1);
    assertFalse(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] { "pid1", "pid2", "pid3" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
    assertTrue(conditions.checkPreconditions(null, null));
    final List<ResourceProviderHandler> handlers3 = getResourceProviderHandlers(new String[] { "pid1" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers3);
    assertFalse(conditions.checkPreconditions(null, null));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Example 4 with ResourceProviderTracker

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker in project sling by apache.

the class FactoryPreconditionsTest method testUnregisteringService.

@Test
public void testUnregisteringService() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, new HashSet<>(Arrays.asList("pid1", "pid3")), null, tracker);
    final List<ResourceProviderHandler> handlers2 = getResourceProviderHandlers(new String[] { "pid1", "pid2", "pid3" });
    Mockito.when(storage.getAllHandlers()).thenReturn(handlers2);
    assertTrue(conditions.checkPreconditions(null, null));
    assertTrue(conditions.checkPreconditions(null, "pid2"));
    assertFalse(conditions.checkPreconditions(null, "pid1"));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Example 5 with ResourceProviderTracker

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker in project sling by apache.

the class FactoryPreconditionsTest method testNoRequiredProviders.

@Test
public void testNoRequiredProviders() {
    final ResourceProviderTracker tracker = Mockito.mock(ResourceProviderTracker.class);
    final ResourceProviderStorage storage = Mockito.mock(ResourceProviderStorage.class);
    Mockito.when(tracker.getResourceProviderStorage()).thenReturn(storage);
    FactoryPreconditions conditions = new FactoryPreconditions();
    conditions.activate(null, null, null, tracker);
    assertTrue(conditions.checkPreconditions(null, null));
    conditions = new FactoryPreconditions();
    conditions.activate(null, Collections.<String>emptySet(), Collections.<String>emptySet(), tracker);
    assertTrue(conditions.checkPreconditions(null, null));
}
Also used : ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceProviderTracker(org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker) Test(org.junit.Test)

Aggregations

ResourceProviderTracker (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker)9 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)6 ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)5 Test (org.junit.Test)5 Before (org.junit.Before)2 ArrayList (java.util.ArrayList)1 BidiMap (org.apache.commons.collections4.BidiMap)1 TreeBidiMap (org.apache.commons.collections4.bidimap.TreeBidiMap)1 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)1 Resource (org.apache.sling.api.resource.Resource)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 Path (org.apache.sling.api.resource.path.Path)1 Mapping (org.apache.sling.resourceresolver.impl.mapping.Mapping)1 ResourceChangeListenerWhiteboard (org.apache.sling.resourceresolver.impl.observation.ResourceChangeListenerWhiteboard)1 ChangeListener (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker.ChangeListener)1 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)1 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)1 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)1 Bundle (org.osgi.framework.Bundle)1 ServiceReference (org.osgi.framework.ServiceReference)1