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 */
016 package org.opengion.fukurou.transfer;
017
018 import java.io.File;
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import org.opengion.fukurou.db.Transaction;
023 import org.opengion.fukurou.util.ApplicationInfo;
024 import 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 */
044 public 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[0] );
072 fileNames = list.toArray( new String[list.size()] );
073
074 // return fileNames;
075 return fileNames.clone();
076 }
077
078 /**
079 * 更新(削除)対象のファイルå�?配å?)を返しますã?
080 *
081 * @og.rev 5.5.2.4 (2012/05/16) 配å?を返す場合ã?、å?部表現を暴露しなã�?‚ˆã�?�«、clone を返しますã?
082 *
083 * @return ファイルå�?配å?)
084 */
085 public String[] getKeys() {
086 // return fileNames;
087 String[] rtn = null ;
088 if( fileNames != null ) { rtn = fileNames.clone(); }
089 return rtn ;
090 }
091
092 /**
093 * 更新(削除)対象のファイルå�?配å?)をセãƒ?ƒˆしますã?
094 *
095 * @og.rev 5.5.2.4 (2012/05/16) 参ç?の格納には、System.arraycopy を使ã�?�¾すã?
096 *
097 * @param keys ファイルå�?配å?)
098 */
099 public void setKeys( final String[] keys ) {
100 // fileNames = keys;
101 if( keys != null ) {
102 int size = keys.length ;
103 fileNames = new String[size];
104 System.arraycopy( keys,0,fileNames,0,size );
105 }
106 else {
107 fileNames = null;
108 }
109 }
110
111 /**
112 * 読取した伝é?ãƒ??タのヘッãƒ??ãƒ??タの状況を'2'(抜å?済み)に更新しますã?
113 * 更新対象の通番NOにつã�?�¦は、{@link #setKeys(String[])}で外部からセãƒ?ƒˆすることもできますã?
114 *
115 * @param config 伝é?設定オブジェクãƒ?
116 * @param tran トランザクションオブジェクãƒ?
117 * @see #setKeys(String[])
118 */
119 @Override
120 public void complete( final TransferConfig config, final Transaction tran ) {
121 if( fileNames == null || fileNames.length == 0 ) { return; }
122 // 読取パラメーターに"NODEL"が指定されてã�?‚‹場合ã?、スキャンしたファイルを削除しなã�??
123 if( "NODEL".equalsIgnoreCase( config.getReadPrm() ) ) { return; }
124
125 for( String fileName : fileNames ) {
126 File file = new File( fileName );
127 if( !file.exists() ) {
128 String errMsg = "ファイルが存在しませんã€?FILE=" + file.getAbsolutePath() + "]";
129 throw new RuntimeException( errMsg );
130 }
131
132 boolean rtn = file.delete();
133 if( !rtn ) {
134 String errMsg = "ファイルの削除に失敗しましたã€?FILE=" + file.getAbsolutePath() + "]";
135 throw new RuntimeException( errMsg );
136 }
137 }
138 }
139
140 /**
141 * (ここでは何もしません)
142 *
143 * @param config 伝é?設定オブジェクãƒ?
144 * @param appInfo DB接続情報
145 */
146 @Override
147 public void error( final TransferConfig config, final ApplicationInfo appInfo ) {
148 }
149 }