|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.osgi.storagemanager.StorageManager
public final class StorageManager
Storage managers provide a facility for tracking the state of a group of files having relationship with each others and being updated by several entities at the same time. The typical usecase is in shared configuration data areas.
The facilities provided here are cooperative. That is, all participants must agree to the conventions and to calling the given API. There is no capacity to enforce these conventions or prohibit corruption.
Clients can not extend this class
Example
//Open the storage manager org.eclipse.osgi.storagemanager.StorageManager cacheStorageManager = new StorageManager("d:/sharedFolder/bar/", false); //$NON-NLS-1$ try { cacheStorageManager.open(true); } catch (IOException e) { // Ignore the exception. The registry will be rebuilt from source. } //To read from a file java.io.File fileA = cacheStorageManager.lookup("fileA", false)); java.io.File fileB = cacheStorageManager.lookup("fileB", false)); //Do the reading code new java.io.FileOutputStream(fileA); //To write in files cacheStorageManager.add("fileC"); //add the file to the filemanager (in this case we assume it is not already here) cacheStorageManager.add("fileD"); // The file is never written directly into the file name, so we create some temporary file java.io.File fileC = cacheStorageManager.createTempFile("fileC"); java.io.File fileD = cacheStorageManager.createTempFile("fileD"); //Do the actual writing here... //Finally update the storagemanager with the actual file to manage. cacheStorageManager.update(new String[] {"fileC", "fileD"}, new String[] {fileC.getName(), fileD.getName()}; //Close the file manager at the end cacheStorageManager.close();
Implementation details
The following implementation details are provided to help with understanding the
behavior of this class.
The general principle is to maintain a table which maps user-level file names
onto an actual disk files. If a file needs to be modified,
it is stored into a new file. The old content is not removed from disk until all entities
have closed there instance of the storage manager.
Once the instance has been created, open() must be called before performing any other operation.
On open the storage manager obtains a snapshot of the current managed files contents. If an
entity updates a managed file, the storage manager will save the content for that instance of the
storage manager, all other storage manager instances will still have access to that managed file's
content as it was when the instance was first opened.
Constructor Summary | |
---|---|
StorageManager(java.io.File base,
java.lang.String lockMode)
Returns a new storage manager for the area identified by the given base directory. |
|
StorageManager(java.io.File base,
java.lang.String lockMode,
boolean readOnly)
Returns a new storage manager for the area identified by the given base directory. |
Method Summary | |
---|---|
void |
add(java.lang.String managedFile)
Add the given managed file name to the list of files managed by this manager. |
void |
close()
This method declares the storage manager as closed. |
java.io.File |
createTempFile(java.lang.String file)
Creates a new unique empty temporary-file in the storage manager base directory. |
java.io.File |
getBase()
Returns the directory containing the files being managed by this storage manager. |
int |
getId(java.lang.String managedFile)
Returns the current numeric id (appendage) of the given managed file. |
java.io.InputStream |
getInputStream(java.lang.String managedFile)
Returns a managed InputStream for a managed file. |
java.io.InputStream[] |
getInputStreamSet(java.lang.String[] managedFiles)
Returns a managed input stream set for the managed file names. |
java.lang.String[] |
getManagedFiles()
Returns a list of all the managed files currently being managed. |
ManagedOutputStream |
getOutputStream(java.lang.String managedFile)
Returns a ManagedOutputStream for a managed file. |
ManagedOutputStream[] |
getOutputStreamSet(java.lang.String[] managedFiles)
Returns an array of ManagedOutputStream for a set of managed files. |
boolean |
isReadOnly()
Returns if readOnly state this storage manager is using. |
java.io.File |
lookup(java.lang.String managedFile,
boolean add)
Returns the actual file location to use when reading the given managed file. |
void |
open(boolean wait)
This methods opens the storage manager. |
void |
remove(java.lang.String managedFile)
Removes the given managed file from management by this storage manager. |
void |
update(java.lang.String[] managedFiles,
java.lang.String[] sources)
Update the given managed files with the content in the given source files. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public StorageManager(java.io.File base, java.lang.String lockMode)
base
- the directory holding the files to be managedlockMode
- the lockMode to use for the storage manager. It can have one the 3 values: none, java.io, java.nio
and also supports null in which case the lock strategy will be the global one.public StorageManager(java.io.File base, java.lang.String lockMode, boolean readOnly)
base
- the directory holding the files to be managedlockMode
- the lockMode to use for the storage manager. It can have one the 3 values: none, java.io, java.nio
and also supports null in which case the lock strategy will be the global one.readOnly
- true if the managed files are read-onlyMethod Detail |
---|
public void add(java.lang.String managedFile) throws java.io.IOException
managedFile
- name of the file to manage
java.io.IOException
- if there are any problems adding the given file name to the managerpublic void update(java.lang.String[] managedFiles, java.lang.String[] sources) throws java.io.IOException
managedFiles
- the managed files to updatesources
- the new content for the managed files
java.io.IOException
- if there are any problems updating the given managed filespublic java.lang.String[] getManagedFiles()
public java.io.File getBase()
public int getId(java.lang.String managedFile)
managedFile + "." + getId(target)
. A value of -1 is returned
if the given name is not managed.
managedFile
- the name of the managed file
public boolean isReadOnly()
public java.io.File lookup(java.lang.String managedFile, boolean add) throws java.io.IOException
null
can be returned if the given managed file name is not
managed and add is set to false.
The returned file should be considered read-only. Any updates to the content of this
file should be done using update(String[], String[])
.
managedFile
- the managed file to lookupadd
- indicate whether the managed file name should be added to the manager if
it is not already managed.
null
if the given managed file is not managed
java.io.IOException
- if the add flag is set to true and the addition of the managed file failedpublic void remove(java.lang.String managedFile) throws java.io.IOException
managedFile
- the managed file to remove
java.io.IOException
- if an error occured removing the managed filepublic void close()
public void open(boolean wait) throws java.io.IOException
wait
- indicates if the open operation must wait in case of contention on the lock file.
java.io.IOException
- if an error occured opening the storage managerpublic java.io.File createTempFile(java.lang.String file) throws java.io.IOException
file
- the file name to create temporary file from.
java.io.IOException
- if the file can not be created.update(String[], String[])
public java.io.InputStream getInputStream(java.lang.String managedFile) throws java.io.IOException
InputStream
for a managed file.
null
can be returned if the given name is not managed.
managedFile
- the name of the managed file to open.
null
if the given name is not managed.
java.io.IOException
- if the content is missing, corrupt or an error occurs.public java.io.InputStream[] getInputStreamSet(java.lang.String[] managedFiles) throws java.io.IOException
null
if a given name is not managed.
This method should be used for managed file sets which use the output streams returned
by the getOutputStreamSet(String[])
to save data.
managedFiles
- the names of the managed files to open.
java.io.IOException
- if the content of one of the managed files is missing, corrupt or an error occurs.public ManagedOutputStream getOutputStream(java.lang.String managedFile) throws java.io.IOException
ManagedOutputStream
for a managed file.
Closing the ouput stream will update the storage manager with the
new content of the managed file.
managedFile
- the name of the managed file to write.
java.io.IOException
- if an error occurs opening the managed file.public ManagedOutputStream[] getOutputStreamSet(java.lang.String[] managedFiles) throws java.io.IOException
ManagedOutputStream
for a set of managed files.
When all managed output streams in the set have been closed, the storage manager
will be updated with the new content of the managed files.
Aborting any one of the streams will cause the entire content of the set to abort
and be discarded.
managedFiles
- list of names of the managed file to write.
java.io.IOException
- if an error occurs opening the managed files.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |