Class BroadleafFileServiceImpl
- All Implemented Interfaces:
BroadleafFileService
This service provides a pluggable way to provide those services via FileServiceProvider
implementations.
This service can be used by any component that needs to write files to an area shared by multiple application servers.
For example usage, see SiteMapGenerator
. The Broadleaf CMS module also uses this component to load, store, and
manipulate images for the file-system.
Generally, the process to create a new asset in the shared file system is ...
1. Call initializeWorkArea() to get a temporary directory
2. Create files, directories, etc. using the FileWorkArea.filePathLocation
as the root directory.
3. Once your file processing is complete, call #addOrUpdateResources(FileWorkArea, FileApplicationType)
to
4. Call #closeWorkArea()
to clear out the temporary files
- Author:
- bpolster
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected FileServiceProvider
protected BroadleafFileServiceExtensionManager
protected String
protected List<FileServiceProvider>
protected int
protected String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addOrUpdateResource
(FileWorkArea workArea, File file, boolean removeFilesFromWorkArea) Takes in a temporary work area and a single File and copies that files to the configured FileProvider's permanent storage.addOrUpdateResourceForPath
(FileWorkArea workArea, File file, boolean removeFilesFromWorkArea) Takes in a temporary work area and a single File and copies that files to the configured FileProvider's permanent storage.void
addOrUpdateResources
(FileWorkArea workArea, boolean removeFilesFromWorkArea) Takes in a temporary work area and copies all of the files to the configured FileProvider's permanent storage.void
addOrUpdateResources
(FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) Takes in a temporary work area and a list of Files and copies them to the configured FileProvider's permanent storage.addOrUpdateResourcesForPaths
(FileWorkArea workArea, boolean removeFilesFromWorkArea) Takes in a temporary work area and copies all of the files to the configured FileProvider's permanent storage.addOrUpdateResourcesForPaths
(FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) Takes in a temporary work area and a list of Files and copies them to the configured FileProvider's permanent storage.protected void
buildFileList
(File file, Collection<File> fileList) Adds the file to the passed in Collection.protected String
protected void
checkFiles
(FileWorkArea workArea, List<File> fileList) boolean
Returns true if the resource is available on the classpath.void
closeWorkArea
(FileWorkArea fwArea) Closes the passed in work area.protected String
getBaseDirectory
(boolean skipSite) Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it exists or java.tmp.io if that property has not been set.getClasspathResource
(String name) Allows assets to be included in the Java classpath.getLocalResource
(String resourceName) Checks for a resource in the temporary directory of the file-system.protected File
getLocalResource
(String resourceName, boolean skipSite) int
getResource
(String name) Returns a File representing the passed in name.getResource
(String name, Long localTimeout) Returns a File representing the resource.getSharedLocalResource
(String resourceName) Checks for a resource in the temporary directory of the file-system.protected String
getTempDirectory
(String baseDirectory) Returns a directory that is unique for this work area.Create a file work area that can be used for further operations.protected org.springframework.core.io.ClassPathResource
protected void
removeLocalCacheFiles
(String resourceName, String baseDirectory) boolean
removeResource
(String resourceName) Removes the resource matching the passed in file name from the FileProviderprotected FileServiceProvider
Returns the FileServiceProvider that can handle the passed in application type.void
setDefaultFileServiceProvider
(FileServiceProvider defaultFileServiceProvider) void
setFileServiceProviders
(List<FileServiceProvider> fileServiceProviders) void
setMaxGeneratedDirectoryDepth
(int maxGeneratedDirectoryDepth) void
setTempFileSystemBaseDirectory
(String tempFileSystemBaseDirectory)
-
Field Details
-
fileServiceProviders
-
defaultFileServiceProvider
-
tempFileSystemBaseDirectory
-
maxGeneratedDirectoryDepth
@Value("${asset.server.max.generated.file.system.directories}") protected int maxGeneratedDirectoryDepth -
fileServiceClasspathDirectory
-
extensionManager
-
-
Constructor Details
-
BroadleafFileServiceImpl
public BroadleafFileServiceImpl()
-
-
Method Details
-
initializeWorkArea
Create a file work area that can be used for further operations.- Specified by:
initializeWorkArea
in interfaceBroadleafFileService
- Returns:
-
closeWorkArea
Closes the passed in work area. This method will delete the work area (typically a directory on the file system and all items it encloses).- Specified by:
closeWorkArea
in interfaceBroadleafFileService
- Parameters:
fwArea
- Area
-
getResource
Description copied from interface:BroadleafFileService
Returns a File representing the passed in name. This method will always access the file via the FileProvider which might be a remote operation.- Specified by:
getResource
in interfaceBroadleafFileService
- Parameters:
name
- - fully qualified path to the resource- Returns:
-
getLocalResource
-
getLocalResource
Description copied from interface:BroadleafFileService
Checks for a resource in the temporary directory of the file-system. Will check for a site-specific file.- Specified by:
getLocalResource
in interfaceBroadleafFileService
- Returns:
-
getResource
Description copied from interface:BroadleafFileService
Returns a File representing the resource. This method first checks the local temporary directory for the file. If it exists and has been modified within the timeout parameter, it will be returned. Otherwise, this method will make a call toBroadleafFileService.getResource(String)
.If the timeout parameter is null then if the resource exists locally, it will be returned.
- Specified by:
getResource
in interfaceBroadleafFileService
- Parameters:
name
- - fully qualified path to the resourcelocalTimeout
- - timeframe that the temporary file is considered valid- Returns:
-
checkForResourceOnClassPath
Description copied from interface:BroadleafFileService
Returns true if the resource is available on the classpath.- Specified by:
checkForResourceOnClassPath
in interfaceBroadleafFileService
- Returns:
-
lookupResourceOnClassPath
-
getClasspathResource
Description copied from interface:BroadleafFileService
Allows assets to be included in the Java classpath.This method was designed to support an internal Broadleaf use case and may not have general applicability beyond that. For Broadleaf demo sites, many of the product images are shared across the demo sites.
Rather than copy those images, they are stored in a Jar file and shared by all of the sites.
- Specified by:
getClasspathResource
in interfaceBroadleafFileService
- Parameters:
name
- - fully qualified path to the resource- Returns:
-
removeResource
Removes the resource matching the passed in file name from the FileProvider- Specified by:
removeResource
in interfaceBroadleafFileService
- Parameters:
resourceName
- - fully qualified path to the resource
-
addOrUpdateResource
Takes in a temporary work area and a single File and copies that files to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResource
in interfaceBroadleafFileService
-
addOrUpdateResourceForPath
public String addOrUpdateResourceForPath(FileWorkArea workArea, File file, boolean removeFilesFromWorkArea) Takes in a temporary work area and a single File and copies that files to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResourceForPath
in interfaceBroadleafFileService
- Parameters:
workArea
- the work area from the given filefile
- the file to uploadremoveFilesFromWorkArea
- whether or not the given file should be removed from workArea when it has been copied
-
addOrUpdateResources
Takes in a temporary work area and copies all of the files to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResources
in interfaceBroadleafFileService
-
addOrUpdateResourcesForPaths
public List<String> addOrUpdateResourcesForPaths(FileWorkArea workArea, boolean removeFilesFromWorkArea) Description copied from interface:BroadleafFileService
Takes in a temporary work area and copies all of the files to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResourcesForPaths
in interfaceBroadleafFileService
-
addOrUpdateResources
public void addOrUpdateResources(FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) Description copied from interface:BroadleafFileService
Takes in a temporary work area and a list of Files and copies them to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResources
in interfaceBroadleafFileService
-
addOrUpdateResourcesForPaths
public List<String> addOrUpdateResourcesForPaths(FileWorkArea workArea, List<File> files, boolean removeFilesFromWorkArea) Description copied from interface:BroadleafFileService
Takes in a temporary work area and a list of Files and copies them to the configured FileProvider's permanent storage.
Passing in removeFilesFromWorkArea to true allows for more efficient file processing when using a local file system as it performs a move operation instead of a copy.
- Specified by:
addOrUpdateResourcesForPaths
in interfaceBroadleafFileService
- Parameters:
workArea
- the work area for the given filesfiles
- the files to copy to the provider's permanent storageremoveFilesFromWorkArea
- whether or not the given files hsould be removed from the given workArea after they are uploaded
-
removeLocalCacheFiles
-
selectFileServiceProvider
Returns the FileServiceProvider that can handle the passed in application type.By default, this method returns the component configured at blFileServiceProvider
- Returns:
-
checkFiles
-
getBaseDirectory
Returns the baseDirectory for writing and reading files as the property assetFileSystemPath if it exists or java.tmp.io if that property has not been set. -
getTempDirectory
Returns a directory that is unique for this work area. -
buildThreadIdString
-
buildFileList
Adds the file to the passed in Collection. If the file is a directory, adds its children recursively. Otherwise, just adds the file to the list.- Parameters:
file
-fileList
-
-
getTempFileSystemBaseDirectory
-
setTempFileSystemBaseDirectory
-
getFileServiceProviders
-
setFileServiceProviders
-
getMaxGeneratedDirectoryDepth
public int getMaxGeneratedDirectoryDepth() -
setMaxGeneratedDirectoryDepth
public void setMaxGeneratedDirectoryDepth(int maxGeneratedDirectoryDepth) -
getDefaultFileServiceProvider
-
setDefaultFileServiceProvider
-