Interface LazyCreationService
-
- All Known Implementing Classes:
LazyCreationServiceImpl
public interface LazyCreationService
Service that encapsulates a 'get or create' pattern for resources (incl. possibly a path several parent directories), that adresses the following problems:- The creation is cluster-safe. If requests in several cluster nodes need the same resource and try to create it at the same time, only one of them will succeed. This should, however, not cause the other requests to fail.
- The user needs the right to read the resource, but shouldn't neccesarily have the right to create or modify the resource. Thus, the creation is done with an admin resolver, but the result is returned with the user's resolver.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
LazyCreationService.CreationStrategy
Strategy to create the resource itself.static interface
LazyCreationService.InitializationStrategy
Strategy to initialize the resource, if that's a resource intensive task that should be separated fromLazyCreationService.CreationStrategy
.static interface
LazyCreationService.ParentCreationStrategy
Strategy to create the parents of the retrieved resource.static interface
LazyCreationService.RetrievalStrategy<T>
Strategy to retrieve the resources content.
-
Field Summary
Fields Modifier and Type Field Description static LazyCreationService.RetrievalStrategy<org.apache.sling.api.resource.Resource>
IDENTITY_RETRIEVER
SimplestLazyCreationService.RetrievalStrategy
: just returns the resource.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> T
getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.InitializationStrategy initializer, LazyCreationService.ParentCreationStrategy parentCreationStrategy)
Retrieves a resource or applies a creation and initialization strategy to be carried out with an admin resolver to create it, for resource intensive initialization processes that should not performed twice in the cluster.<T> T
getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.InitializationStrategy initializer, Map<String,Object> parentProperties)
Retrieves a resource or applies a creation and initialization strategy to be carried out with an admin resolver to create it, for resource intensive initialization processes that should not performed twice in the cluster.<T> T
getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.ParentCreationStrategy parentCreationStrategy)
Retrieves a resource or applies a creation strategy to be carried out with an admin resolver to create it.<T> T
getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, Map<String,Object> parentProperties)
Retrieves a resource or applies a creation strategy to be carried out with an admin resolver to create it.boolean
isInitialized(org.apache.sling.api.resource.Resource resource)
For resources created bygetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, ParentCreationStrategy)
orgetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, Map)
, this returns true when the initialization process is finished.org.apache.sling.api.resource.Resource
waitForInitialization(org.apache.sling.api.resource.ResourceResolver resolver, String path)
For resources created bygetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, ParentCreationStrategy)
orgetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, Map)
, this returns the resource when the initialization process is finished.
-
-
-
Field Detail
-
IDENTITY_RETRIEVER
static final LazyCreationService.RetrievalStrategy<org.apache.sling.api.resource.Resource> IDENTITY_RETRIEVER
SimplestLazyCreationService.RetrievalStrategy
: just returns the resource.
-
-
Method Detail
-
getOrCreate
<T> T getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, Map<String,Object> parentProperties) throws javax.jcr.RepositoryException
Retrieves a resource or applies a creation strategy to be carried out with an admin resolver to create it.It is an error if getter still returns null after creator is executed.
- Type Parameters:
T
- the type theLazyCreationService.RetrievalStrategy
returns.- Parameters:
resolver
- the users resolver. If the resource needed to be created, that is getter returns null, we create it and call aSession.refresh(boolean)
(true) on this session before we call getter again. Passed as a parameter to getter.path
- the absolute path at which the creator creates the resource. Passed as a parameter to getter and creator.getter
- side effect free function to retrieve the resource. This can be executed several times in this process.creator
- a strategy to create the resource. Only called when the resource doesn't exist. Is only called after the parent of path is created.parentProperties
- properties with which non-existing parents of path are created (ResourceResolver.create(Resource, String, Map)
).- Returns:
- the object.
- Throws:
javax.jcr.RepositoryException
-
getOrCreate
<T> T getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.ParentCreationStrategy parentCreationStrategy) throws javax.jcr.RepositoryException
Retrieves a resource or applies a creation strategy to be carried out with an admin resolver to create it.It is an error if getter still returns null after creator is executed.
- Type Parameters:
T
- the type theLazyCreationService.RetrievalStrategy
returns.- Parameters:
resolver
- the users resolver. If the resource needed to be created, that is getter returns null, we create it and call aSession.refresh(boolean)
(true) on this session before we call getter again. Passed as a parameter to getter.path
- the absolute path at which the creator creates the resource. Passed as a parameter to getter and creator.getter
- side effect free function to retrieve the resource. This can be executed several times in this process.creator
- a strategy to create the resource. Only called when the resource doesn't exist. Is only called after the parent of path is created.parentCreationStrategy
- strategy with which non-existing parents of path are created.- Returns:
- the object.
- Throws:
javax.jcr.RepositoryException
-
getOrCreate
<T> T getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.InitializationStrategy initializer, Map<String,Object> parentProperties) throws javax.jcr.RepositoryException, org.apache.sling.api.resource.PersistenceException
Retrieves a resource or applies a creation and initialization strategy to be carried out with an admin resolver to create it, for resource intensive initialization processes that should not performed twice in the cluster. The resource is locked for the cluster withLockManager
during that time.It is an error if getter still returns null after creator is executed.
- Type Parameters:
T
- the type theLazyCreationService.RetrievalStrategy
returns.- Parameters:
resolver
- the users resolver. If the resource needed to be created, that is getter returns null, we create it and call aSession.refresh(boolean)
(true) on this session before we call getter again. Passed as a parameter to getter.path
- the absolute path at which the creator creates the resource. Passed as a parameter to getter and creator.getter
- side effect free function to retrieve the resource. This can be executed several times in this process.creator
- a strategy to create the resource. Only called when the resource doesn't exist. Is only called after the parent of path is created. This should not perform any resource intensive actions - these should be done in initializer.initializer
- the resource intensive part of the resource creationparentProperties
- properties with which non-existing parents of path are created (ResourceResolver.create(Resource, String, Map)
).- Returns:
- the object.
- Throws:
javax.jcr.RepositoryException
org.apache.sling.api.resource.PersistenceException
-
getOrCreate
<T> T getOrCreate(org.apache.sling.api.resource.ResourceResolver resolver, String path, LazyCreationService.RetrievalStrategy<T> getter, LazyCreationService.CreationStrategy creator, LazyCreationService.InitializationStrategy initializer, LazyCreationService.ParentCreationStrategy parentCreationStrategy) throws javax.jcr.RepositoryException, org.apache.sling.api.resource.PersistenceException
Retrieves a resource or applies a creation and initialization strategy to be carried out with an admin resolver to create it, for resource intensive initialization processes that should not performed twice in the cluster. The resource is locked for the cluster withLockManager
during that time.It is an error if getter still returns null after creator is executed.
- Type Parameters:
T
- the type theLazyCreationService.RetrievalStrategy
returns.- Parameters:
resolver
- the users resolver. If the resource needed to be created, that is getter returns null, we create it and call aSession.refresh(boolean)
(true) on this session before we call getter again. Passed as a parameter to getter.path
- the absolute path at which the creator creates the resource. Passed as a parameter to getter and creator.getter
- side effect free function to retrieve the resource. This can be executed several times in this process.creator
- a strategy to create the resource. Only called when the resource doesn't exist. Is only called after the parent of path is created.initializer
- the resource intensive part of the resource creationparentCreationStrategy
- strategy with which non-existing parents of path are created.- Returns:
- the object.
- Throws:
javax.jcr.RepositoryException
org.apache.sling.api.resource.PersistenceException
-
isInitialized
boolean isInitialized(org.apache.sling.api.resource.Resource resource) throws javax.jcr.RepositoryException
For resources created bygetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, ParentCreationStrategy)
orgetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, Map)
, this returns true when the initialization process is finished.- Throws:
javax.jcr.RepositoryException
-
waitForInitialization
org.apache.sling.api.resource.Resource waitForInitialization(org.apache.sling.api.resource.ResourceResolver resolver, String path) throws javax.jcr.RepositoryException
For resources created bygetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, ParentCreationStrategy)
orgetOrCreate(ResourceResolver, String, RetrievalStrategy, CreationStrategy, InitializationStrategy, Map)
, this returns the resource when the initialization process is finished. If the resource is in creation, this waits a while.- Returns:
- the initialized resource, or null if we couldn't find it or it took too long.
- Throws:
javax.jcr.RepositoryException
-
-