Class DefaultModulesLoader
- java.lang.Object
-
- com.marklogic.client.ext.helper.LoggingObject
-
- com.marklogic.client.ext.modulesloader.impl.DefaultModulesLoader
-
- All Implemented Interfaces:
ModulesLoader
public class DefaultModulesLoader extends LoggingObject implements ModulesLoader
Default implementation of ModulesLoader.REST modules and non-REST modules are handled in different ways. Non-REST modules are loaded via an instance of AssetFileLoader, which most likely loads modules via port 8000, directly into the desired modules database.
REST modules however have to be loaded via the REST server that they're targeted for. This class also loads them by default via multiple threads to increase performance, as it can take a couple seconds to load each set of search options, service, and transform.
To handle errors while loading REST modules in parallel, an implementation of LoadModulesFailureListener can be added to this class (ideally would have just been a Consumer that receives a Throwable). By default, an instance of SimpleLoadModulesFailureListener is added. Also by default, any Throwable caught by this class will be rethrown after all REST modules have been loaded. This behavior can be disabled by setting rethrowRestModulesFailure to false, in which case the failures are just logged.
-
-
Field Summary
-
Fields inherited from class com.marklogic.client.ext.helper.LoggingObject
logger
-
-
Constructor Summary
Constructors Constructor Description DefaultModulesLoader()Use this when you don't need to load asset modules - i.e.DefaultModulesLoader(AssetFileLoader assetFileLoader)Use this when you need to load REST modules and asset modules as well (non-REST modules).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFailureListener(LoadModulesFailureListener listener)protected voidapplyIncludeFilenamePattern()protected voidapplyJsonProperties(com.marklogic.client.admin.ServerConfigurationManager mgr, org.springframework.core.io.Resource r, java.io.File file)protected voidapplyXmlProperties(com.marklogic.client.admin.ServerConfigurationManager mgr, org.springframework.core.io.Resource r, java.io.File file)protected voidexecuteTask(java.lang.Runnable r)Protected in case a subclass wants to execute the Runnable in a different way - e.g.AssetFileLoadergetAssetFileLoader()protected java.lang.StringgetDatabaseClientInfo(com.marklogic.client.DatabaseClient client)ExtensionMetadataProvidergetExtensionMetadataProvider()protected java.lang.StringgetExtensionNameFromFile(org.springframework.core.io.Resource r)java.util.List<LoadModulesFailureListener>getFailureListeners()protected java.io.FilegetFileFromResource(org.springframework.core.io.Resource r)ModulesManagergetModulesManager()StaticCheckergetStaticChecker()TokenReplacergetTokenReplacer()protected booleanignoreResource(org.springframework.core.io.Resource r)If includeFilenamePattern is not null, then it is matched against the absolute path of the File that is resolved from the given resource.voidinitializeDefaultTaskExecutor()org.springframework.core.io.ResourceinstallNamespace(org.springframework.core.io.Resource r)org.springframework.core.io.ResourceinstallQueryOptions(org.springframework.core.io.Resource r)org.springframework.core.io.ResourceinstallService(org.springframework.core.io.Resource r, com.marklogic.client.admin.ExtensionMetadata metadata, com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters... methodParams)org.springframework.core.io.ResourceinstallTransform(org.springframework.core.io.Resource r, com.marklogic.client.admin.ExtensionMetadata metadata)booleanisCatchExceptions()protected voidloadAssets(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)java.util.Set<org.springframework.core.io.Resource>loadModules(com.marklogic.client.DatabaseClient client, ModulesFinder modulesFinder, java.lang.String... paths)Load modules from the given base directory, selecting modules via the given ModulesFinder, and loading them via the given DatabaseClient.protected java.util.Set<org.springframework.core.io.Resource>loadModules(Modules modules)java.util.Set<org.springframework.core.io.Resource>loadModules(java.lang.String baseDir, ModulesFinder modulesFinder, com.marklogic.client.DatabaseClient client)Just delegates to the loadModules method that takes an array of paths.protected voidloadNamespaces(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)protected voidloadProperties(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)Only supports a JSON file.protected voidloadQueryOptions(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)protected voidloadResources(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)protected voidloadTransforms(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)protected java.lang.StringreadAndReplaceTokens(org.springframework.core.io.Resource r)Handles reading in the content of a Resource, and then replacing tokens in it if a tokenReplacer has been set.voidremoveFailureListener(LoadModulesFailureListener listener)protected voidrethrowRestModulesFailureIfOneExists()voidsetAssetFileLoader(AssetFileLoader assetFileLoader)voidsetCatchExceptions(boolean catchExceptions)voidsetDatabaseClient(com.marklogic.client.DatabaseClient client)voidsetExtensionMetadataProvider(ExtensionMetadataProvider extensionMetadataProvider)voidsetIncludeFilenamePattern(java.util.regex.Pattern includeFilenamePattern)voidsetModulesManager(ModulesManager modulesManager)voidsetRethrowRestModulesFailure(boolean rethrowRestModulesFailure)voidsetShutdownTaskExecutorAfterLoadingModules(boolean shutdownTaskExecutorAfterLoadingModules)voidsetStaticChecker(StaticChecker staticChecker)voidsetTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)voidsetTaskThreadCount(int taskThreadCount)voidsetTokenReplacer(TokenReplacer tokenReplacer)voidwaitForTaskExecutorToFinish()If an AsyncTaskExecutor is used for loading options/services/transforms, we need to wait for the tasks to complete before we e.g.-
Methods inherited from class com.marklogic.client.ext.helper.LoggingObject
format
-
-
-
-
Constructor Detail
-
DefaultModulesLoader
public DefaultModulesLoader(AssetFileLoader assetFileLoader)
Use this when you need to load REST modules and asset modules as well (non-REST modules).- Parameters:
assetFileLoader-
-
DefaultModulesLoader
public DefaultModulesLoader()
Use this when you don't need to load asset modules - i.e. only need to load REST modules.
-
-
Method Detail
-
initializeDefaultTaskExecutor
public void initializeDefaultTaskExecutor()
-
loadModules
public java.util.Set<org.springframework.core.io.Resource> loadModules(com.marklogic.client.DatabaseClient client, ModulesFinder modulesFinder, java.lang.String... paths)Load modules from the given base directory, selecting modules via the given ModulesFinder, and loading them via the given DatabaseClient. Note that asset modules will not be loaded by the DatabaseClient that's passed in here, because the /v1/ext endpoint is too slow when there are e.g. hundreds of modules or more - asset modules are instead loaded via AssetFileLoader.- Specified by:
loadModulesin interfaceModulesLoaderpaths- the DatabaseClient to use for loading REST extensions- Returns:
- the set of resources containing all modules written
-
loadModules
public java.util.Set<org.springframework.core.io.Resource> loadModules(java.lang.String baseDir, ModulesFinder modulesFinder, com.marklogic.client.DatabaseClient client)Just delegates to the loadModules method that takes an array of paths.- Specified by:
loadModulesin interfaceModulesLoader- Parameters:
baseDir-modulesFinder-client-- Returns:
- the set of resources capturing each module that was written
-
loadModules
protected java.util.Set<org.springframework.core.io.Resource> loadModules(Modules modules)
-
rethrowRestModulesFailureIfOneExists
protected void rethrowRestModulesFailureIfOneExists()
-
waitForTaskExecutorToFinish
public void waitForTaskExecutorToFinish()
If an AsyncTaskExecutor is used for loading options/services/transforms, we need to wait for the tasks to complete before we e.g. release the DatabaseClient.
-
loadProperties
protected void loadProperties(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
Only supports a JSON file.- Parameters:
modules-loadedModules-
-
applyJsonProperties
protected void applyJsonProperties(com.marklogic.client.admin.ServerConfigurationManager mgr, org.springframework.core.io.Resource r, java.io.File file)
-
applyXmlProperties
protected void applyXmlProperties(com.marklogic.client.admin.ServerConfigurationManager mgr, org.springframework.core.io.Resource r, java.io.File file)- Parameters:
mgr-r-file-
-
getFileFromResource
protected java.io.File getFileFromResource(org.springframework.core.io.Resource r)
-
loadAssets
protected void loadAssets(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
- Parameters:
modules-loadedModules-
-
applyIncludeFilenamePattern
protected void applyIncludeFilenamePattern()
-
loadQueryOptions
protected void loadQueryOptions(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
- Parameters:
modules-loadedModules-
-
loadTransforms
protected void loadTransforms(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
- Parameters:
modules-loadedModules-
-
loadResources
protected void loadResources(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
- Parameters:
modules-loadedModules-
-
loadNamespaces
protected void loadNamespaces(Modules modules, java.util.Set<org.springframework.core.io.Resource> loadedModules)
- Parameters:
modules-loadedModules-
-
installService
public org.springframework.core.io.Resource installService(org.springframework.core.io.Resource r, com.marklogic.client.admin.ExtensionMetadata metadata, com.marklogic.client.admin.ResourceExtensionsManager.MethodParameters... methodParams)- Parameters:
r-metadata-methodParams-- Returns:
- the resource is one was installed, else null
-
installTransform
public org.springframework.core.io.Resource installTransform(org.springframework.core.io.Resource r, com.marklogic.client.admin.ExtensionMetadata metadata)- Parameters:
r-metadata-- Returns:
- the resource is one was installed, else null
-
installQueryOptions
public org.springframework.core.io.Resource installQueryOptions(org.springframework.core.io.Resource r)
- Parameters:
r-- Returns:
- the resource is one was installed, else null
-
readAndReplaceTokens
protected java.lang.String readAndReplaceTokens(org.springframework.core.io.Resource r)
Handles reading in the content of a Resource, and then replacing tokens in it if a tokenReplacer has been set.
-
executeTask
protected void executeTask(java.lang.Runnable r)
Protected in case a subclass wants to execute the Runnable in a different way - e.g. capturing the Future that could be returned.- Parameters:
r-
-
installNamespace
public org.springframework.core.io.Resource installNamespace(org.springframework.core.io.Resource r)
- Parameters:
r-- Returns:
- the resource if one is installed, else null
-
getExtensionNameFromFile
protected java.lang.String getExtensionNameFromFile(org.springframework.core.io.Resource r)
- Parameters:
r-- Returns:
- the extension name if one exists; if not, then the filename
-
ignoreResource
protected boolean ignoreResource(org.springframework.core.io.Resource r)
If includeFilenamePattern is not null, then it is matched against the absolute path of the File that is resolved from the given resource. If the pattern doesn't match the absolute path, then true is returned. If a File cannot be resolved, then false is returned.- Parameters:
r-- Returns:
- true if resource should be ignored
-
setDatabaseClient
public void setDatabaseClient(com.marklogic.client.DatabaseClient client)
-
setExtensionMetadataProvider
public void setExtensionMetadataProvider(ExtensionMetadataProvider extensionMetadataProvider)
-
setModulesManager
public void setModulesManager(ModulesManager modulesManager)
-
isCatchExceptions
public boolean isCatchExceptions()
-
setCatchExceptions
public void setCatchExceptions(boolean catchExceptions)
-
getExtensionMetadataProvider
public ExtensionMetadataProvider getExtensionMetadataProvider()
-
getModulesManager
public ModulesManager getModulesManager()
-
setStaticChecker
public void setStaticChecker(StaticChecker staticChecker)
-
getStaticChecker
public StaticChecker getStaticChecker()
-
setTaskExecutor
public void setTaskExecutor(org.springframework.core.task.TaskExecutor taskExecutor)
-
setTaskThreadCount
public void setTaskThreadCount(int taskThreadCount)
-
setShutdownTaskExecutorAfterLoadingModules
public void setShutdownTaskExecutorAfterLoadingModules(boolean shutdownTaskExecutorAfterLoadingModules)
-
setAssetFileLoader
public void setAssetFileLoader(AssetFileLoader assetFileLoader)
-
addFailureListener
public void addFailureListener(LoadModulesFailureListener listener)
-
removeFailureListener
public void removeFailureListener(LoadModulesFailureListener listener)
-
getDatabaseClientInfo
protected java.lang.String getDatabaseClientInfo(com.marklogic.client.DatabaseClient client)
- Parameters:
client-- Returns:
- a string that is useful for logging information about the DatabaseClient connection before a call is made to load a REST extension.
-
getAssetFileLoader
public AssetFileLoader getAssetFileLoader()
-
getFailureListeners
public java.util.List<LoadModulesFailureListener> getFailureListeners()
-
getTokenReplacer
public TokenReplacer getTokenReplacer()
-
setTokenReplacer
public void setTokenReplacer(TokenReplacer tokenReplacer)
-
setIncludeFilenamePattern
public void setIncludeFilenamePattern(java.util.regex.Pattern includeFilenamePattern)
-
setRethrowRestModulesFailure
public void setRethrowRestModulesFailure(boolean rethrowRestModulesFailure)
-
-