Search in sources :

Example 81 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ScriptCacheImpl method activate.

@Activate
protected void activate(ScriptCacheImplConfiguration configuration, BundleContext bundleCtx) {
    threadPool = threadPoolManager.get("Script Cache Thread Pool");
    bundleContext = bundleCtx;
    additionalExtensions = configuration.org_apache_sling_scripting_cache_additional__extensions();
    int newMaxCacheSize = configuration.org_apache_sling_scripting_cache_size();
    if (newMaxCacheSize != DEFAULT_CACHE_SIZE) {
        // change the map only if there's a configuration change regarding the cache's max size
        CachingMap<CachedScript> newMap = new CachingMap<>(newMaxCacheSize);
        newMap.putAll(internalMap);
        internalMap = newMap;
    }
    ResourceResolver resolver = null;
    try {
        resolver = rrf.getServiceResourceResolver(null);
        searchPaths = resolver.getSearchPath();
    } catch (LoginException e) {
        LOGGER.error("Unable to retrieve a ResourceResolver for determining the search paths.", e);
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
    configureCache();
    active = true;
}
Also used : CachingMap(org.apache.sling.scripting.core.impl.helper.CachingMap) CachedScript(org.apache.sling.scripting.api.CachedScript) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException) Activate(org.osgi.service.component.annotations.Activate)

Example 82 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceProviderEntryTest method testAdd3ProvidersReverse.

@Test
public void testAdd3ProvidersReverse() throws LoginException {
    String firstPath = "/rootel";
    String secondPath = firstPath + "/child";
    String thirdPath = "/apps/sling/sample";
    final ResourceResolver firstResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> first = Mockito.mock(ResourceProvider.class);
    Mockito.when(first.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(firstPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(firstResolver));
    final ResourceResolver secondResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> second = Mockito.mock(ResourceProvider.class);
    Mockito.when(second.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(secondPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(secondResolver));
    final ResourceResolver thirdResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> third = Mockito.mock(ResourceProvider.class);
    Mockito.when(third.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(thirdPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(thirdResolver));
    providers.add(createRPHandler(first, "rp1", 1, firstPath));
    providers.add(createRPHandler(second, "rp2", 2, secondPath));
    providers.add(createRPHandler(third, "rp3", 3, thirdPath));
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    assertEqualsResolver(thirdResolver, getResource("/apps/sling/sample/html.js"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/apps/sling/microsling/html.js"));
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) 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) Test(org.junit.Test)

Example 83 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver 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)

Example 84 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceProviderEntryTest method testRemoveProviders.

@Test
public void testRemoveProviders() throws LoginException {
    String firstPath = "/rootel";
    String thirdPath = "/apps/sling/sample";
    String secondPath = firstPath + "/child";
    final ResourceResolver firstResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> first = Mockito.mock(ResourceProvider.class);
    Mockito.when(first.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(firstPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(firstResolver));
    final ResourceResolver secondResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> second = Mockito.mock(ResourceProvider.class);
    Mockito.when(second.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(secondPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(secondResolver));
    final ResourceResolver thirdResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> third = Mockito.mock(ResourceProvider.class);
    Mockito.when(third.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(thirdPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(thirdResolver));
    final Map<String, Object> firstProps = new HashMap<String, Object>();
    firstProps.put(Constants.SERVICE_ID, (long) 1);
    final Map<String, Object> secondProps = new HashMap<String, Object>();
    secondProps.put(Constants.SERVICE_ID, (long) 2);
    final Map<String, Object> thirdProps = new HashMap<String, Object>();
    thirdProps.put(Constants.SERVICE_ID, (long) 3);
    ResourceProviderHandler firstH = createRPHandler(first, "rp1", 1, firstPath);
    providers.add(firstH);
    providers.add(createRPHandler(second, "rp2", 2, secondPath));
    providers.add(createRPHandler(third, "rp3", 3, thirdPath));
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    providers.remove(firstH);
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/rootel/sddsf/sdfsdf/html.js"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    providers.add(firstH);
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
}
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) HashMap(java.util.HashMap) 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) Test(org.junit.Test)

Example 85 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ResourceProviderEntryTest method testAdd3Providers.

@Test
public void testAdd3Providers() throws LoginException {
    String firstPath = "/rootel";
    String secondPath = firstPath + "/child";
    String thirdPath = "/apps/sling/sample";
    final ResourceResolver firstResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> first = Mockito.mock(ResourceProvider.class);
    Mockito.when(first.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(firstPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(firstResolver));
    final ResourceResolver secondResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> second = Mockito.mock(ResourceProvider.class);
    Mockito.when(second.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(secondPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(secondResolver));
    final ResourceResolver thirdResolver = Mockito.mock(ResourceResolver.class);
    final ResourceProvider<?> third = Mockito.mock(ResourceProvider.class);
    Mockito.when(third.getResource(Mockito.any(ResolveContext.class), Mockito.startsWith(thirdPath), Mockito.any(ResourceContext.class), Mockito.any(Resource.class))).thenReturn(new TestResource(thirdResolver));
    providers.add(createRPHandler(first, "rp1", 1, firstPath));
    providers.add(createRPHandler(second, "rp2", 2, secondPath));
    providers.add(createRPHandler(third, "rp3", 3, thirdPath));
    this.providersBasedResolver = null;
    assertEqualsResolver(this.mockedRootResolver, getResource("/"));
    assertEqualsResolver(firstResolver, getResource("/rootel"));
    assertEqualsResolver(firstResolver, getResource("/rootel/html.js"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child"));
    assertEqualsResolver(secondResolver, getResource("/rootel/child/html.js"));
    assertEqualsResolver(thirdResolver, getResource("/apps/sling/sample/html.js"));
    assertEqualsResolver(this.mockedRootResolver, getResource("/apps/sling/microsling/html.js"));
}
Also used : ResolveContext(org.apache.sling.spi.resource.provider.ResolveContext) ResourceContext(org.apache.sling.spi.resource.provider.ResourceContext) 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) Test(org.junit.Test)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15