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.BufferedReader; 019import java.io.File; 020import java.util.ArrayList; 021import java.util.List; 022 023import org.opengion.fukurou.db.Transaction; 024import org.opengion.fukurou.util.ApplicationInfo; 025import org.opengion.fukurou.util.Closer; 026import org.opengion.fukurou.util.FileUtil; 027import org.opengion.fukurou.util.LogWriter; 028import org.opengion.fukurou.util.StringUtil; 029 030/** 031 * 伝送要求に対して、テキストファイルからデータを読取します。 032 * 但し、読取されるデータについては、旧伝送システムの形式と互換性を持たせるため、 033 * 31Byteから430Byteまでの400Byteを取得します。 034 * 035 * 読取するファイル名は、読取対象で指定します。 036 * ファイル名は絶対パスで指定する必要があります。 037 * 038 * 読込及びその後の実行処理が正常終了した場合は、読取ファイルは削除されます。 039 * 但し、読取パラメーターに"UNDEL"という文字を設定した場合は、正常終了した場合でも 040 * ファイルは削除されません。 041 * 042 * また、読取するテキストファイルのエンコードは読取パラメーターが指定することができます。 043 * 指定しない場合、システムリソースの"DB_ENCODE"で指定された値が適用されます。 044 * 045 * @og.group 伝送システム 046 * 047 * @version 5.0 048 * @author Hiroki.Nakamura 049 * @since JDK1.6 050 */ 051public class TransferRead_SAMCB implements TransferRead { 052 053 // 読取ファイルオブジェクト 054 private String fileName = null; 055 056 // 完了時に読取ファイルを削除するかどうか 057 private boolean fileDel = true; 058 059 /** 060 * ファイルからデータを読み取ります。 061 * 062 * @param config 伝送設定オブジェクト 063 * @param tran トランザクションオブジェクト 064 * 065 * @return 読み取りしたデータ(配列) 066 */ 067 @Override 068 public String[] read( final TransferConfig config, final Transaction tran ) { 069 fileName = config.getReadObj(); 070 File fileRead = new File( fileName ); 071 if( !fileRead.exists() ) { return new String[0]; } 072 073 String readPrm = config.getReadPrm(); 074 if( readPrm != null && readPrm.indexOf( "UNDEL" ) >= 0 ) { 075 fileDel = false; 076 readPrm = readPrm.replace( "UNDEL", "" ).trim(); 077 } 078 079 // 読取ファイルのエンコード 080 String fileEncode = readPrm; 081 if( fileEncode == null || fileEncode.length() == 0 ) { 082 fileEncode = "UTF-8"; 083 } 084 085 List<String> valList = new ArrayList<String>(); 086 BufferedReader reader = FileUtil.getBufferedReader( fileRead, fileEncode ); 087 String line = null; 088 try { 089 while( ( line = reader.readLine() ) != null ) { 090 line = StringUtil.stringFill( line, 500, fileEncode ); 091 byte[] bytes = StringUtil.makeByte( line, fileEncode ); 092 line = StringUtil.makeString( bytes, 30, 400, fileEncode ); 093 valList.add( line ); 094 } 095 } 096 catch( Throwable ex ) { 097 LogWriter.log( ex ); 098 String errMsg = "ファイル読取時にエラーが発生しました。[LINE=" + line + "]"; 099 throw new RuntimeException( errMsg, ex ); 100 } 101 finally { 102 Closer.ioClose( reader ); 103 } 104 105 return valList.toArray( new String[valList.size()] ); 106 } 107 108 /** 109 * 更新(削除)対象のファイル名(配列)を返します。 110 * 111 * @return ファイル名(配列) 112 */ 113 @Override 114 public String[] getKeys() { 115 return new String[] { fileName }; // 5.5.2.4 (2012/05/16) 116 } 117 118 /** 119 * 更新(削除)対象のファイル名をセットします。 120 * 121 * @param keys ファイル名(配列) 122 */ 123 @Override 124 public void setKeys( final String[] keys ) { 125 if( keys == null || keys.length == 0 ) { return; } 126 fileName = keys[0]; 127 } 128 129 /** 130 * 読取したデータに対して完了処理を行います。 131 * 具体的には、読取したテキストファイルを削除します。 132 * 133 * @param config 伝送設定オブジェクト 134 * @param tran トランザクションオブジェクト 135 */ 136 @Override 137 public void complete( final TransferConfig config, final Transaction tran ) { 138 if( !fileDel ) { return; } 139 140 File fileRead = new File( fileName ); 141 if( !fileRead.exists() ) { 142 return; 143 } 144 145 boolean rtn = fileRead.delete(); 146 if( !rtn ) { 147 String errMsg = "ファイルの削除に失敗しました。[FILE=" + fileRead.getAbsolutePath() + "]"; 148 throw new RuntimeException( errMsg ); 149 } 150 } 151 152 /** 153 * 読取したデータに対してエラー処理を行います。 154 * (ここでは何も処理しません) 155 * 156 * @param config 伝送設定オブジェクト 157 * @param appInfo DB接続情報 158 */ 159 @Override 160 public void error( final TransferConfig config, final ApplicationInfo appInfo ) {} 161}