org.broadleafcommerce.common.file.service
Class BroadleafFileServiceImpl

java.lang.Object
  extended by org.broadleafcommerce.common.file.service.BroadleafFileServiceImpl
All Implemented Interfaces:
BroadleafFileService

@Service(value="blFileService")
public class BroadleafFileServiceImpl
extends Object
implements BroadleafFileService

Many components in the Broadleaf Framework can benefit from creating and manipulating temporary files as well as storing and accessing files in a remote repository (such as AmazonS3). 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
protected  FileServiceProvider defaultFileServiceProvider
           
protected  String fileServiceClasspathDirectory
           
protected  List<FileServiceProvider> fileServiceProviders
           
protected  int maxGeneratedDirectoryDepth
           
protected  String tempFileSystemBaseDirectory
           
 
Constructor Summary
BroadleafFileServiceImpl()
           
 
Method Summary
protected  String addLeadingSlash(String fileName)
           
 void addOrUpdateResource(FileWorkArea workArea, File file, boolean removeFilesFromWorkArea)
          Takes in a work area and a fileName.
 void addOrUpdateResources(FileWorkArea workArea, boolean removeFilesFromWorkArea)
          Takes in a work area and application type and moves all of the files to the configured FileProvider.
 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.
protected  void buildFileList(File file, Collection<File> fileList)
          Adds the file to the passed in Collection.
protected  String buildFilePath(String directory, String fileName)
          Builds a file path that ensures the directory and filename are separated by a single separator.
protected  void checkFiles(FileWorkArea workArea, List<File> fileList)
           
 boolean checkForResourceOnClassPath(String name)
          Returns true if the resource is available on the classpath.
 void closeWorkArea(FileWorkArea fwArea)
          Closes the passed in work area.
protected  StringBuilder 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.
 InputStream getClasspathResource(String name)
          Allows assets to be included in the Java classpath.
 FileServiceProvider getDefaultFileServiceProvider()
           
 List<FileServiceProvider> getFileServiceProviders()
           
 File getLocalResource(String resourceName)
          Checks for a resource in the temporary directory of the file-system.
protected  File getLocalResource(String resourceName, boolean skipSite)
           
 int getMaxGeneratedDirectoryDepth()
           
 File getResource(String name)
          Returns a File representing the passed in name.
 File getResource(String name, FileApplicationType applicationType)
          Returns a File representing the passed in name and application type.
 File getSharedLocalResource(String resourceName)
          Checks for a resource in the temporary directory of the file-system.
protected  String getTempDirectory(StringBuilder baseDirectory)
          Returns a directory that is unique for this work area.
 String getTempFileSystemBaseDirectory()
           
 FileWorkArea initializeWorkArea()
          Create a file work area that can be used for further operations.
protected  org.springframework.core.io.ClassPathResource lookupResourceOnClassPath(String name)
           
protected  String removeLeadingSlash(String fileName)
           
 boolean removeResource(String resourceName)
          Removes the resource matching the passed in file name from the FileProvider
protected  FileServiceProvider selectFileServiceProvider()
          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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fileServiceProviders

protected List<FileServiceProvider> fileServiceProviders

defaultFileServiceProvider

protected FileServiceProvider defaultFileServiceProvider

tempFileSystemBaseDirectory

@Value(value="${file.service.temp.file.base.directory}")
protected String tempFileSystemBaseDirectory

maxGeneratedDirectoryDepth

@Value(value="${asset.server.max.generated.file.system.directories}")
protected int maxGeneratedDirectoryDepth

fileServiceClasspathDirectory

@Value(value="${asset.server.file.classpath.directory}")
protected String fileServiceClasspathDirectory
Constructor Detail

BroadleafFileServiceImpl

public BroadleafFileServiceImpl()
Method Detail

initializeWorkArea

public FileWorkArea initializeWorkArea()
Create a file work area that can be used for further operations.

Specified by:
initializeWorkArea in interface BroadleafFileService
Returns:

closeWorkArea

public void closeWorkArea(FileWorkArea fwArea)
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 interface BroadleafFileService
Parameters:
Work - Area

getResource

public File getResource(String name)
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 as opposed to the BroadleafFileService.getResource(String, FileApplicationType) method which may access a copy of the file that is stored locally.

Specified by:
getResource in interface BroadleafFileService
Parameters:
name - - fully qualified path to the resource
Returns:

getLocalResource

protected File getLocalResource(String resourceName,
                                boolean skipSite)

getLocalResource

public File getLocalResource(String resourceName)
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 interface BroadleafFileService
Returns:

getSharedLocalResource

public File getSharedLocalResource(String resourceName)
Description copied from interface: BroadleafFileService
Checks for a resource in the temporary directory of the file-system. Will check for a global (e.g. not site specific file).

Specified by:
getSharedLocalResource in interface BroadleafFileService
Returns:

getResource

public File getResource(String name,
                        FileApplicationType applicationType)
Description copied from interface: BroadleafFileService
Returns a File representing the passed in name and application type. The application type provides an opportunity for the provider to cache the file locally for infrequently change files.

Specified by:
getResource in interface BroadleafFileService
Parameters:
name - - fully qualified path to the resource
applicationType - - The type of file being accessed
Returns:

buildFilePath

protected String buildFilePath(String directory,
                               String fileName)
Builds a file path that ensures the directory and filename are separated by a single separator.

Parameters:
directory -
fileName -
Returns:

checkForResourceOnClassPath

public boolean checkForResourceOnClassPath(String name)
Description copied from interface: BroadleafFileService
Returns true if the resource is available on the classpath.

Specified by:
checkForResourceOnClassPath in interface BroadleafFileService
Returns:

lookupResourceOnClassPath

protected org.springframework.core.io.ClassPathResource lookupResourceOnClassPath(String name)

getClasspathResource

public InputStream getClasspathResource(String name)
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 interface BroadleafFileService
Parameters:
name - - fully qualified path to the resource
Returns:

removeResource

public boolean removeResource(String resourceName)
Removes the resource matching the passed in file name from the FileProvider

Specified by:
removeResource in interface BroadleafFileService
Parameters:
resourceName - - fully qualified path to the resource

addOrUpdateResource

public void addOrUpdateResource(FileWorkArea workArea,
                                File file,
                                boolean removeFilesFromWorkArea)
Takes in a work area and a fileName. Loads the file onto the provider. 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 interface BroadleafFileService
Parameters:
workArea -
applicationType -
fileNames -
removeFilesFromWorkArea -

addOrUpdateResources

public void addOrUpdateResources(FileWorkArea workArea,
                                 boolean removeFilesFromWorkArea)
Takes in a work area and application type and moves all of the files to the configured FileProvider.

Specified by:
addOrUpdateResources in interface BroadleafFileService
Parameters:
workArea -
applicationType -

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 interface BroadleafFileService

selectFileServiceProvider

protected FileServiceProvider selectFileServiceProvider()
Returns the FileServiceProvider that can handle the passed in application type. By default, this method returns the component configured at blFileServiceProvider

Parameters:
applicationType -
Returns:

checkFiles

protected void checkFiles(FileWorkArea workArea,
                          List<File> fileList)

removeLeadingSlash

protected String removeLeadingSlash(String fileName)

addLeadingSlash

protected String addLeadingSlash(String fileName)

getBaseDirectory

protected StringBuilder 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. This method appends a trailing slash to the directory if it does not already have one.


getTempDirectory

protected String getTempDirectory(StringBuilder baseDirectory)
Returns a directory that is unique for this work area.


buildFileList

protected void buildFileList(File file,
                             Collection<File> fileList)
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

public String getTempFileSystemBaseDirectory()

setTempFileSystemBaseDirectory

public void setTempFileSystemBaseDirectory(String tempFileSystemBaseDirectory)

getFileServiceProviders

public List<FileServiceProvider> getFileServiceProviders()

setFileServiceProviders

public void setFileServiceProviders(List<FileServiceProvider> fileServiceProviders)

getMaxGeneratedDirectoryDepth

public int getMaxGeneratedDirectoryDepth()

setMaxGeneratedDirectoryDepth

public void setMaxGeneratedDirectoryDepth(int maxGeneratedDirectoryDepth)

getDefaultFileServiceProvider

public FileServiceProvider getDefaultFileServiceProvider()

setDefaultFileServiceProvider

public void setDefaultFileServiceProvider(FileServiceProvider defaultFileServiceProvider)


Copyright © 2013. All Rights Reserved.