Class CmsModificationContext

java.lang.Object
org.opencms.db.CmsModificationContext
All Implemented Interfaces:
AutoCloseable

public class CmsModificationContext extends Object implements AutoCloseable
A helper class used by CmsSecurityManager to keep track of modified resources for the 'online folders' feature and 'publish' them when we are done with them.

This class is meant to be used with the try-with-resources syntax from Java 7: Use the acquire() method in the try expression (e.g. 'try (CmsModificationContext modContext = CmsModificationContext.acquire(requestContext)) { ... }'. Then use the add() method on the modification context to add any modified resources. Once the close() method is automatically invoked by the try-with-resources statement, *and* we are in the outermost nesting level of such try-with-resources statements, the modified resources are published if they belong to a configured online folder.

  • Method Details

    • doWithModificationContext

      public static <T> T doWithModificationContext(CmsRequestContext requestContext, org.apache.commons.lang3.function.FailableSupplier<T,CmsException> runnable) throws CmsException
      Executes the given action and returns the result, wrapping the execution in a modification context.
      Type Parameters:
      T - the result type
      Parameters:
      requestContext - the current request context
      runnable - the action to execute
      Returns:
      the result of the action
      Throws:
      CmsException - if something goes wrong
    • getOnlineFolderOptions

    • initialize

      public static void initialize(CmsSecurityManager securityManager, CmsObject adminCms, CmsOnlineFolderOptions options)
      Initializes this class.
      Parameters:
      securityManager - the security manager instance
      adminCms - a CmsObject with admin privileges
      onlineFolderPath - the online folder path (nay be null)
    • isInOnlineFolder

      public static boolean isInOnlineFolder(String path)
      Checks if the given path is below the configured online folder.

      If no online folder is configured, this will return false.

      Parameters:
      path - the path to check
      Returns:
      true if the given path is below the configured online folder
    • isInstantPublishing

      public static boolean isInstantPublishing()
      Checks if an 'instant publish' operation is currently running.
      Returns:
      true if an 'instant publish' operation is running
    • acquire

      protected static CmsModificationContext acquire(CmsRequestContext context)
      Acquires a modification context.

      If a modification context was acquired in a higher stack frame, the existing context will be returned, but its level counter will be increased by 1, otherwise a new context will be created with a level counter of 1.

      Parameters:
      context - the request context for which to get a modification context
      Returns:
      the modification context
    • add

      public void add(CmsResource resource)
      Checks if the given resource is in the online only folder, and if so, adds it to the set of resources that should be instant-published when the modification context is closed.
      Parameters:
      resource - the resource to add
    • addId

      public void addId(CmsUUID structureId)
      Alternative to add(CmsResource) for methods where the resource is not read.

      Avoid using this if possible, in favor of add(CmsResource)

      Parameters:
      structureId - the structure id of the resource to add
    • close

      public void close() throws CmsException
      Decrements the modification context's level counter by 1, and finally closes it if the counter reaches zero.

      If the counter reaches zero, all resources added with the add() method will be published synchronously, without going through the publish queue.

      Specified by:
      close in interface AutoCloseable
      Throws:
      CmsException
      See Also: