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.
    An admin resolver is only created when needed.
    • 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 the LazyCreationService.RetrievalStrategy returns.
        Parameters:
        resolver - the users resolver. If the resource needed to be created, that is getter returns null, we create it and call a Session.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 the LazyCreationService.RetrievalStrategy returns.
        Parameters:
        resolver - the users resolver. If the resource needed to be created, that is getter returns null, we create it and call a Session.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 with LockManager during that time.

        It is an error if getter still returns null after creator is executed.

        Type Parameters:
        T - the type the LazyCreationService.RetrievalStrategy returns.
        Parameters:
        resolver - the users resolver. If the resource needed to be created, that is getter returns null, we create it and call a Session.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 creation
        parentProperties - 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 with LockManager during that time.

        It is an error if getter still returns null after creator is executed.

        Type Parameters:
        T - the type the LazyCreationService.RetrievalStrategy returns.
        Parameters:
        resolver - the users resolver. If the resource needed to be created, that is getter returns null, we create it and call a Session.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 creation
        parentCreationStrategy - strategy with which non-existing parents of path are created.
        Returns:
        the object.
        Throws:
        javax.jcr.RepositoryException
        org.apache.sling.api.resource.PersistenceException