Package edu.uiuc.ncsa.security.storage
Class FileStore<V extends Identifiable>
- java.lang.Object
-
- edu.uiuc.ncsa.security.storage.IndexedStreamStore<V>
-
- edu.uiuc.ncsa.security.storage.FileStore<V>
-
- All Implemented Interfaces:
Store<V>
,Map<Identifier,V>
- Direct Known Subclasses:
FSClientStore
,MonitoredFileStore
public abstract class FileStore<V extends Identifiable> extends IndexedStreamStore<V>
A store backed by the file system. This works with all implementations since it just serializes whatever item (which must be of typeIdentifiable
) it has.How's it work?
There are two directories, one, the storage directory, contains the serialized items themselves. The file names are hashes of the unique identifiers. The other directory, the index directory, contains files whose names are hashes of the other information (e.g. temporary credentials, client identifier, verifier, access token,...). You will have to override to get the right index entries. (See below) Each of these index files contains a single line which is the file name in the storage directory. So a request to get by the temporary credential will hash the credential, grab the index file, read the name of the actual transaction file and load that.This does no caching of any sort. If you want caching (strongly suggested) create a transaction cache and set its backing cache to be an instance of this.
Usage
To use this, you need to override a couple of methods:
create()
to return whatever V really is.realSave(boolean, edu.uiuc.ncsa.security.core.Identifiable)
Optional if you need some other processing before or after the save. In the case, e.g., of transactions, you want to save them by access token and verifier too, e.g. by invokingcreateIndexEntry(String, String)
of the real identifier and the other one. Retrieval of these is done with the methodgetIndexEntry(String)
.realRemove(edu.uiuc.ncsa.security.core.Identifiable)
After calling super on the object, remove all index entries withremoveIndexEntry(String)
.
Created by Jeff Gaynor
on 11/3/11 at 1:54 PM
-
-
Field Summary
Fields Modifier and Type Field Description protected File
indexDirectory
protected File
storageDirectory
-
Fields inherited from class edu.uiuc.ncsa.security.storage.IndexedStreamStore
converter, identifiableProvider, initializer
-
Fields inherited from interface edu.uiuc.ncsa.security.core.Store
VERSION_TAG
-
-
Constructor Summary
Constructors Modifier Constructor Description FileStore(File directory, IdentifiableProvider<V> idp, MapConverter<V> cp, boolean removeEmptyFiles, boolean removeFailedFiles)
Accepts a directory for both the index and data and creates the subdirectories.protected
FileStore(File storeDirectory, File indexDirectory, IdentifiableProvider<V> identifiableProvider, MapConverter<V> converter, boolean removeEmptyFiles, boolean removeFailedFiles)
For the case where the data and index directories are explicitly given.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkPermissions()
Since administrators can and have inadvertently changed directory or file permissions while the server is running, here is a method to check if the directory has acceptable permissions.void
clear()
Required by the map interfaceboolean
containsKey(Object key)
boolean
containsValue(Object value)
V
create()
Create a new object of the given type.protected void
createIndexEntry(String otherKey, String identifier)
Add an index entry for an item that is not the unique identifier.boolean
delete(String identifier)
protected void
doSetup(File storeDirectory, File indexDirectory, IdentifiableProvider<V> identifiableProvider, MapConverter<V> converter, boolean removeEmptyFiles, boolean removeFailedFiles)
Set<Map.Entry<Identifier,V>>
entrySet()
V
get(Object key)
This updates the last accessed listenerList<V>
getAll()
Method to get every element in the store.File
getIndexDirectory()
protected V
getIndexEntry(String token)
Get a stored item using a key other than the identifier.protected File
getItemFile(String identifier)
protected File
getItemFile(V t)
MapConverter
getMapConverter()
File
getStorageDirectory()
XMLConverter<V>
getXMLConverter()
Set<Identifier>
keySet()
protected V
loadByIdentifier(String identifier)
protected V
loadFile(File f)
protected V
loadFromIndex(String hashedName)
Finds a file with the given index value.void
putAll(Map m)
protected V
realRemove(V oldItem)
Does the actual removal of the item from the store.void
realSave(boolean checkExists, V t)
Does the actual work of writing everything to the data directory.void
register(V t)
Almost Identical to put(K,V) but since the object should have an identifier, passing along the key is redundant.V
remove(Object key)
This is required by the map interface.boolean
remove(List<Identifiable> objects)
Terribly inefficient.protected boolean
removeIndexEntry(String token)
Remove an index entry (not the actual item!).void
save(V t)
Saves an object.List<V>
search(String key, String condition, boolean isRegEx)
Allows for searching via a reg ex.List<V>
search(String key, String condition, boolean isRegEx, List<String> attr)
Return a subset of all the attributes.List<V>
search(String key, String condition, boolean isRegEx, List<String> attr, String dateField, Date before, Date after)
void
setIndexDirectory(File indexDirectory)
void
setStorageDirectory(File storageDirectory)
int
size()
int
size(boolean includeVersions)
String
toString()
void
update(V t)
Update an existing object.Collection<V>
values()
Not an efficient way to get the values, but this will get them all.-
Methods inherited from class edu.uiuc.ncsa.security.storage.IndexedStreamStore
createIndexEntry, getCreatedItems, hashString, isEmpty, loadStream, put, put
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Methods inherited from interface edu.uiuc.ncsa.security.core.Store
getMostRecent
-
-
-
-
Constructor Detail
-
FileStore
protected FileStore(File storeDirectory, File indexDirectory, IdentifiableProvider<V> identifiableProvider, MapConverter<V> converter, boolean removeEmptyFiles, boolean removeFailedFiles)
For the case where the data and index directories are explicitly given.- Parameters:
storeDirectory
-indexDirectory
-identifiableProvider
-converter
-
-
FileStore
public FileStore(File directory, IdentifiableProvider<V> idp, MapConverter<V> cp, boolean removeEmptyFiles, boolean removeFailedFiles)
Accepts a directory for both the index and data and creates the subdirectories.- Parameters:
directory
-idp
-cp
-
-
-
Method Detail
-
checkPermissions
protected void checkPermissions()
Since administrators can and have inadvertently changed directory or file permissions while the server is running, here is a method to check if the directory has acceptable permissions. Each call to the store should invoke this as its first action.
-
doSetup
protected void doSetup(File storeDirectory, File indexDirectory, IdentifiableProvider<V> identifiableProvider, MapConverter<V> converter, boolean removeEmptyFiles, boolean removeFailedFiles)
-
getMapConverter
public MapConverter getMapConverter()
-
getIndexDirectory
public File getIndexDirectory()
-
setIndexDirectory
public void setIndexDirectory(File indexDirectory)
-
getStorageDirectory
public File getStorageDirectory()
-
setStorageDirectory
public void setStorageDirectory(File storageDirectory)
-
realSave
public void realSave(boolean checkExists, V t)
Does the actual work of writing everything to the data directory. Override this as needed and invokecreateIndexEntry(String, String)
to put and entry for the item into the index. When overriding, call this via super first or the item itself will not be saved.- Parameters:
checkExists
-t
-
-
createIndexEntry
protected void createIndexEntry(String otherKey, String identifier) throws IOException
Add an index entry for an item that is not the unique identifier.- Parameters:
otherKey
- the other key to indexidentifier
- the unique identifier for this item- Throws:
IOException
-
loadFromIndex
protected V loadFromIndex(String hashedName)
Finds a file with the given index value. This will look in the index directory for the file with the same name as the lookup, then read the contents of the lookup which is a hashed uri- Parameters:
hashedName
-- Returns:
- Throws:
IOException
-
update
public void update(V t)
Description copied from interface:Store
Update an existing object. AnUnregisteredObjectException
is thrown if the object has not been saved first.
-
register
public void register(V t)
Description copied from interface:Store
Almost Identical to put(K,V) but since the object should have an identifier, passing along the key is redundant. This persists the object in the store. Note that this returns void since the contract assumes that this is not registered. If the object is registered an exception should be thrown. Generally use save(V).
-
clear
public void clear()
Required by the map interface
-
save
public void save(V t)
Description copied from interface:Store
Saves an object. This bridges the gap between SQL stores update and insert commands. Implementations should check if the object already exists in the store and issue an appropriate call.
-
keySet
public Set<Identifier> keySet()
-
values
public Collection<V> values()
Not an efficient way to get the values, but this will get them all.- Returns:
-
entrySet
public Set<Map.Entry<Identifier,V>> entrySet()
-
size
public int size()
-
size
public int size(boolean includeVersions)
-
containsKey
public boolean containsKey(Object key)
-
containsValue
public boolean containsValue(Object value)
-
getAll
public List<V> getAll()
Description copied from interface:Store
Method to get every element in the store. This is useful for command line interfaces. Note that this might be very expensive.- Returns:
-
remove
public boolean remove(List<Identifiable> objects)
Terribly inefficient. Terribly. However, the assumption is that a file store is small rather than large. For larger numbers of entries, use a database.- Parameters:
objects
-- Returns:
-
delete
public boolean delete(String identifier)
-
realRemove
protected V realRemove(V oldItem)
Does the actual removal of the item from the store. Be sure to override this to remove any index entries if you need to.- Parameters:
oldItem
- The item (which is Identifiable) to be removed.- Returns:
-
remove
public V remove(Object key)
This is required by the map interface. The argument is really the identifier. This returns the transaction if there was one already associated with this identifier- Parameters:
key
-- Returns:
-
putAll
public void putAll(Map m)
-
removeIndexEntry
protected boolean removeIndexEntry(String token)
Remove an index entry (not the actual item!). To remove the item, useremove(Object)
.- Parameters:
token
-
-
getIndexEntry
protected V getIndexEntry(String token)
Get a stored item using a key other than the identifier.- Parameters:
token
-- Returns:
-
create
public V create()
Description copied from interface:Store
Create a new object of the given type. This is not in the store until it is registered. Attempts to update the object should throw an exception. Note that this allows for a separation of creation semantics. Some objects require specific initialization before saving- Specified by:
create
in interfaceStore<V extends Identifiable>
- Overrides:
create
in classIndexedStreamStore<V extends Identifiable>
- Returns:
-
getXMLConverter
public XMLConverter<V> getXMLConverter()
-
search
public List<V> search(String key, String condition, boolean isRegEx, List<String> attr)
Description copied from interface:Store
Return a subset of all the attributes. For non-SQL stores performance may be slow.- Returns:
-
search
public List<V> search(String key, String condition, boolean isRegEx, List<String> attr, String dateField, Date before, Date after)
-
-