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.hayabusa.remote;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.opengion.fukurou.db.Transaction;
023import org.opengion.fukurou.db.TransactionReal;
024import org.opengion.fukurou.transfer.TransferConfig;
025import org.opengion.fukurou.transfer.TransferExec;
026import org.opengion.fukurou.util.ApplicationInfo;
027import org.opengion.fukurou.util.StringUtil;
028import org.opengion.hayabusa.common.HybsSystem;
029import org.opengion.hayabusa.common.HybsSystemException;
030
031/**
032 * RemoteControllableインタフェイスを実装した
033 * サーブレット経由で遠隔伝送実行処理を行うためのクラスです。
034 *
035 * このクラスは、伝送実行処理のラッパークラスです。
036 * 引数のKBEXECのパラメーターに基づき、伝送実行オブジェクトを生成し、伝送処理を実行します。
037 * 詳細については、{@link org.opengion.fukurou.transfer.TransferExec_HTTP}を参照して下さい。
038 *
039 * @og.rev 5.4.2.0 (2011/12/01) 新規作成
040 *
041 * @version  4.1
042 * @author   Hiroki Nakamura
043 * @since    JDK6.0,
044 *
045 */
046public class TransferExecWrapper implements RemoteControllable {
047
048        // 伝送実行クラスのベースクラス名
049        private static final String EXEC_CLASS_BASE = "org.opengion.fukurou.transfer.TransferExec_" ;
050
051        // コネクションにアプリケーション情報を追記するかどうか指定
052        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
053
054        private static final ApplicationInfo appInfo;
055
056        static {
057                if( USE_DB_APPLICATION_INFO ) {
058                        appInfo = new ApplicationInfo();
059                        // ユーザーID,IPアドレス,ホスト名
060                        appInfo.setClientInfo( "TransferExecWrapper",HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
061                        // 画面ID,操作,プログラムID
062                        appInfo.setModuleInfo( "TransferExecWrapper","TransferExecWrapper","TransferExecWrapper" );
063                }
064                else {
065                        appInfo = null;
066                }
067        }
068        /**
069         * RemoteControllableインタフェイスの実装メソッドです。
070         *
071         * @og.rev 5.7.1.2 (2013/12/20) msg ⇒ errMsg 変更
072         *
073         * @param       valMap   サーブレットが受け取ったキーと値のマップ
074         *
075         * @return      XML形式の実行結果
076         */
077        public String remoteControl( final Map<String,String> valMap ) {
078                // パラメーターより伝送設定オブジェクトを生成します。
079                TransferConfig conf = new TransferConfig(
080                                                                valMap.get( "KBREAD" )
081                                                                , valMap.get( "READOBJ" )
082                                                                , valMap.get( "READPRM" )
083                                                                , valMap.get( "KBEXEC" )
084                                                                , valMap.get( "EXECDBID" )
085                                                                , valMap.get( "EXECOBJ" )
086                                                                , valMap.get( "EXECPRM" )
087                                                                , valMap.get( "ERROR_SENDTO" )
088                                                                , valMap.get( "HFROM" )
089                                                                , null, -1 );
090
091                // パラメーターより伝送実行オブジェクトに渡す値一覧(配列)を生成します。
092                String[] vals = getVals( valMap );
093
094                Transaction tran = null;
095                try {
096                        tran = new TransactionReal( appInfo );
097                        // 実行方法のオブジェクトを生成します。
098                        TransferExec exec = (TransferExec)StringUtil.newInstance( EXEC_CLASS_BASE + valMap.get( "KBEXEC" ) );
099                        // 処理を実行します。
100                        exec.execute( vals, conf, tran );
101                }
102                catch ( Throwable ex ) {
103                        String errMsg = "伝送処理実行(HTTP経由)でエラーが発生しました。";
104                        throw new HybsSystemException( errMsg, ex );    // 5.7.1.2 (2013/12/20) msg ⇒ errMsg 変更
105                }
106                finally {
107                        if( tran != null ) { tran.close(); }
108                }
109
110                return "";
111        }
112
113        /**
114         * パラメーターより伝送実行オブジェクトに渡す値一覧(配列)を生成します。
115         * 対象パラメーターは①n(データ件数) と ②v1~vn(データ) です。
116         *
117         * @param valMap パラメーターMap
118         *
119         * @return 値一覧(配列)
120         */
121        private String[] getVals( final Map<String,String> valMap ) {
122                int rows = 0;
123                String rno = valMap.get( "n" );
124                if( rno != null && rno.length() > 0 ) {
125                        rows = Integer.valueOf( rno );
126                }
127                List<String> list = new ArrayList<String>();
128                for( int i=0; i<rows; i++ ) {
129                        String val = valMap.get( "v" + i );
130                        list.add( val );
131                }
132                return list.toArray( new String[list.size()] );
133        }
134}