001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.fukurou.transfer; 017 018import java.io.File; 019import java.util.ArrayList; 020import java.util.List; 021 022import org.opengion.fukurou.db.Transaction; 023import org.opengion.fukurou.util.ApplicationInfo; 024import org.opengion.fukurou.util.FileUtil; 025 026/** 027 * 伝送要求に対して、ファイルまたはディレクトリをスキャンし、それに含まれる 028 * ファイルの絶対パスのリストを取得します。 029 * 030 * 伝送定義マスタの読取対象は、スキャン対象のファイルまたはディレクトリを指定します。 031 * 処理実行後は、正常終了した場合は、スキャンしたファイルを削除します。 032 * 但し、読取パラメーターに"NODEL"を指定した場合、ファイルの削除は行われません。 033 * また、エラーが発生した場合は読取パラメーターの設定に関わらずファイルの削除は 034 * 行われません。 035 * 036 * 読取対象にディレクトリを指定した場合は、再起的にサブフォルダもスキャンされます。 037 * 038 * @og.group 伝送システム 039 * 040 * @version 5.0 041 * @author Hiroki.Nakamura 042 * @since JDK1.6 043 */ 044public class TransferRead_FILELIST implements TransferRead { 045 046 // 更新(削除)対象のファイル名(配列) 047 private String[] fileNames = null; 048 049 /** 050 * ファイルまたはディレクトリをスキャンしファイルの絶対パスのリストを取得します。 051 * 052 * @og.rev 5.4.3.2 (2011/12/06) コピー中のファイル判定追加 053 * @og.rev 5.5.2.4 (2012/05/16) 配列を返す場合は、内部表現を暴露しないように、clone を返します。 054 * 055 * @param config 伝送設定オブジェクト 056 * @param tran トランザクションオブジェクト 057 * 058 * @return ファイル一覧(配列) 059 */ 060 @Override 061 public String[] read( final TransferConfig config, final Transaction tran ) { 062 File file = new File( config.getReadObj() ); 063 if( !file.exists() ) { 064 String errMsg = "スキャン対象のファイル/ディレクトリ。[FILE=" + file.getAbsolutePath() + "]"; 065 throw new RuntimeException( errMsg ); 066 } 067 068 List<String> list = new ArrayList<String>(); 069 FileUtil.getFileList( file, false, list , false); // 5.4.3.2 コピー判定追加 070 071 fileNames = list.toArray( new String[list.size()] ); 072 073 return fileNames.clone(); 074 } 075 076 /** 077 * 更新(削除)対象のファイル名(配列)を返します。 078 * 079 * @og.rev 5.5.2.4 (2012/05/16) 配列を返す場合は、内部表現を暴露しないように、clone を返します。 080 * 081 * @return ファイル名(配列) 082 */ 083 public String[] getKeys() { 084 String[] rtn = null ; 085 if( fileNames != null ) { rtn = fileNames.clone(); } 086 return rtn ; 087 } 088 089 /** 090 * 更新(削除)対象のファイル名(配列)をセットします。 091 * 092 * @og.rev 5.5.2.4 (2012/05/16) 参照の格納には、System.arraycopy を使います。 093 * 094 * @param keys ファイル名(配列) 095 */ 096 public void setKeys( final String[] keys ) { 097 if( keys != null ) { 098 int size = keys.length ; 099 fileNames = new String[size]; 100 System.arraycopy( keys,0,fileNames,0,size ); 101 } 102 else { 103 fileNames = null; 104 } 105 } 106 107 /** 108 * 読取した伝送データのヘッダーデータの状況を'2'(抜出済み)に更新します。 109 * 更新対象の通番NOについては、{@link #setKeys(String[])}で外部からセットすることもできます。 110 * 111 * @param config 伝送設定オブジェクト 112 * @param tran トランザクションオブジェクト 113 * @see #setKeys(String[]) 114 */ 115 @Override 116 public void complete( final TransferConfig config, final Transaction tran ) { 117 if( fileNames == null || fileNames.length == 0 ) { return; } 118 // 読取パラメーターに"NODEL"が指定されている場合は、スキャンしたファイルを削除しない。 119 if( "NODEL".equalsIgnoreCase( config.getReadPrm() ) ) { return; } 120 121 for( String fileName : fileNames ) { 122 File file = new File( fileName ); 123 if( !file.exists() ) { 124 String errMsg = "ファイルが存在しません。[FILE=" + file.getAbsolutePath() + "]"; 125 throw new RuntimeException( errMsg ); 126 } 127 128 boolean rtn = file.delete(); 129 if( !rtn ) { 130 String errMsg = "ファイルの削除に失敗しました。[FILE=" + file.getAbsolutePath() + "]"; 131 throw new RuntimeException( errMsg ); 132 } 133 } 134 } 135 136 /** 137 * (ここでは何もしません) 138 * 139 * @param config 伝送設定オブジェクト 140 * @param appInfo DB接続情報 141 */ 142 @Override 143 public void error( final TransferConfig config, final ApplicationInfo appInfo ) {} 144}