Author: radu Date: Thu Nov 24 13:48:20 2016 New Revision: 1771128 URL: http://svn.apache.org/viewvc?rev=1771128&view=rev Log: SLING-6165 - Expose a service for Sling Scripting that provides request-scoped Resource Resolvers for scripting dependencies
* renamed ScriptingResourceResolverFactory to ScriptingResourceResolverProvider to better reflect what this service does * corrected package version in JavaDoc in @since Added: sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverProvider.java - copied, changed from r1771125, sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverFactory.java sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImpl.java - copied, changed from r1771125, sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImpl.java sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImplTest.java - copied, changed from r1771125, sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImplTest.java Removed: sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverFactory.java sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImpl.java sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImplTest.java Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolver.java Copied: sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverProvider.java (from r1771125, sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverFactory.java) URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverProvider.java?p2=sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverProvider.java&p1=sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverFactory.java&r1=1771125&r2=1771128&rev=1771128&view=diff ============================================================================== --- sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverFactory.java (original) +++ sling/trunk/bundles/scripting/api/src/main/java/org/apache/sling/scripting/api/resolver/ScriptingResourceResolverProvider.java Thu Nov 24 13:48:20 2016 @@ -16,19 +16,18 @@ ******************************************************************************/ package org.apache.sling.scripting.api.resolver; -import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; import aQute.bnd.annotation.ProviderType; /** - * The {@code ScriptingResourceResolverFactory} provides methods to obtain {@link ResourceResolver}s that can be used by scripting + * The {@code ScriptingResourceResolverProvider} provides methods to obtain {@link ResourceResolver}s that can be used by scripting * bundles to perform their script resolution operations. * - * @since 2.4.0 + * @since 1.0.0 */ @ProviderType -public interface ScriptingResourceResolverFactory { +public interface ScriptingResourceResolverProvider { /** * <p>Provides a request-scoped {@link ResourceResolver} with only read access to the search paths.</p> @@ -42,13 +41,4 @@ public interface ScriptingResourceResolv * (e.g. reusing the same resolver for multiple threads).</p> */ ResourceResolver getRequestScopedResourceResolver(); - - /** - * Provides a {@link ResourceResolver} with only read access to the search paths. Once you're done processing {@link Resource}s with - * this {@code ResourceResolver} make sure to close it (or use it in a - * <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html"><code>try-with-resources</code><a/> - * block). - */ - ResourceResolver getResourceResolver(); - } Modified: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolver.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolver.java?rev=1771128&r1=1771127&r2=1771128&view=diff ============================================================================== --- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolver.java (original) +++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolver.java Thu Nov 24 13:48:20 2016 @@ -27,7 +27,7 @@ import org.apache.sling.api.resource.Log import org.apache.sling.api.resource.PersistenceException; import org.apache.sling.api.resource.Resource; import org.apache.sling.api.resource.ResourceResolver; -import org.apache.sling.scripting.api.resolver.ScriptingResourceResolverFactory; +import org.apache.sling.scripting.api.resolver.ScriptingResourceResolverProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -136,7 +136,7 @@ public class ScriptingResourceResolver i @Override public void close() { LOGGER.warn("Attempted to call close on the scripting per-request resource resolver. This is handled automatically by the {}.", - ScriptingResourceResolverFactory.class.getName()); + ScriptingResourceResolverProvider.class.getName()); if (shouldLogClosing) { StringWriter writer = new StringWriter(); Throwable t = new Throwable(); Copied: sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImpl.java (from r1771125, sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImpl.java) URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImpl.java?p2=sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImpl.java&p1=sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImpl.java&r1=1771125&r2=1771128&rev=1771128&view=diff ============================================================================== --- sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImpl.java (original) +++ sling/trunk/bundles/scripting/core/src/main/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImpl.java Thu Nov 24 13:48:20 2016 @@ -22,7 +22,7 @@ import javax.servlet.ServletRequestListe import org.apache.sling.api.resource.LoginException; import org.apache.sling.api.resource.ResourceResolver; import org.apache.sling.api.resource.ResourceResolverFactory; -import org.apache.sling.scripting.api.resolver.ScriptingResourceResolverFactory; +import org.apache.sling.scripting.api.resolver.ScriptingResourceResolverProvider; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; @@ -34,15 +34,15 @@ import org.slf4j.LoggerFactory; @Component( name = "Apache Sling Scripting Resource Resolver Factory", - service = {ScriptingResourceResolverFactory.class, ServletRequestListener.class}, - configurationPid = "org.apache.sling.scripting.core.impl.ScriptingResourceResolverFactoryImpl" + service = {ScriptingResourceResolverProvider.class, ServletRequestListener.class}, + configurationPid = "org.apache.sling.scripting.core.impl.ScriptingResourceResolverProviderImpl" ) @Designate( - ocd = ScriptingResourceResolverFactoryImpl.Configuration.class + ocd = ScriptingResourceResolverProviderImpl.Configuration.class ) -public class ScriptingResourceResolverFactoryImpl implements ScriptingResourceResolverFactory, ServletRequestListener { +public class ScriptingResourceResolverProviderImpl implements ScriptingResourceResolverProvider, ServletRequestListener { - private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingResourceResolverFactoryImpl.class); + private static final Logger LOGGER = LoggerFactory.getLogger(ScriptingResourceResolverProviderImpl.class); private final ThreadLocal<ScriptingResourceResolver> perThreadResourceResolver = new ThreadLocal<>(); private boolean logStackTraceOnResolverClose; @@ -85,15 +85,6 @@ public class ScriptingResourceResolverFa } @Override - public ResourceResolver getResourceResolver() { - try { - return rrf.getServiceResourceResolver(null); - } catch (LoginException e) { - throw new IllegalStateException("Unable to retrieve a scripting resource resolver.", e); - } - } - - @Override public void requestInitialized(ServletRequestEvent sre) { // we don't care about this event; the request scoped resource resolver is created lazily } Copied: sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImplTest.java (from r1771125, sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImplTest.java) URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImplTest.java?p2=sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImplTest.java&p1=sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImplTest.java&r1=1771125&r2=1771128&rev=1771128&view=diff ============================================================================== --- sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverFactoryImplTest.java (original) +++ sling/trunk/bundles/scripting/core/src/test/java/org/apache/sling/scripting/core/impl/ScriptingResourceResolverProviderImplTest.java Thu Nov 24 13:48:20 2016 @@ -41,11 +41,11 @@ import org.mockito.stubbing.Answer; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.*; -public class ScriptingResourceResolverFactoryImplTest { +public class ScriptingResourceResolverProviderImplTest { private static final int MAX_CONCURRENT_RESOLVERS = 200; private static final int RESOLVER_REUSE_FOR_SAME_THREAD = 100; - private ScriptingResourceResolverFactoryImpl scriptingResourceResolverFactory; + private ScriptingResourceResolverProviderImpl scriptingResourceResolverFactory; private Set<ResourceResolver> delegates; @Before @@ -60,7 +60,7 @@ public class ScriptingResourceResolverFa return delegate; } }); - scriptingResourceResolverFactory = new ScriptingResourceResolverFactoryImpl(); + scriptingResourceResolverFactory = new ScriptingResourceResolverProviderImpl(); Whitebox.setInternalState(scriptingResourceResolverFactory, "rrf", rrf); } @@ -101,14 +101,14 @@ public class ScriptingResourceResolverFa } - private Callable<ResourceResolver> createCallable(final ScriptingResourceResolverFactoryImpl scriptingResourceResolverFactory) { + private Callable<ResourceResolver> createCallable(final ScriptingResourceResolverProviderImpl scriptingResourceResolverFactory) { return new Callable<ResourceResolver>() { @Override public ResourceResolver call() { ResourceResolver resourceResolver = scriptingResourceResolverFactory.getRequestScopedResourceResolver(); for (int i = 0; i < RESOLVER_REUSE_FOR_SAME_THREAD; i++) { ResourceResolver subsequentResolver = scriptingResourceResolverFactory.getRequestScopedResourceResolver(); - assertEquals("Expected that subsequent calls to ScriptingResourceResolverFactory#getRequestScopedResourceResolver() " + + assertEquals("Expected that subsequent calls to ScriptingResourceResolverProvider#getRequestScopedResourceResolver() " + "from the same thread will not create additional resolvers.", resourceResolver, subsequentResolver); } scriptingResourceResolverFactory.requestDestroyed(mock(ServletRequestEvent.class));