Class SiteContextManager

java.lang.Object
org.craftercms.engine.service.context.SiteContextManager
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.DisposableBean, org.springframework.context.ApplicationContextAware

@Validated public class SiteContextManager extends Object implements org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.DisposableBean
Registry and lifecycle manager of SiteContexts.
Author:
Alfonso Vásquez
  • Field Details

    • applicationContext

      protected org.springframework.context.ApplicationContext applicationContext
    • siteLockFactory

      protected org.craftercms.commons.concurrent.locks.KeyBasedLockFactory<ReentrantLock> siteLockFactory
    • contextRegistry

      protected Map<String,SiteContext> contextRegistry
    • contextFactory

      protected SiteContextFactory contextFactory
    • fallbackContextFactory

      protected SiteContextFactory fallbackContextFactory
    • siteListResolver

      protected SiteListResolver siteListResolver
    • entitlementValidator

      protected org.craftercms.commons.entitlements.validator.EntitlementValidator entitlementValidator
    • waitForContextInit

      protected boolean waitForContextInit
    • jobThreadPoolExecutor

      protected Executor jobThreadPoolExecutor
    • defaultSiteName

      protected String defaultSiteName
    • directoryWatcherRegistry

      protected Map<String,io.methvin.watcher.DirectoryWatcher> directoryWatcherRegistry
      Directory watcher registry for each site
    • directoryWatcherLastProcessedHash

      protected Map<String,String> directoryWatcherLastProcessedHash
      Directory watcher processed event hash
    • directoryWatcherCounter

      protected Map<String,AtomicInteger> directoryWatcherCounter
      Directory watcher counter to count modified events
    • directoryWatcherExecutor

      protected Map<String,ScheduledExecutorService> directoryWatcherExecutor
      Directory watcher site rebuild tasks
    • watcherPaths

      protected String[] watcherPaths
      Directory watcher watch paths
    • watcherIgnorePaths

      protected String[] watcherIgnorePaths
      Directory watcher ignore paths
    • watcherCounterLimit

      protected int watcherCounterLimit
      Directory watcher counter limit to run rebuild
    • watcherIntervalPeriod

      protected int watcherIntervalPeriod
      Directory watcher interval period to run rebuild task check
    • contextBuildRetryMaxCount

      protected int contextBuildRetryMaxCount
      Context build retry max count
    • contextBuildRetryWaitTimeBase

      protected long contextBuildRetryWaitTimeBase
      Context build retry wait time base in milliseconds
    • contextBuildRetryWaitTimeMultiplier

      protected int contextBuildRetryWaitTimeMultiplier
      Context build retry wait time multiple
    • modePreview

      protected boolean modePreview
      true if Engine is in preview mode, false otherwise
  • Constructor Details

    • SiteContextManager

      public SiteContextManager(SiteContextFactory contextFactory, SiteContextFactory fallbackContextFactory, SiteListResolver siteListResolver, boolean waitForContextInit, Executor jobThreadPoolExecutor, String defaultSiteName, int contextBuildRetryMaxCount, long contextBuildRetryWaitTimeBase, int contextBuildRetryWaitTimeMultiplier, boolean modePreview, String[] watcherPaths, String[] watcherIgnorePaths, int watcherCounterLimit, int watcherIntervalPeriod)
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
    • setEntitlementValidator

      @Autowired public void setEntitlementValidator(@Lazy org.craftercms.commons.entitlements.validator.EntitlementValidator entitlementValidator)
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • listContexts

      public Collection<SiteContext> listContexts()
    • createContexts

      public void createContexts(boolean concurrent)
      Creates all contexts (if not already created) from the site list resolver
      Parameters:
      concurrent - if the context creation should be done concurrently
    • registerPreviewWatcher

      protected void registerPreviewWatcher(String siteName)
      Register files watcher for preview mode Any files from watcherPaths will be watched for CREATE, MODIFY, DELETE actions
      Parameters:
      siteName - site name
    • registerPreviewRebuildTask

      public void registerPreviewRebuildTask(String siteName, boolean isFallback)
      Register preview rebuild task. When the watcher triggers, set a counter to 1 and sleep for 200 (configurable) milliseconds. Wake up and check: Is the counter 5 (one second has passed)? Then, trigger a rebuild. Has anything else changed (more changes since we slept)? If so, increment the counter and sleep for 200 milliseconds. If nothing has changed, then trigger a rebuild.
      Parameters:
      siteName - site name
      isFallback - is fallback
    • syncContexts

      public void syncContexts()
    • destroyAllContexts

      public void destroyAllContexts()
      Destroys all contexts
    • getContext

      public SiteContext getContext(String siteName, boolean fallback)
      Gets the SiteContext for the specified site name. If no context exists, a new one is created.
      Parameters:
      siteName - the context's site name
      fallback - if the context is a fallback (which means it will be used if no context can be resolved during requests
      Returns:
      the context
    • startContextRebuild

      public void startContextRebuild(String siteName, boolean fallback)
      Starts a context rebuild in the background
      Parameters:
      siteName - the site name of the context
      fallback - if the new context should be a fallback context
    • startContextRebuild

      public void startContextRebuild(String siteName, boolean fallback, Consumer<SiteContext> callback)
      Starts a context rebuild in the background
      Parameters:
      siteName - the site name of the context
      fallback - if the new context should be a fallback context
      callback - function to call with the new context after it has been rebuilt
    • getContextWithRetries

      protected SiteContext getContextWithRetries(String siteName) throws InterruptedException
      Get a site context and initializing if it does not exist. If in preview mode, do not retry on failure and report the exception immediately. If in live mode, retry on failure. After the max retries count, report the exception.
      Parameters:
      siteName - the site name of the context
      Returns:
      the site context
      Throws:
      InterruptedException - if the current thread is interrupted while waiting
    • hasValidContext

      public boolean hasValidContext(String siteId)
      Determine if a site has valid context
      Parameters:
      siteId - the site id
      Returns:
      true if site has valid context, false otherwise
    • startDestroyContext

      public void startDestroyContext(String siteName)
      Starts a destroy context in the background
      Parameters:
      siteName - the site name of the context
    • destroyContext

      protected void destroyContext(String siteName)
      Destroys the context for the specified site name, and removes it from the registry, effectively purging it
      Parameters:
      siteName - the site name of the context to destroy
    • destroyContexts

      protected void destroyContexts(Collection<String> siteNames)
    • createContext

      protected SiteContext createContext(String siteName, boolean fallback)
    • rebuildContext

      protected SiteContext rebuildContext(String siteName, boolean fallback)
    • destroyContext

      protected void destroyContext(SiteContext siteContext)
    • validateSiteCreationEntitlement

      protected boolean validateSiteCreationEntitlement()
    • startRebuildAll

      public void startRebuildAll()
      Rebuild all site contexts currently found in the registry