org.apache.jackrabbit.core.fs.db
Class DatabaseFileSystem

java.lang.Object
  extended by org.apache.jackrabbit.core.fs.db.DatabaseFileSystem
All Implemented Interfaces:
FileSystem
Direct Known Subclasses:
DbFileSystem, JNDIDatabaseFileSystem

public class DatabaseFileSystem
extends Object
implements FileSystem

Base class for database file systems. This class contains common functionality for database file system subclasses that normally differ only in the way the database connection is acquired. Subclasses should override the getConnection() method to return the configured database connection.

See the DbFileSystem for a detailed description of the available configuration options and database behaviour.


Field Summary
protected  Connection con
           
protected  String copyFileSQL
           
protected  String copyFilesSQL
           
protected  String deleteFileSQL
           
protected  String deleteFolderSQL
           
protected static int INITIAL_BUFFER_SIZE
           
protected  boolean initialized
           
protected  String insertFileSQL
           
protected  String insertFolderSQL
           
protected  String schema
           
protected static String SCHEMA_OBJECT_PREFIX_VARIABLE
           
protected  String schemaObjectPrefix
           
protected  String selectChildCountSQL
           
protected  String selectDataSQL
           
protected  String selectExistSQL
           
protected  String selectFileAndFolderNamesSQL
           
protected  String selectFileExistSQL
           
protected  String selectFileNamesSQL
           
protected  String selectFolderExistSQL
           
protected  String selectFolderNamesSQL
           
protected  String selectLastModifiedSQL
           
protected  String selectLengthSQL
           
protected static int SLEEP_BEFORE_RECONNECT
           
protected  String updateDataSQL
           
protected  String updateLastModifiedSQL
           
 
Fields inherited from interface org.apache.jackrabbit.core.fs.FileSystem
SEPARATOR, SEPARATOR_CHAR
 
Constructor Summary
DatabaseFileSystem()
          Default constructor
 
Method Summary
protected  void buildSQLStatements()
          Builds the SQL statements
protected  void checkSchema()
          Checks if the required schema objects exist and creates them if they don't exist yet.
 void close()
          Close the file system.
protected  void closeConnection(Connection connection)
          Closes the given database connection.
protected  void closeResultSet(ResultSet rs)
           
protected  void closeStatement(Statement stmt)
           
 void copy(String srcPath, String destPath)
          Copies a file or folder to a new location.
protected  void copyDeepFolder(String srcPath, String destPath)
          Recursively copies the given folder to the given destination.
protected  void copyFile(String srcPath, String destPath)
          Copies the given file entry to the given destination path.
protected  void createDeepFolder(String folderPath)
          Creates the specified files system folder entry, recursively creating any non-existing intermediate folder entries.
 void createFolder(String folderPath)
          Creates the folder named by this path, including any necessary but nonexistent parent folders.
protected  String createSchemaSql(String sql)
          Replace wildcards.
 void deleteFile(String filePath)
          Deletes the file denoted by this path.
 void deleteFolder(String folderPath)
          Deletes the folder denoted by this path.
 boolean equals(Object obj)
          
protected  Statement executeStmt(String sql, Object[] params)
          Executes the given SQL statement with the specified parameters.
 boolean exists(String path)
          Tests whether the file system entry denoted by this path exists.
protected  Connection getConnection()
          Abstract factory method for creating a new database connection.
 InputStream getInputStream(String filePath)
          Returns an input stream of the contents of the file denoted by this path.
 OutputStream getOutputStream(String filePath)
          Returns an output stream for writing bytes to the file denoted by this path.
 RandomAccessOutputStream getRandomAccessOutputStream(String filePath)
          Returns an output stream for writing bytes to the file denoted by this path.
 String getSchema()
           
 String getSchemaObjectPrefix()
           
 boolean hasChildren(String path)
          Tests whether the file system entry denoted by this path has child entries.
 int hashCode()
          Returns zero to satisfy the Object equals/hashCode contract.
 void init()
          Initialize the file system
protected  void initConnection()
          Initializes the database connection used by this file system.
protected  void initPreparedStatements()
          Initializes the map of prepared statements.
 boolean isFile(String path)
          Tests whether the file system entry denoted by this path exists and is a file.
 boolean isFolder(String path)
          Tests whether the file system entry denoted by this path exists and is a folder.
 boolean isSchemaCheckEnabled()
           
 long lastModified(String path)
          Returns the time that the file system entry denoted by this path was last modified.
 long length(String filePath)
          Returns the length of the file denoted by this path.
 String[] list(String folderPath)
          Returns an array of strings naming the files and folders in the folder denoted by this path.
 String[] listFiles(String folderPath)
          Returns an array of strings naming the files in the folder denoted by this path.
 String[] listFolders(String folderPath)
          Returns an array of strings naming the folders in the folder denoted by this path.
 void move(String srcPath, String destPath)
          Moves a file or folder to a new location.
protected  void prepareSchemaObjectPrefix()
          Makes sure that schemaObjectPrefix does only consist of characters that are allowed in names on the target database.
protected  boolean reestablishConnection()
          Re-establishes the database connection.
protected  void resetStatement(PreparedStatement stmt)
          Resets the given PreparedStatement by clearing the parameters and warnings contained.
 void setSchema(String schema)
           
 void setSchemaCheckEnabled(boolean enabled)
           
 void setSchemaObjectPrefix(String schemaObjectPrefix)
           
 void touch(String filePath)
          Set the modified time of an existing file to now.
protected  void verifyRootExists()
          Verifies that the root file system entry exists.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCHEMA_OBJECT_PREFIX_VARIABLE

protected static final String SCHEMA_OBJECT_PREFIX_VARIABLE
See Also:
Constant Field Values

initialized

protected boolean initialized

schema

protected String schema

schemaObjectPrefix

protected String schemaObjectPrefix

INITIAL_BUFFER_SIZE

protected static final int INITIAL_BUFFER_SIZE
See Also:
Constant Field Values

con

protected Connection con

SLEEP_BEFORE_RECONNECT

protected static final int SLEEP_BEFORE_RECONNECT
See Also:
Constant Field Values

selectExistSQL

protected String selectExistSQL

selectFileExistSQL

protected String selectFileExistSQL

selectFolderExistSQL

protected String selectFolderExistSQL

selectChildCountSQL

protected String selectChildCountSQL

selectDataSQL

protected String selectDataSQL

selectLastModifiedSQL

protected String selectLastModifiedSQL

selectLengthSQL

protected String selectLengthSQL

selectFileNamesSQL

protected String selectFileNamesSQL

selectFolderNamesSQL

protected String selectFolderNamesSQL

selectFileAndFolderNamesSQL

protected String selectFileAndFolderNamesSQL

deleteFileSQL

protected String deleteFileSQL

deleteFolderSQL

protected String deleteFolderSQL

insertFileSQL

protected String insertFileSQL

insertFolderSQL

protected String insertFolderSQL

updateDataSQL

protected String updateDataSQL

updateLastModifiedSQL

protected String updateLastModifiedSQL

copyFileSQL

protected String copyFileSQL

copyFilesSQL

protected String copyFilesSQL
Constructor Detail

DatabaseFileSystem

public DatabaseFileSystem()
Default constructor

Method Detail

getSchemaObjectPrefix

public String getSchemaObjectPrefix()

setSchemaObjectPrefix

public void setSchemaObjectPrefix(String schemaObjectPrefix)

getSchema

public String getSchema()

setSchema

public void setSchema(String schema)

isSchemaCheckEnabled

public final boolean isSchemaCheckEnabled()
Returns:
whether the schema check is enabled

setSchemaCheckEnabled

public final void setSchemaCheckEnabled(boolean enabled)
Parameters:
enabled - set whether the schema check is enabled

equals

public boolean equals(Object obj)

Overrides:
equals in class Object

hashCode

public int hashCode()
Returns zero to satisfy the Object equals/hashCode contract. This class is mutable and not meant to be used as a hash key.

Overrides:
hashCode in class Object
Returns:
always zero
See Also:
Object.hashCode()

init

public void init()
          throws FileSystemException
Initialize the file system

Specified by:
init in interface FileSystem
Throws:
FileSystemException - if the file system initialization fails

close

public void close()
           throws FileSystemException
Close the file system. After calling this method, the file system is no longer accessible.

Specified by:
close in interface FileSystem
Throws:
FileSystemException

createFolder

public void createFolder(String folderPath)
                  throws FileSystemException
Creates the folder named by this path, including any necessary but nonexistent parent folders. Note that if this operation fails it may have succeeded in creating some of the necessary parent folders.

Specified by:
createFolder in interface FileSystem
Parameters:
folderPath - the path of the folder to be created.
Throws:
FileSystemException - if a file system entry denoted by path already exists or if another error occurs.

deleteFile

public void deleteFile(String filePath)
                throws FileSystemException
Deletes the file denoted by this path.

Specified by:
deleteFile in interface FileSystem
Parameters:
filePath - the path of the file to be deleted.
Throws:
FileSystemException - if this path does not denote a file or if another error occurs.

deleteFolder

public void deleteFolder(String folderPath)
                  throws FileSystemException
Deletes the folder denoted by this path. Any contents of this folder (folders and files) will be deleted recursively.

Specified by:
deleteFolder in interface FileSystem
Parameters:
folderPath - the path of the folder to be deleted.
Throws:
FileSystemException - if this path does not denote a folder or if another error occurs.

exists

public boolean exists(String path)
               throws FileSystemException
Tests whether the file system entry denoted by this path exists.

Specified by:
exists in interface FileSystem
Parameters:
path - the path of a file system entry.
Returns:
true if the file system entry at path exists; false otherwise.
Throws:
FileSystemException

isFile

public boolean isFile(String path)
               throws FileSystemException
Tests whether the file system entry denoted by this path exists and is a file.

Specified by:
isFile in interface FileSystem
Parameters:
path - the path of a file system entry.
Returns:
true if the file system entry at path is a file; false otherwise.
Throws:
FileSystemException

isFolder

public boolean isFolder(String path)
                 throws FileSystemException
Tests whether the file system entry denoted by this path exists and is a folder.

Specified by:
isFolder in interface FileSystem
Parameters:
path - the path of a file system entry.
Returns:
true if the file system entry at path is a folder; false otherwise.
Throws:
FileSystemException

lastModified

public long lastModified(String path)
                  throws FileSystemException
Returns the time that the file system entry denoted by this path was last modified.

Specified by:
lastModified in interface FileSystem
Parameters:
path - the path of a file system entry.
Returns:
A long value representing the time the file system entry was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the modification time can't be determined.
Throws:
FileSystemException - if the file system entry does not exist.

length

public long length(String filePath)
            throws FileSystemException
Returns the length of the file denoted by this path.

Specified by:
length in interface FileSystem
Parameters:
filePath - the path of the file.
Returns:
The length, in bytes, of the file denoted by this path, or -1L if the length can't be determined.
Throws:
FileSystemException - if the path does not denote an existing file.

hasChildren

public boolean hasChildren(String path)
                    throws FileSystemException
Tests whether the file system entry denoted by this path has child entries.

Specified by:
hasChildren in interface FileSystem
Parameters:
path - the path of a file system entry.
Returns:
true if the file system entry at path has child entries; false otherwise.
Throws:
FileSystemException

list

public String[] list(String folderPath)
              throws FileSystemException
Returns an array of strings naming the files and folders in the folder denoted by this path.

Specified by:
list in interface FileSystem
Parameters:
folderPath - the path of the folder whose contents is to be listed.
Returns:
an array of strings naming the files and folders in the folder denoted by this path.
Throws:
FileSystemException - if this path does not denote a folder or if another error occurs.

listFiles

public String[] listFiles(String folderPath)
                   throws FileSystemException
Returns an array of strings naming the files in the folder denoted by this path.

Specified by:
listFiles in interface FileSystem
Parameters:
folderPath - the path of the folder whose contents is to be listed.
Returns:
an array of strings naming the files in the folder denoted by this path.
Throws:
FileSystemException - if this path does not denote a folder or if another error occurs.

listFolders

public String[] listFolders(String folderPath)
                     throws FileSystemException
Returns an array of strings naming the folders in the folder denoted by this path.

Specified by:
listFolders in interface FileSystem
Parameters:
folderPath - the path of the folder whose contents is to be listed.
Returns:
an array of strings naming the folders in the folder denoted by this path.
Throws:
FileSystemException - if this path does not denote a folder or if another error occurs.

touch

public void touch(String filePath)
           throws FileSystemException
Set the modified time of an existing file to now.

Specified by:
touch in interface FileSystem
Parameters:
filePath - the path of the file.
Throws:
FileSystemException - if the path does not denote an existing file.

getInputStream

public InputStream getInputStream(String filePath)
                           throws FileSystemException
Returns an input stream of the contents of the file denoted by this path.

Specified by:
getInputStream in interface FileSystem
Parameters:
filePath - the path of the file.
Returns:
an input stream of the contents of the file.
Throws:
FileSystemException - if the file does not exist or if it cannot be read from

getOutputStream

public OutputStream getOutputStream(String filePath)
                             throws FileSystemException
Returns an output stream for writing bytes to the file denoted by this path. The file will be created if it doesn't exist. If the file exists, its contents will be overwritten.

Specified by:
getOutputStream in interface FileSystem
Parameters:
filePath - the path of the file.
Returns:
an output stream for writing bytes to the file.
Throws:
FileSystemException - if the file cannot be written to or created

getRandomAccessOutputStream

public RandomAccessOutputStream getRandomAccessOutputStream(String filePath)
                                                     throws FileSystemException,
                                                            UnsupportedOperationException
Returns an output stream for writing bytes to the file denoted by this path. The file will be created if it doesn't exist. The current position of the file pointer is set to 0. See also RandomAccessOutputStream.seek(long);

Specified by:
getRandomAccessOutputStream in interface FileSystem
Parameters:
filePath - the path of the file.
Returns:
an random access output stream for writing bytes to the file.
Throws:
FileSystemException - if the file could not be created or if the output stream cannot be obtained.
UnsupportedOperationException - if the implementation does not support file access through a RandomAccessOutputStream.

copy

public void copy(String srcPath,
                 String destPath)
          throws FileSystemException
Copies a file or folder to a new location.

Specified by:
copy in interface FileSystem
Parameters:
srcPath - the path of the file or folder to be copied.
destPath - the destination path to which the file or folder is to be copied.
Throws:
FileSystemException - if the copy fails

move

public void move(String srcPath,
                 String destPath)
          throws FileSystemException
Moves a file or folder to a new location.

Specified by:
move in interface FileSystem
Parameters:
srcPath - the path of the file or folder to be moved.
destPath - the destination path to which the file or folder is to be moved.
Throws:
FileSystemException - if the move fails

initConnection

protected void initConnection()
                       throws Exception
Initializes the database connection used by this file system.

Subclasses should normally override the getConnection() method instead of this one. The default implementation calls getConnection() to get the database connection and disables the autocommit feature.

Throws:
Exception - if an error occurs

getConnection

protected Connection getConnection()
                            throws Exception
Abstract factory method for creating a new database connection. This method is called by initConnection() when the file system is started. The returned connection should come with the default JDBC settings, as the initConnection() method will explicitly set the autoCommit and other properties as needed.

Note that the returned database connection is kept during the entire lifetime of the file system, after which it is closed by close() using the closeConnection(Connection) method.

Returns:
new connection
Throws:
Exception - if an error occurs

closeConnection

protected void closeConnection(Connection connection)
                        throws SQLException
Closes the given database connection. This method is called by close() to close the connection acquired using getConnection() when the file system was started.

The default implementation just calls the Connection.close() method of the given connection, but subclasses can override this method to provide more extensive database and connection cleanup.

Parameters:
connection - database connection
Throws:
SQLException - if an error occurs

reestablishConnection

protected boolean reestablishConnection()
Re-establishes the database connection. This method is called by executeStmt(String, Object[]) after a SQLException had been encountered.

Returns:
true if the connection could be successfully re-established, false otherwise.

executeStmt

protected Statement executeStmt(String sql,
                                Object[] params)
                         throws SQLException
Executes the given SQL statement with the specified parameters. If a SQLException is encountered one attempt is made to re-establish the database connection and re-execute the statement.

Parameters:
sql - statement to execute
params - parameters to set
Returns:
the Statement object that had been executed
Throws:
SQLException - if an error occurs

prepareSchemaObjectPrefix

protected void prepareSchemaObjectPrefix()
                                  throws Exception
Makes sure that schemaObjectPrefix does only consist of characters that are allowed in names on the target database. Illegal characters will be escaped as necessary.

Throws:
Exception - if an error occurs

checkSchema

protected void checkSchema()
                    throws Exception
Checks if the required schema objects exist and creates them if they don't exist yet.

Throws:
Exception - if an error occurs

createSchemaSql

protected String createSchemaSql(String sql)
Replace wildcards.


buildSQLStatements

protected void buildSQLStatements()
Builds the SQL statements


initPreparedStatements

protected void initPreparedStatements()
                               throws SQLException
Initializes the map of prepared statements.

Throws:
SQLException - if an error occurs

verifyRootExists

protected void verifyRootExists()
                         throws Exception
Verifies that the root file system entry exists. If it doesn't exist yet it will be automatically created.

Throws:
Exception - if an error occurs

createDeepFolder

protected void createDeepFolder(String folderPath)
                         throws FileSystemException
Creates the specified files system folder entry, recursively creating any non-existing intermediate folder entries.

Parameters:
folderPath - folder entry to create
Throws:
FileSystemException - if an error occurs

copyDeepFolder

protected void copyDeepFolder(String srcPath,
                              String destPath)
                       throws FileSystemException
Recursively copies the given folder to the given destination.

Parameters:
srcPath - folder to be copied
destPath - destination path to which the folder is to be copied
Throws:
FileSystemException - if an error occurs

copyFile

protected void copyFile(String srcPath,
                        String destPath)
                 throws FileSystemException
Copies the given file entry to the given destination path. The parent folder of the destination path will be created if it doesn't exist already. If the destination path refers to an existing file, the file will be overwritten.

Parameters:
srcPath - file to be copied
destPath - destination path to which the file is to be copied
Throws:
FileSystemException - if an error occurs

resetStatement

protected void resetStatement(PreparedStatement stmt)
Resets the given PreparedStatement by clearing the parameters and warnings contained.

NOTE: This method MUST be called in a synchronized context as neither this method nor the PreparedStatement instance on which it operates are thread safe.

Parameters:
stmt - The PreparedStatement to reset. If null this method does nothing.

closeResultSet

protected void closeResultSet(ResultSet rs)

closeStatement

protected void closeStatement(Statement stmt)


Copyright © 2004-2009 The Apache Software Foundation. All Rights Reserved.