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 Summary
FieldsModifier and TypeFieldDescriptionprotected org.springframework.context.ApplicationContextprotected intContext build retry max countprotected longContext build retry wait time base in millisecondsprotected intContext build retry wait time multipleprotected SiteContextFactoryprotected Map<String, SiteContext> protected Stringprotected Map<String, AtomicInteger> Directory watcher counter to count modified eventsprotected Map<String, ScheduledExecutorService> Directory watcher site rebuild tasksDirectory watcher processed event hashDirectory watcher registry for each siteprotected org.craftercms.commons.entitlements.validator.EntitlementValidatorprotected SiteContextFactoryprotected Executorprotected booleantrue if Engine is in preview mode, false otherwiseprotected SiteListResolverprotected org.craftercms.commons.concurrent.locks.KeyBasedLockFactory<ReentrantLock> protected booleanprotected intDirectory watcher counter limit to run rebuildprotected String[]Directory watcher ignore pathsprotected intDirectory watcher interval period to run rebuild task checkprotected String[]Directory watcher watch paths -
Constructor Summary
ConstructorsConstructorDescriptionSiteContextManager(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 Summary
Modifier and TypeMethodDescriptionprotected SiteContextcreateContext(String siteName, boolean fallback) voidcreateContexts(boolean concurrent) Creates all contexts (if not already created) from the site list resolvervoiddestroy()voidDestroys all contextsprotected voiddestroyContext(String siteName) Destroys the context for the specified site name, and removes it from the registry, effectively purging itprotected voiddestroyContext(SiteContext siteContext) protected voiddestroyContexts(Collection<String> siteNames) getContext(String siteName, boolean fallback) Gets theSiteContextfor the specified site name.protected SiteContextgetContextWithRetries(String siteName) Get a site context and initializing if it does not exist.booleanhasValidContext(String siteId) Determine if a site has valid contextprotected SiteContextrebuildContext(String siteName, boolean fallback) voidregisterPreviewRebuildTask(String siteName, boolean isFallback) Register preview rebuild task.protected voidregisterPreviewWatcher(String siteName) Register files watcher for preview mode Any files from watcherPaths will be watched for CREATE, MODIFY, DELETE actionsvoidsetApplicationContext(org.springframework.context.ApplicationContext applicationContext) voidsetEntitlementValidator(org.craftercms.commons.entitlements.validator.EntitlementValidator entitlementValidator) voidstartContextRebuild(String siteName, boolean fallback) Starts a context rebuild in the backgroundvoidstartContextRebuild(String siteName, boolean fallback, Consumer<SiteContext> callback) Starts a context rebuild in the backgroundvoidstartDestroyContext(String siteName) Starts a destroy context in the backgroundvoidRebuild all site contexts currently found in the registryvoidprotected boolean
-
Field Details
-
applicationContext
protected org.springframework.context.ApplicationContext applicationContext -
siteLockFactory
protected org.craftercms.commons.concurrent.locks.KeyBasedLockFactory<ReentrantLock> siteLockFactory -
contextRegistry
-
contextFactory
-
fallbackContextFactory
-
siteListResolver
-
entitlementValidator
protected org.craftercms.commons.entitlements.validator.EntitlementValidator entitlementValidator -
waitForContextInit
protected boolean waitForContextInit -
jobThreadPoolExecutor
-
defaultSiteName
-
directoryWatcherRegistry
Directory watcher registry for each site -
directoryWatcherLastProcessedHash
Directory watcher processed event hash -
directoryWatcherCounter
Directory watcher counter to count modified events -
directoryWatcherExecutor
Directory watcher site rebuild tasks -
watcherPaths
Directory watcher watch paths -
watcherIgnorePaths
Directory watcher ignore paths -
watcherCounterLimit
protected int watcherCounterLimitDirectory watcher counter limit to run rebuild -
watcherIntervalPeriod
protected int watcherIntervalPeriodDirectory watcher interval period to run rebuild task check -
contextBuildRetryMaxCount
protected int contextBuildRetryMaxCountContext build retry max count -
contextBuildRetryWaitTimeBase
protected long contextBuildRetryWaitTimeBaseContext build retry wait time base in milliseconds -
contextBuildRetryWaitTimeMultiplier
protected int contextBuildRetryWaitTimeMultiplierContext build retry wait time multiple -
modePreview
protected boolean modePreviewtrue 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:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware
-
setEntitlementValidator
@Autowired public void setEntitlementValidator(@Lazy org.craftercms.commons.entitlements.validator.EntitlementValidator entitlementValidator) -
destroy
public void destroy()- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-
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
Register files watcher for preview mode Any files from watcherPaths will be watched for CREATE, MODIFY, DELETE actions- Parameters:
siteName- site name
-
registerPreviewRebuildTask
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 nameisFallback- is fallback
-
syncContexts
public void syncContexts() -
destroyAllContexts
public void destroyAllContexts()Destroys all contexts -
getContext
Gets theSiteContextfor the specified site name. If no context exists, a new one is created.- Parameters:
siteName- the context's site namefallback- if the context is a fallback (which means it will be used if no context can be resolved during requests- Returns:
- the context
-
startContextRebuild
Starts a context rebuild in the background- Parameters:
siteName- the site name of the contextfallback- if the new context should be a fallback context
-
startContextRebuild
Starts a context rebuild in the background- Parameters:
siteName- the site name of the contextfallback- if the new context should be a fallback contextcallback- function to call with the new context after it has been rebuilt
-
getContextWithRetries
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
Determine if a site has valid context- Parameters:
siteId- the site id- Returns:
- true if site has valid context, false otherwise
-
startDestroyContext
Starts a destroy context in the background- Parameters:
siteName- the site name of the context
-
destroyContext
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
-
createContext
-
rebuildContext
-
destroyContext
-
validateSiteCreationEntitlement
protected boolean validateSiteCreationEntitlement() -
startRebuildAll
public void startRebuildAll()Rebuild all site contexts currently found in the registry
-