📄 filemanager.java
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.eqlext.utils;import com.queplix.core.error.GenericSystemException;import com.queplix.core.modules.config.utils.SysPropertyManager;import com.queplix.core.modules.eqlext.error.FileManagerException;import com.queplix.core.utils.FileHelper;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.log.AbstractLogger;import com.queplix.core.utils.log.Log;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;/** * Attachment's file and directory helper class. * @see com.queplix.core.utils.FileHelper * * @author [ALB] Andrey Baranov * @author [DBR] Daniel Raskin * @author [ONZ] Oleg N. Zhovtanyuk * @version $Revision: 1.1.1.1 $ $Date: 2005/09/12 15:30:41 $ */public final class FileManager { // ========================================================== Initialization // Name separator public static final char NAME_SEPARATOR = '/'; // Logger instance. private static final AbstractLogger logger = Log.getLog( FileManager.class ); // Private constructor - blocks instantiation. private FileManager() {} // ========================================== Directories management methods /** * Returns the main file storage directory. * @return File object */ public static File getFSDirectory() { try { String dirName = SysPropertyManager.getProperty( "FILE_STORAGE" ); File fsDir = new File( dirName ); if (!fsDir.exists()) { fsDir.mkdirs(); } return fsDir.getCanonicalFile(); } catch( IOException ex ) { throw new GenericSystemException( ex ); } } /** * Gets the directory (as the <code>File</code> object) by the name. * * @param dirName LOCAL directory name * @return File object or null, if not found * @throws FileManagerException */ public static File getDirectory( String dirName ) throws FileManagerException { if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Getting directory '" + dirName + "'..." ); } // Check for empty name. if( StringHelper.isEmpty( dirName ) ) { throw new NullPointerException( "Directory name is empty." ); } File dir; try { dir = getFullFile( dirName ); String fullName = dir.getAbsolutePath(); if( !checkFile( dir ) ) { throw new java.lang.SecurityException( "Access denied to file '" + fullName + "'." ); } if( !dir.exists() ) { logger.WARN( "Directory '" + fullName + "' does not exist." ); return null; } } catch( java.lang.SecurityException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } // Ok. if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Got directory '" + dir + "'." ); } return dir; } /** * Creates a new directory. * * @param dirName LOCAL directory name * @return <b>true</b> on success, <b>false</b> on fail * @throws FileManagerException */ public static boolean createDirectory( String dirName ) throws FileManagerException { if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Creating directory '" + dirName + "'..." ); } // Check for empty name. if( StringHelper.isEmpty( dirName ) ) { throw new NullPointerException( "Directory name is empty." ); } File dir; try { dir = getFullFile( dirName ); if( !checkFile( dir ) ) { throw new java.lang.SecurityException( "Can't create directory '" + dir.getAbsolutePath() + "' - access denied." ); } FileHelper.createDirectory( dir ); } catch( IOException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } catch( java.lang.SecurityException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } // Ok. if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Directory '" + dir + "' created." ); } return true; } /** * Rename the existing directory. * * @param oldDirName old LOCAL directory name * @param dirName new LOCAL directory name * @return <b>true</b> on success, <b>false</b> on fail * @throws FileManagerException */ public static boolean renameDirectory( String oldDirName, String dirName ) throws FileManagerException { if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Renaming directory '" + oldDirName + "' to '" + dirName + "'..." ); } // Check for empty names. if( StringHelper.isEmpty( oldDirName ) ) { throw new NullPointerException( "The old directory name is empty." ); } if( StringHelper.isEmpty( dirName ) ) { throw new NullPointerException( "The new directory name is empty." ); } File oldDir; File dir; try { oldDir = getFullFile( oldDirName ); dir = getFullFile( dirName ); // If directories are similar - skip process if( oldDir.equals( dir ) ) { return false; } String oldFullName = oldDir.getAbsolutePath(); String fullName = dir.getAbsolutePath(); if( !oldDir.exists() ) { throw new FileNotFoundException( "Can't rename directory '" + oldFullName + "' - it doesn't exist." ); } if( !oldDir.isDirectory() ) { throw new FileNotFoundException( "Can't rename '" + oldFullName + "' - it is not a directory." ); } if( !checkFile( dir ) ) { throw new java.lang.SecurityException( "Can't rename to directory '" + fullName + "' - access denied." ); } // Rename dir. /** @todo move it to FileHelper in future */ if( !oldDir.renameTo( dir ) ) { throw new IOException( "Can't rename directory '" + oldFullName + "' to '" + fullName + "' - internal I/O error." ); } } catch( IOException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } catch( java.lang.SecurityException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } // Ok. if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Directory '" + oldDir + "' is renamed to '" + dir + "'." ); } return true; } /** * Deletes the existing directory. * * @param dirName LOCAL directory name * @return <b>true</b> on success, <b>false</b> on fail * @throws FileManagerException */ public static boolean deleteDirectory( String dirName ) throws FileManagerException { if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Deleting directory '" + dirName + "'..." ); } // Check for empty name. if( StringHelper.isEmpty( dirName ) ) { throw new NullPointerException( "Directory name is empty." ); } File dir; try { dir = getFullFile( dirName ); if( !dir.exists() ) { logger.WARN( "Directory '" + dirName + "' doesn't exist." ); return false; } String fullName = dir.getAbsolutePath(); if( !dir.isDirectory() ) { throw new FileNotFoundException( "Can't delete '" + fullName + "' - it is not a directory." ); } String[] listing = dir.list(); if( listing != null && listing.length > 0 ) { throw new IOException( "Can't delete directory '" + fullName + "' - it is not empty" ); } if( !checkFile( dir ) ) { throw new java.lang.SecurityException( "Can't delete directory '" + fullName + "' - access denied." ); } // Delete directory. if( !FileHelper.delete( dir ) ) { throw new IOException( "Can't delete directory '" + fullName + "' - internal I/O error." ); } } catch( IOException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } catch( java.lang.SecurityException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } // Ok. if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Directory '" + dir + "' deleted." ); } return true; } // ================================================ Files management methods /** * Loads the file by name (as the <code>FileInfo</code> object). * * @param filePath LOCAL file path * @return FileInfo object * @throws FileManagerException */ public static FileInfo loadFile( String filePath ) throws FileManagerException { if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "Loading file '" + filePath + "'..." ); } // Check for empty path. if( StringHelper.isEmpty( filePath ) ) { throw new NullPointerException( "File path is empty." ); } FileInfo fi; try { File fullFile = getFullFile( filePath ); if( logger.getLogger().isDebugEnabled() ) { logger.DEBUG( "Full path: " + fullFile.getCanonicalPath() ); } byte[] buf = FileHelper.loadFile( fullFile ); fi = new FileInfo( filePath, buf ); } catch( IOException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } catch( java.lang.SecurityException ex ) { logger.ERROR( ex ); throw new FileManagerException( ex ); } // Ok. if( logger.getLogger().isInfoEnabled() ) { logger.INFO( "File '" + filePath + "' loaded." ); logger.INFO( "Loaded " + fi.getData().length + " bytes" ); } return fi; } /** * <p>Copy file with LOCAL path <code>srcFilePath</code> to the * destination file <code>destFile</code>.</p> * <p>Don't check access permissions for <code>destFile</code>.</p> * <p>Create all subdirectories if needed.</p> * @param srcFilePath given LOCAL file path * @param destFile destination file * @throws FileManagerException */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -