Package gov.usgs.earthquake.distribution
Class FileProductStorage
- java.lang.Object
-
- gov.usgs.util.DefaultConfigurable
-
- gov.usgs.earthquake.distribution.FileProductStorage
-
- All Implemented Interfaces:
ProductStorage
,Configurable
- Direct Known Subclasses:
DYFILegacyStorage
,HashFileProductStorage
,URLProductStorage
public class FileProductStorage extends DefaultConfigurable implements ProductStorage
Store products in the file system. This implementation of ProductStorage extracts products into directories. The FileProductStorage implements the Configurable interface and can use the following configuration parameters:- directory
- (Optional, default = storage) The base directory where products are stored. Each product is stored in a separate directory within this directory.
- verifySignatures
- (Optional, default = off) Whether or not to verify signatures:
- off
- no verification
- test
- test but accept invalid signatures
- anything else
- reject invalid signatures.
- keychain
- (Optional) List of key section names to load for signature verification.
String getProductPath(ProductId) ProductSource getProductSourceFormat(File) ProductOutput getProductHandlerFormat(File)
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_DIRECTORY
Default storage path if none is provided.static int
DIRECTORY_NAME_LENGTH
This is chosen because 16^3 = 4096 < 32000, which is the ext3 subdirectory limit.static java.lang.String
DIRECTORY_PROPERTY_NAME
Storage path property name used by Configurable interface.static java.lang.String
LEGACY_STORAGES_PROPERTY
Property for legacyStorages.static java.lang.String
STORAGE_LISTENER_PROPERTY
Property for configured listenersstatic boolean
USE_HASH_PATHS_DEFAULT
Do not use hashes (Default).static java.lang.String
USE_HASH_PATHS_PROPERTY
Property for whether or not to hash file paths.
-
Constructor Summary
Constructors Constructor Description FileProductStorage()
Create a new FileProductStorage using the default storage path.FileProductStorage(java.io.File baseDirectory)
Create a new FileProductStorage.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addStorageListener(StorageListener listener)
Adds aStorageListener
to be notified when a change occurs in thisProductStorage
.void
configure(Config config)
Configure this object.java.io.File
getBaseDirectory()
protected java.lang.String
getHashedProductPath(ProductId id)
Product
getInMemoryProduct(ProductId id)
Get a product from storage, loading all file contents into memory.ProductKeyChain
getKeychain()
java.util.List<ProductStorage>
getLegacyStorages()
java.lang.String
getNormalProductPath(ProductId id)
Product
getProduct(ProductId id)
Get a product from storage.java.io.File
getProductFile(ProductId id)
Get the file or directory used to store a specific product.protected ProductHandler
getProductHandlerFormat(java.io.File file)
A method for subclasses to override the storage format.java.lang.String
getProductPath(ProductId id)
A method for subclasses to override the storage path.ProductSource
getProductSource(ProductId id)
Get a ProductSource from storage.protected ProductSource
getProductSourceFormat(java.io.File file)
A method for subclasses to override the storage format.ObjectLock<ProductId>
getStorageLocks()
boolean
hasProduct(ProductId id)
Check whether a product exists in storage.boolean
isRejectInvalidSignatures()
boolean
isTestSignatures()
void
notifyListeners(StorageEvent event)
NotifiesStorageListener
s of the change to theProductStorage
.void
removeProduct(ProductId id)
Remove a product from storage.void
removeStorageListener(StorageListener listener)
Removes aStorageListener
from being notified when a change occurs in thisProductStorage
.void
setBaseDirectory(java.io.File baseDirectory)
void
setKeychain(ProductKeyChain keychain)
void
setRejectInvalidSignatures(boolean rejectInvalidSignatures)
void
setTestSignatures(boolean testSignatures)
void
shutdown()
Called at client shutdown to free resources.void
startup()
Called after client configuration to begin processing.ProductId
storeProduct(Product product)
Store a product in storage.ProductId
storeProductSource(ProductSource source)
Store a ProductSource to storage.-
Methods inherited from class gov.usgs.util.DefaultConfigurable
getName, setName
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gov.usgs.util.Configurable
getName, setName
-
-
-
-
Field Detail
-
STORAGE_LISTENER_PROPERTY
public static final java.lang.String STORAGE_LISTENER_PROPERTY
Property for configured listeners- See Also:
- Constant Field Values
-
DIRECTORY_PROPERTY_NAME
public static final java.lang.String DIRECTORY_PROPERTY_NAME
Storage path property name used by Configurable interface.- See Also:
- Constant Field Values
-
DEFAULT_DIRECTORY
public static final java.lang.String DEFAULT_DIRECTORY
Default storage path if none is provided.- See Also:
- Constant Field Values
-
USE_HASH_PATHS_PROPERTY
public static final java.lang.String USE_HASH_PATHS_PROPERTY
Property for whether or not to hash file paths.- See Also:
- Constant Field Values
-
USE_HASH_PATHS_DEFAULT
public static final boolean USE_HASH_PATHS_DEFAULT
Do not use hashes (Default).- See Also:
- Constant Field Values
-
LEGACY_STORAGES_PROPERTY
public static final java.lang.String LEGACY_STORAGES_PROPERTY
Property for legacyStorages.- See Also:
- Constant Field Values
-
DIRECTORY_NAME_LENGTH
public static final int DIRECTORY_NAME_LENGTH
This is chosen because 16^3 = 4096 < 32000, which is the ext3 subdirectory limit.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FileProductStorage
public FileProductStorage()
Create a new FileProductStorage using the default storage path.
-
FileProductStorage
public FileProductStorage(java.io.File baseDirectory)
Create a new FileProductStorage.- Parameters:
baseDirectory
- the base directory for all products being stored.
-
-
Method Detail
-
getStorageLocks
public ObjectLock<ProductId> getStorageLocks()
- Returns:
- the storageLocks
-
configure
public void configure(Config config) throws java.lang.Exception
Configure this object. Expects a key named "directory".- Specified by:
configure
in interfaceConfigurable
- Overrides:
configure
in classDefaultConfigurable
- Parameters:
config
- the Config object with settings.- Throws:
java.lang.Exception
- if configuration exceptions occur.
-
notifyListeners
public void notifyListeners(StorageEvent event)
Description copied from interface:ProductStorage
NotifiesStorageListener
s of the change to theProductStorage
.- Specified by:
notifyListeners
in interfaceProductStorage
- Parameters:
event
- StorageEvent
-
addStorageListener
public void addStorageListener(StorageListener listener)
Description copied from interface:ProductStorage
Adds aStorageListener
to be notified when a change occurs in thisProductStorage
.- Specified by:
addStorageListener
in interfaceProductStorage
- Parameters:
listener
- The listener to notify of changes.
-
removeStorageListener
public void removeStorageListener(StorageListener listener)
Description copied from interface:ProductStorage
Removes aStorageListener
from being notified when a change occurs in thisProductStorage
.- Specified by:
removeStorageListener
in interfaceProductStorage
- Parameters:
listener
- The listener to remove
-
getProductPath
public java.lang.String getProductPath(ProductId id)
A method for subclasses to override the storage path. The returned path is appended to the base directory when storing and retrieving products.- Parameters:
id
- the product id to convert.- Returns:
- the directory used to store id.
-
getHashedProductPath
protected java.lang.String getHashedProductPath(ProductId id)
- Parameters:
id
- Specific productID- Returns:
- string buffer of hashed product path
-
getNormalProductPath
public java.lang.String getNormalProductPath(ProductId id)
- Parameters:
id
- ProductId- Returns:
- string buffer of normal product path
-
getProductHandlerFormat
protected ProductHandler getProductHandlerFormat(java.io.File file) throws java.lang.Exception
A method for subclasses to override the storage format. When overriding this method, the method getProductSourceFormat should also be overridden.- Parameters:
file
- a file that should be converted into a ProductHandler.- Returns:
- the ProductHandler.
- Throws:
java.lang.Exception
- if error occurs
-
getProductSourceFormat
protected ProductSource getProductSourceFormat(java.io.File file) throws java.lang.Exception
A method for subclasses to override the storage format. When overriding this method, the method getProductHandlerFormat should also be overridden.- Parameters:
file
- a file that should be converted into a ProductSource.- Returns:
- the ProductSource.
- Throws:
java.lang.Exception
- if error occurs
-
getProductFile
public java.io.File getProductFile(ProductId id)
Get the file or directory used to store a specific product.- Parameters:
id
- which product.- Returns:
- a file or directory where the product would be stored.
-
getProduct
public Product getProduct(ProductId id) throws java.lang.Exception
Get a product from storage. Calls the getProductSource method, and uses ObjectProductHandler to convert the ProductSource into a Product.- Specified by:
getProduct
in interfaceProductStorage
- Parameters:
id
- the product to retrieve.- Returns:
- the product, or null if not in this storage.
- Throws:
java.lang.Exception
- if errors occur while retrieving product.
-
getInMemoryProduct
public Product getInMemoryProduct(ProductId id) throws java.lang.Exception
Get a product from storage, loading all file contents into memory. This method may cause memory problems if product contents are large.- Parameters:
id
- the product to retrieve.- Returns:
- the loaded product.
- Throws:
java.lang.Exception
- if error occurs
-
getProductSource
public ProductSource getProductSource(ProductId id) throws java.lang.Exception
Get a ProductSource from storage.- Specified by:
getProductSource
in interfaceProductStorage
- Parameters:
id
- the product to retrieve.- Returns:
- a ProductSource for the product, or null if not in this storage.
- Throws:
java.lang.Exception
- if any errors occur while getting the ProductInput.
-
hasProduct
public boolean hasProduct(ProductId id) throws java.lang.Exception
Check whether a product exists in storage.- Specified by:
hasProduct
in interfaceProductStorage
- Parameters:
id
- the product to check.- Returns:
- true if the product exists, false otherwise.
- Throws:
java.lang.Exception
- if an error occurs while checking.
-
removeProduct
public void removeProduct(ProductId id) throws java.lang.Exception
Remove a product from storage.- Specified by:
removeProduct
in interfaceProductStorage
- Parameters:
id
- product to remove.- Throws:
java.lang.Exception
- if errors occur while removing product.
-
storeProduct
public ProductId storeProduct(Product product) throws java.lang.Exception
Store a product in storage. Same as storeProductSource(new ObjectProductSource(product)).- Specified by:
storeProduct
in interfaceProductStorage
- Parameters:
product
- the product to store.- Returns:
- the id of the stored product.
- Throws:
java.lang.Exception
- if errors occur while storing product.
-
storeProductSource
public ProductId storeProductSource(ProductSource source) throws java.lang.Exception
Store a ProductSource to storage. If any exceptions occur while storing a product (other than the product already existing in storage) the incompletely stored product is removed.- Specified by:
storeProductSource
in interfaceProductStorage
- Parameters:
source
- the ProductSource to store.- Returns:
- the id of the stored product.
- Throws:
java.lang.Exception
- if errors occur while storing product.
-
shutdown
public void shutdown() throws java.lang.Exception
Called at client shutdown to free resources.- Specified by:
shutdown
in interfaceConfigurable
- Overrides:
shutdown
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if exceptions occur while starting.
-
startup
public void startup() throws java.lang.Exception
Called after client configuration to begin processing.- Specified by:
startup
in interfaceConfigurable
- Overrides:
startup
in classDefaultConfigurable
- Throws:
java.lang.Exception
- if exceptions occur while starting.
-
getBaseDirectory
public java.io.File getBaseDirectory()
- Returns:
- the baseDirectory
-
setBaseDirectory
public void setBaseDirectory(java.io.File baseDirectory)
- Parameters:
baseDirectory
- the baseDirectory to set
-
isRejectInvalidSignatures
public boolean isRejectInvalidSignatures()
- Returns:
- the rejectInvalidSignatures
-
setRejectInvalidSignatures
public void setRejectInvalidSignatures(boolean rejectInvalidSignatures)
- Parameters:
rejectInvalidSignatures
- the rejectInvalidSignatures to set
-
isTestSignatures
public boolean isTestSignatures()
- Returns:
- the testSignatures
-
setTestSignatures
public void setTestSignatures(boolean testSignatures)
- Parameters:
testSignatures
- the testSignatures to set
-
getKeychain
public ProductKeyChain getKeychain()
- Returns:
- the keychain
-
setKeychain
public void setKeychain(ProductKeyChain keychain)
- Parameters:
keychain
- the keychain to set
-
getLegacyStorages
public java.util.List<ProductStorage> getLegacyStorages()
- Returns:
- the legacyStorages.
-
-