databasefilesystem.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 1,559 行 · 第 1/4 页
JAVA
1,559 行
} finally { closeStream(in); closeStatement(stmt); } } } /** * Builds the SQL statements */ protected void buildSQLStatements() { insertFileSQL = "insert into " + schemaObjectPrefix + "FSENTRY " + "(FSENTRY_PATH, FSENTRY_NAME, FSENTRY_DATA, " + "FSENTRY_LASTMOD, FSENTRY_LENGTH) " + "values (?, ?, ?, ?, ?)"; insertFolderSQL = "insert into " + schemaObjectPrefix + "FSENTRY " + "(FSENTRY_PATH, FSENTRY_NAME, FSENTRY_LASTMOD, FSENTRY_LENGTH) " + "values (?, ?, ?, 0)"; updateDataSQL = "update " + schemaObjectPrefix + "FSENTRY " + "set FSENTRY_DATA = ?, FSENTRY_LASTMOD = ?, FSENTRY_LENGTH = ? " + "where FSENTRY_PATH = ? and FSENTRY_NAME = ? " + "and FSENTRY_DATA is not null"; updateLastModifiedSQL = "update " + schemaObjectPrefix + "FSENTRY set FSENTRY_LASTMOD = ? " + "where FSENTRY_PATH = ? and FSENTRY_NAME = ? " + "and FSENTRY_DATA is not null"; selectExistSQL = "select 1 from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ?"; selectFileExistSQL = "select 1 from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is not null"; selectFolderExistSQL = "select 1 from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is null"; selectFileNamesSQL = "select FSENTRY_NAME from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_DATA is not null"; selectFolderNamesSQL = "select FSENTRY_NAME from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_DATA is null"; selectFileAndFolderNamesSQL = "select FSENTRY_NAME from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ?"; selectChildCountSQL = "select count(FSENTRY_NAME) from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? "; selectDataSQL = "select FSENTRY_DATA from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is not null"; selectLastModifiedSQL = "select FSENTRY_LASTMOD from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ?"; selectLengthSQL = "select FSENTRY_LENGTH from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is not null"; deleteFileSQL = "delete from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is not null"; deleteFolderSQL = "delete from " + schemaObjectPrefix + "FSENTRY where " + "(FSENTRY_PATH = ? and FSENTRY_NAME = ? and FSENTRY_DATA is null) " + "or (FSENTRY_PATH = ?) " + "or (FSENTRY_PATH like ?) "; copyFileSQL = "insert into " + schemaObjectPrefix + "FSENTRY " + "(FSENTRY_PATH, FSENTRY_NAME, FSENTRY_DATA, " + "FSENTRY_LASTMOD, FSENTRY_LENGTH) " + "select ?, ?, FSENTRY_DATA, " + "FSENTRY_LASTMOD, FSENTRY_LENGTH from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_NAME = ? and FSENTRY_DATA is not null"; copyFilesSQL = "insert into " + schemaObjectPrefix + "FSENTRY " + "(FSENTRY_PATH, FSENTRY_NAME, FSENTRY_DATA, " + "FSENTRY_LASTMOD, FSENTRY_LENGTH) " + "select ?, FSENTRY_NAME, FSENTRY_DATA, " + "FSENTRY_LASTMOD, FSENTRY_LENGTH from " + schemaObjectPrefix + "FSENTRY where FSENTRY_PATH = ? " + "and FSENTRY_DATA is not null"; } /** * Initializes the map of prepared statements. * * @throws SQLException if an error occurs */ protected void initPreparedStatements() throws SQLException { preparedStatements.put( selectExistSQL, con.prepareStatement(selectExistSQL)); preparedStatements.put( selectFileExistSQL, con.prepareStatement(selectFileExistSQL)); preparedStatements.put( selectFolderExistSQL, con.prepareStatement(selectFolderExistSQL)); preparedStatements.put( selectChildCountSQL, con.prepareStatement(selectChildCountSQL)); preparedStatements.put( selectDataSQL, con.prepareStatement(selectDataSQL)); preparedStatements.put( selectLastModifiedSQL, con.prepareStatement(selectLastModifiedSQL)); preparedStatements.put( selectLengthSQL, con.prepareStatement(selectLengthSQL)); preparedStatements.put( selectFileNamesSQL, con.prepareStatement(selectFileNamesSQL)); preparedStatements.put( selectFolderNamesSQL, con.prepareStatement(selectFolderNamesSQL)); preparedStatements.put( selectFileAndFolderNamesSQL, con.prepareStatement(selectFileAndFolderNamesSQL)); preparedStatements.put( deleteFileSQL, con.prepareStatement(deleteFileSQL)); preparedStatements.put( deleteFolderSQL, con.prepareStatement(deleteFolderSQL)); preparedStatements.put( insertFileSQL, con.prepareStatement(insertFileSQL)); preparedStatements.put( insertFolderSQL, con.prepareStatement(insertFolderSQL)); preparedStatements.put( updateDataSQL, con.prepareStatement(updateDataSQL)); preparedStatements.put( updateLastModifiedSQL, con.prepareStatement(updateLastModifiedSQL)); preparedStatements.put( copyFileSQL, con.prepareStatement(copyFileSQL)); preparedStatements.put( copyFilesSQL, con.prepareStatement(copyFilesSQL)); } /** * 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 */ protected void verifyRootExists() throws Exception { // check if root file system entry exists synchronized (selectFolderExistSQL) { ResultSet rs = null; try { Statement stmt = executeStmt( selectFolderExistSQL, new Object[]{FileSystem.SEPARATOR, ""}); rs = stmt.getResultSet(); if (rs.next()) { // root entry exists return; } } catch (SQLException e) { String msg = "failed to check existence of file system root entry"; log.error(msg, e); throw new FileSystemException(msg, e); } finally { closeResultSet(rs); } } // the root entry doesn't exist yet, create it... createDeepFolder(FileSystem.SEPARATOR); } /** * Creates the specified files system folder entry, recursively creating * any non-existing intermediate folder entries. * * @param folderPath folder entry to create * @throws FileSystemException if an error occurs */ protected void createDeepFolder(String folderPath) throws FileSystemException { String parentDir = FileSystemPathUtil.getParentDir(folderPath); String name = FileSystemPathUtil.getName(folderPath); if (!FileSystemPathUtil.denotesRoot(folderPath)) { if (!exists(parentDir)) { createDeepFolder(parentDir); } } synchronized (insertFolderSQL) { try { executeStmt( insertFolderSQL, new Object[]{ parentDir, name, new Long(System.currentTimeMillis())}); } catch (SQLException e) { String msg = "failed to create folder entry: " + folderPath; log.error(msg, e); throw new FileSystemException(msg, e); } } } /** * Recursively copies the given folder to the given destination. * * @param srcPath folder to be copied * @param destPath destination path to which the folder is to be copied * @throws FileSystemException if an error occurs */ protected void copyDeepFolder(String srcPath, String destPath) throws FileSystemException { if (!exists(destPath)) { createDeepFolder(destPath); } String[] names = listFolders(srcPath); for (int i = 0; i < names.length; i++) { String src = (FileSystemPathUtil.denotesRoot(srcPath) ? srcPath + names[i] : srcPath + FileSystem.SEPARATOR + names[i]); String dest = (FileSystemPathUtil.denotesRoot(destPath) ? destPath + names[i] : destPath + FileSystem.SEPARATOR + names[i]); copyDeepFolder(src, dest); } synchronized (copyFilesSQL) { try { executeStmt(copyFilesSQL, new Object[]{destPath, srcPath}); } catch (SQLException e) { String msg = "failed to copy file entries from " + srcPath + " to " + destPath; log.error(msg, e); throw new FileSystemException(msg, e); } } } /** * 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. * * @param srcPath file to be copied * @param destPath destination path to which the file is to be copied * @throws FileSystemException if an error occurs */ protected void copyFile(String srcPath, String destPath) throws FileSystemException { final String srcParentDir = FileSystemPathUtil.getParentDir(srcPath); final String srcName = FileSystemPathUtil.getName(srcPath); final String destParentDir = FileSystemPathUtil.getParentDir(destPath); final String destName = FileSystemPathUtil.getName(destPath); if (!exists(destParentDir)) { createDeepFolder(destParentDir); } if (isFile(destPath)) { deleteFile(destPath); } int count = 0; synchronized (copyFileSQL) { try { Statement stmt = executeStmt( copyFileSQL, new Object[]{ destParentDir, destName, srcParentDir, srcName}); count = stmt.getUpdateCount(); } catch (SQLException e) { String msg = "failed to copy file from " + srcPath + " to " + destPath; log.error(msg, e); throw new FileSystemException(msg, e); } } if (count == 0) { throw new FileSystemException("no such file: " + srcPath); } } /** * Resets the given <code>PreparedStatement</code> by clearing the parameters * and warnings contained. * <p/> * NOTE: This method MUST be called in a synchronized context as neither * this method nor the <code>PreparedStatement</code> instance on which it * operates are thread safe. * * @param stmt The <code>PreparedStatement</code> to reset. If * <code>null</code> this method does nothing. */ protected void resetStatement(PreparedStatement stmt) { if (stmt != null) { try { stmt.clearParameters(); stmt.clearWarnings(); } catch (SQLException se) { log.error("failed resetting PreparedStatement", se); } } } protected void closeResultSet(ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException se) { log.error("failed closing ResultSet", se); } } } protected void closeStream(InputStream in) { if (in != null) { try { in.close(); } catch (IOException ignore) { // ignore } } } protected void closeStatement(Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException se) { log.error("failed closing Statement", se); } } } //--------------------------------------------------------< inner classes > class SizedInputStream extends FilterInputStream { private final long size; private boolean consumed = false; SizedInputStream(InputStream in, long size) { super(in); this.size = size; } long getSize() { return size; } boolean isConsumed() { return consumed; } public int read() throws IOException { consumed = true; return super.read(); } public long skip(long n) throws IOException { consumed = true; return super.skip(n); } public int read(byte b[]) throws IOException { consumed = true; return super.read(b); } public int read(byte b[], int off, int len) throws IOException { consumed = true; return super.read(b, off, len); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?