org.eclipse.osgi.storagemanager
Class StorageManager

java.lang.Object
  extended by org.eclipse.osgi.storagemanager.StorageManager

public final class StorageManager
extends java.lang.Object

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.

Since:
3.2

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

StorageManager

public StorageManager(java.io.File base,
                      java.lang.String lockMode)
Returns a new storage manager for the area identified by the given base directory.

Parameters:
base - the directory holding the files to be managed
lockMode - 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.

StorageManager

public 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.

Parameters:
base - the directory holding the files to be managed
lockMode - 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-only
Method Detail

add

public void add(java.lang.String managedFile)
         throws java.io.IOException
Add the given managed file name to the list of files managed by this manager.

Parameters:
managedFile - name of the file to manage
Throws:
java.io.IOException - if there are any problems adding the given file name to the manager

update

public void update(java.lang.String[] managedFiles,
                   java.lang.String[] sources)
            throws java.io.IOException
Update the given managed files with the content in the given source files. The managedFiles is a list of managed file names which are currently managed. If a managed file name is not currently managed it will be added as a managed file for this storage manager. The sources are absolute (or relative to the current working directory) file paths containing the new content for the corresponding managed files.

Parameters:
managedFiles - the managed files to update
sources - the new content for the managed files
Throws:
java.io.IOException - if there are any problems updating the given managed files

getManagedFiles

public java.lang.String[] getManagedFiles()
Returns a list of all the managed files currently being managed.

Returns:
the names of the managed files

getBase

public java.io.File getBase()
Returns the directory containing the files being managed by this storage manager.

Returns:
the directory containing the managed files

getId

public int getId(java.lang.String managedFile)
Returns the current numeric id (appendage) of the given managed file. managedFile + "." + getId(target). A value of -1 is returned if the given name is not managed.

Parameters:
managedFile - the name of the managed file
Returns:
the id of the managed file

isReadOnly

public boolean isReadOnly()
Returns if readOnly state this storage manager is using.

Returns:
if this storage manager update state is read-only.

lookup

public java.io.File lookup(java.lang.String managedFile,
                           boolean add)
                    throws java.io.IOException
Returns the actual file location to use when reading the given managed file. A value of 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[]).

Parameters:
managedFile - the managed file to lookup
add - indicate whether the managed file name should be added to the manager if it is not already managed.
Returns:
the absolute file location to use for the given managed file or null if the given managed file is not managed
Throws:
java.io.IOException - if the add flag is set to true and the addition of the managed file failed

remove

public void remove(java.lang.String managedFile)
            throws java.io.IOException
Removes the given managed file from management by this storage manager.

Parameters:
managedFile - the managed file to remove
Throws:
java.io.IOException - if an error occured removing the managed file

close

public void close()
This method declares the storage manager as closed. From thereon, the instance can no longer be used. It is important to close the manager as it also cleans up old copies of the managed files.


open

public void open(boolean wait)
          throws java.io.IOException
This methods opens the storage manager. This method must be called before any operation on the storage manager.

Parameters:
wait - indicates if the open operation must wait in case of contention on the lock file.
Throws:
java.io.IOException - if an error occured opening the storage manager

createTempFile

public java.io.File createTempFile(java.lang.String file)
                            throws java.io.IOException
Creates a new unique empty temporary-file in the storage manager base directory. The file name must be at least 3 characters. This file can later be used to update a managed file.

Parameters:
file - the file name to create temporary file from.
Returns:
the newly-created empty file.
Throws:
java.io.IOException - if the file can not be created.
See Also:
update(String[], String[])

getInputStream

public java.io.InputStream getInputStream(java.lang.String managedFile)
                                   throws java.io.IOException
Returns a managed InputStream for a managed file. null can be returned if the given name is not managed.

Parameters:
managedFile - the name of the managed file to open.
Returns:
an input stream to the managed file or null if the given name is not managed.
Throws:
java.io.IOException - if the content is missing, corrupt or an error occurs.

getInputStreamSet

public java.io.InputStream[] getInputStreamSet(java.lang.String[] managedFiles)
                                        throws java.io.IOException
Returns a managed input stream set for the managed file names. Elements of the returned set may be 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.

Parameters:
managedFiles - the names of the managed files to open.
Returns:
a set input streams to the given managed files.
Throws:
java.io.IOException - if the content of one of the managed files is missing, corrupt or an error occurs.

getOutputStream

public ManagedOutputStream getOutputStream(java.lang.String managedFile)
                                    throws java.io.IOException
Returns a ManagedOutputStream for a managed file. Closing the ouput stream will update the storage manager with the new content of the managed file.

Parameters:
managedFile - the name of the managed file to write.
Returns:
a managed output stream for the managed file.
Throws:
java.io.IOException - if an error occurs opening the managed file.

getOutputStreamSet

public ManagedOutputStream[] getOutputStreamSet(java.lang.String[] managedFiles)
                                         throws java.io.IOException
Returns an array of 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.

Parameters:
managedFiles - list of names of the managed file to write.
Returns:
an array of managed output streams respectively of managed files.
Throws:
java.io.IOException - if an error occurs opening the managed files.