001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: FileOperator.java 11751 2008-01-30 22:23:27Z mpreston $
024: */
025: package com.bostechcorp.cbesb.runtime.file;
026:
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.InputStream;
031: import java.io.OutputStream;
032:
033: import com.bostechcorp.cbesb.common.util.ErrorUtil;
034:
035: /**
036: * The file operation class.
037: *
038: * @author j.zhang
039: * @version 1.0.0
040: */
041: public class FileOperator {
042:
043: /**
044: * Scan the folder for all files.
045: *
046: * @param dirName The folder name.
047: * @return All of the files in the folder.
048: */
049: public static File[] scanDir(String dirName) {
050: FileOperator.createFolder(dirName);
051: File folder = new File(dirName);
052: File[] files = folder.listFiles();
053: return files;
054: }
055:
056: /**
057: * Scan the stage directory to check if any files are present in it.
058: *
059: * @param stageDir The stageDir name.
060: * @param holdDir The holdDir name.
061: * @param sourceDir The sourceDir name.
062: * @param isHold Boolean value.
063: */
064: public static void scanStageDir(String stageDir, String holdDir,
065: String sourceDir, boolean isHold) {
066: // Check if any files are present in StageDir.
067: File[] stageFiles = scanDir(stageDir);
068: if (stageFiles != null) {
069: for (int i = 0; i < stageFiles.length; i++) {
070: if (stageFiles[i].isHidden()
071: || stageFiles[i].isDirectory()) {
072: continue;
073: }
074: if (isHold) {
075: // move the files to HoldDir.
076: FileOperator.moveFile(stageFiles[i], holdDir);
077: } else {
078: if (sourceDir != null) {
079: // move the files to SourceDir to be reprocessed.
080: FileOperator.moveFile(stageFiles[i], sourceDir);
081: } else {
082: // For FTP component, any files in stageDir should be deleted.
083: FileOperator.deleteFile(stageFiles[i]);
084: }
085: }
086: }
087: }
088: }
089:
090: /**
091: * Move the file to another folder.
092: *
093: * @param oldPath The old file.
094: * @param folderName The file path that is folder name.
095: */
096: public static void moveFile(File oldFile, String folderName) {
097: // copyFile(oldFile, folderName);
098: // deleteFile(oldFile);
099: createFolder(folderName);
100: oldFile.renameTo(new File(folderName, oldFile.getName()));
101: }
102:
103: /**
104: * Copy the file to another folder and the new filename must be specificed.
105: *
106: * @param oldFile The old file.
107: * @param newPath The new file path that is folder name.
108: */
109: public static void copyFile(File oldFile, String newPath) {
110: try {
111: int byteRead = 0;
112: int byteSum = 0;
113: if (oldFile.exists()) {
114: InputStream inputStream = new FileInputStream(oldFile);
115: createFolder(newPath);
116: String newFilename = newPath + File.separator
117: + oldFile.getName();
118: OutputStream outputStream = new FileOutputStream(
119: newFilename);
120: byte[] buffer = new byte[1024];
121: while ((byteRead = inputStream.read(buffer)) != -1) {
122: byteSum += byteRead;
123: outputStream.write(buffer, 0, byteRead);
124: }
125: outputStream.close();
126: inputStream.close();
127: }
128: } catch (Exception e) {
129: // throw new Exception(new Message(Messages.FILE_COPY_ERROR).getMessage());
130:
131: ErrorUtil.printError("Exception in copyFile(): ", e);
132: }
133: }
134:
135: /**
136: * Delete the specificed file.
137: *
138: * @param path This is the full file path and filename.
139: * @return The delete flag: true is successful; false is unsuccessful.
140: */
141: public static boolean deleteFile(File file) {
142: boolean deleteFlag = false;
143: if (file.exists()) {
144: deleteFlag = file.delete();
145: }
146: return deleteFlag;
147: }
148:
149: /**
150: * Create the specificed folder.
151: *
152: * @param folderPath The folder name and full path.
153: */
154: public static void createFolder(String folderPath) {
155: File createFolder = new File(folderPath);
156: if (!createFolder.exists()) {
157: createFolder.mkdirs();
158: }
159: }
160:
161: /**
162: * Archive the specified file.
163: *
164: * @param filePath The file path and filename.
165: * @param archivePath The archive location to place file.
166: * @param archiveFilePattern Describes a file pattern to user to rename the file when being archived.
167: * @param count An automatically incremented value that starts from 1 when the component is started.
168: */
169: public static void archiveFile(File file, String archivePath,
170: String archiveFilePattern, int count) {
171: createFolder(archivePath);
172: String filename = FilePatternMatcher.processFilePattern(file
173: .getName(), archiveFilePattern, count);
174: if (filename.equals("")) {
175: filename = file.getName();
176: }
177: File dest = new File(archivePath, filename);
178: file.renameTo(dest);
179: }
180: }
|