Search in sources :

Example 1 with ResourceProviderHandler

use of org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler 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 ResourceProviderHandler

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

the class FactoryPreconditionsTest method getResourceProviderHandlersWithNames.

private List<ResourceProviderHandler> getResourceProviderHandlersWithNames(String[] names) {
    final List<ResourceProviderHandler> result = new ArrayList<ResourceProviderHandler>();
    for (final String n : names) {
        final ResourceProviderHandler handler = Mockito.mock(ResourceProviderHandler.class);
        final ResourceProviderInfo info = Mockito.mock(ResourceProviderInfo.class);
        Mockito.when(info.getName()).thenReturn(n);
        final ServiceReference ref = Mockito.mock(ServiceReference.class);
        Mockito.when(handler.getInfo()).thenReturn(info);
        Mockito.when(info.getServiceReference()).thenReturn(ref);
        result.add(handler);
    }
    return result;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ArrayList(java.util.ArrayList) ResourceProviderInfo(org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with ResourceProviderHandler

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

the class ResourceResolverControl method checkSourceAndDest.

private AuthenticatedResourceProvider checkSourceAndDest(final ResourceResolverContext context, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    // check source
    final Node<ResourceProviderHandler> srcNode = getResourceProviderStorage().getTree().getBestMatchingNode(srcAbsPath);
    if (srcNode == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    AuthenticatedResourceProvider srcProvider = null;
    try {
        srcProvider = context.getProviderManager().getOrCreateProvider(srcNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (srcProvider == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    final Resource srcResource = srcProvider.getResource(srcAbsPath, null, null);
    if (srcResource == null) {
        throw new PersistenceException("Source resource does not exist.", null, srcAbsPath, null);
    }
    // check destination
    final Node<ResourceProviderHandler> destNode = getResourceProviderStorage().getTree().getBestMatchingNode(destAbsPath);
    if (destNode == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    AuthenticatedResourceProvider destProvider = null;
    try {
        destProvider = context.getProviderManager().getOrCreateProvider(destNode.getValue(), this);
    } catch (LoginException e) {
    // ignore
    }
    if (destProvider == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    final Resource destResource = destProvider.getResource(destAbsPath, null, null);
    if (destResource == null) {
        throw new PersistenceException("Destination resource does not exist.", null, destAbsPath, null);
    }
    // check for sub providers of src and dest
    if (srcProvider == destProvider && !collectProviders(context, srcNode) && !collectProviders(context, destNode)) {
        return srcProvider;
    }
    return null;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) PersistenceException(org.apache.sling.api.resource.PersistenceException) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) LoginException(org.apache.sling.api.resource.LoginException) AuthenticatedResourceProvider(org.apache.sling.resourceresolver.impl.providers.stateful.AuthenticatedResourceProvider)

Example 4 with ResourceProviderHandler

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

the class BasicResolveContext method getParentProviderAndContext.

private Object[] getParentProviderAndContext() {
    ResourceProvider<?> parentProvider = null;
    ResolveContext<?> parentResolveContext = null;
    if (this.parentPath != null) {
        String path = this.parentPath;
        while (path != null && parentProvider == null) {
            final Node<ResourceProviderHandler> node = this.control.getResourceProviderStorage().getTree().getBestMatchingNode(path);
            if (node != null) {
                final ResourceProviderHandler handler = node.getValue();
                try {
                    parentResolveContext = this.resolveContextManager.getOrCreateResolveContext(handler, this.control);
                    if (parentResolveContext != null) {
                        parentProvider = handler.getResourceProvider();
                    }
                } catch (final LoginException se) {
                // skip this, try next
                }
                if (parentProvider == null) {
                    parentResolveContext = null;
                    path = ResourceUtil.getParent(path);
                }
            } else {
                path = null;
            }
        }
    }
    return parentProvider != null ? new Object[] { parentProvider, parentResolveContext } : null;
}
Also used : ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) LoginException(org.apache.sling.api.resource.LoginException)

Example 5 with ResourceProviderHandler

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

the class ResourceProviderEntryTest method testRemoveTheOnlyProvider.

@Test
public void testRemoveTheOnlyProvider() throws LoginException {
    long counter = 1;
    providers.clear();
    for (String path : new String[] { "/foo", "/", "/foo/bar" }) {
        final ResourceResolver resolver = Mockito.mock(ResourceResolver.class);
        final ResourceProvider<?> p = Mockito.mock(ResourceProvider.class);
        Mockito.when(p.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(path), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(resolver));
        ++counter;
        ResourceProviderHandler h = createRPHandler(p, "rp" + counter, counter, path);
        providers.add(h);
        this.providersBasedResolver = null;
        {
            final Resource r = getResource(path);
            assertEqualsResolver(resolver, r);
            assertFalse(r instanceof SyntheticResource);
        }
        providers.remove(h);
        this.providersBasedResolver = null;
        {
            final Resource r = getResource(path);
            // If our provider is indeed gone, we should get one of the following conditions
            if (r == null) {
            //fine
            } else if (!p.equals(r.getResourceResolver())) {
            //fine
            } else {
                fail("Expecting inactive provider after removing it for " + path);
            }
        }
    }
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) ResourceProviderHandler(org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) AbstractResource(org.apache.sling.api.resource.AbstractResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) Test(org.junit.Test)

Aggregations

ResourceProviderHandler (org.apache.sling.resourceresolver.impl.providers.ResourceProviderHandler)18 Resource (org.apache.sling.api.resource.Resource)9 ResourceProviderStorage (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage)7 ResolveContext (org.apache.sling.spi.resource.provider.ResolveContext)7 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)6 Test (org.junit.Test)6 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)5 ResourceProviderTracker (org.apache.sling.resourceresolver.impl.providers.ResourceProviderTracker)5 ResourceContext (org.apache.sling.spi.resource.provider.ResourceContext)5 ResourceProvider (org.apache.sling.spi.resource.provider.ResourceProvider)5 Before (org.junit.Before)5 ArrayList (java.util.ArrayList)4 ResourceProviderInfo (org.apache.sling.resourceresolver.impl.providers.ResourceProviderInfo)4 ServiceReference (org.osgi.framework.ServiceReference)4 LoginException (org.apache.sling.api.resource.LoginException)3 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)3 ResourceAccessSecurityTracker (org.apache.sling.resourceresolver.impl.ResourceAccessSecurityTracker)3 ResourceProviderStorageProvider (org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorageProvider)3 QueryLanguageProvider (org.apache.sling.spi.resource.provider.QueryLanguageProvider)3 AbstractResource (org.apache.sling.api.resource.AbstractResource)2