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.process;
017
018import org.opengion.fukurou.db.ConnectionFactory;
019import org.opengion.fukurou.util.Argument;
020import org.opengion.fukurou.util.ApplicationInfo;
021import org.opengion.fukurou.util.LogWriter;
022
023import java.util.Set ;
024import java.util.Map ;
025import java.util.LinkedHashMap ;
026import java.net.InetAddress;
027import java.net.UnknownHostException;
028
029import java.sql.Connection;
030
031/**
032 * Process_DBParam は、他のプロセスへ共通のデータベース接続を割り当てる為の、
033 * ParamProcess インターフェースの実装クラスです。
034 *
035 * DB接続 が必要な Process (DBCountFilter、DBMerge、DBReader、DBWriterなど)を
036 * 使用して処理する場合に、接続を指定することができます。
037 * DBID(接続先) は、Process_DBParam の -configFile で指定する DBConfig.xml ファイルを使用します。
038 *
039 * @og.formSample
040 *  Process_DBParam -infoUSER=C00000 -infoPGID=GE1234 -configFile=DBConfig.xml
041 *
042 *     -infoUSER=実行ユーザー         : DB接続履歴取得用の実行ユーザー(例:C00000)
043 *     -infoPGID=実行プログラムID     : DB接続履歴取得用の実行プログラムID(例:GE1234)
044 *     -configFile=実行プログラムID   : DB接続情報設定 XMLファイル(例:DBConfig.xml)
045 *
046 * @og.rev 4.0.0.0 (2007/11/22) DBConfig.xml による DBID(接続先)指定に変更。
047 *
048 * @version  4.0
049 * @author   Kazuhiko Hasegawa
050 * @since    JDK5.0,
051 */
052public class Process_DBParam extends AbstractProcess implements ParamProcess {
053        /** 実行しているサーバーの名称 */
054        private static final String HOST_NAME ;
055        /** 実行しているサーバーのIPアドレス */
056        private static final String HOST_ADRS ;
057
058        private ApplicationInfo appInfo = null;
059
060        // 5.3.4.0 (2011/04/01) bulkData 関係のメソッドを追加
061        private Set<String> bulkData ;
062
063        private static final Map<String,String> mustProparty   ;        // [プロパティ]必須チェック用 Map
064        private static final Map<String,String> usableProparty ;        // [プロパティ]整合性チェック Map
065
066        static {
067                String dmnHost ;
068                String dnmAdrs ;
069                try {
070                        InetAddress address = InetAddress.getLocalHost();
071                        dmnHost = address.getHostName() ;
072                        dnmAdrs = address.getHostAddress() ;
073                }
074                catch( UnknownHostException ex ) {
075                        dmnHost = "Unknown";
076                        dnmAdrs = "Unknown";
077                }
078                HOST_NAME = dmnHost;
079                HOST_ADRS = dnmAdrs;
080        }
081
082        static {
083                mustProparty = new LinkedHashMap<String,String>();
084                mustProparty.put( "infoUSER",   "DB接続履歴取得用の実行ユーザー" );
085                mustProparty.put( "infoPGID",   "DB接続履歴取得用の実行プログラムID" );
086                mustProparty.put( "configFile", "DB接続情報設定 XMLファイル" );
087
088                usableProparty = new LinkedHashMap<String,String>();
089        }
090
091        /**
092         * デフォルトコンストラクター。
093         * このクラスは、動的作成されます。デフォルトコンストラクターで、
094         * super クラスに対して、必要な初期化を行っておきます。
095         *
096         */
097        public Process_DBParam() {
098                super( "org.opengion.fukurou.process.Process_DBParam",mustProparty,usableProparty );
099        }
100
101        /**
102         * ApplicationInfoオブジェクトを登録します。
103         * これは、通常の初期処理ではなく、タグリブから起動される場合のみ
104         * 呼ばれるメソッドです。
105         * 初期処理メソッド(init)では、appInfo がセット済みの場合は、
106         * ConnectionFactoryの初期化を行いません。
107         *
108         * @og.rev 4.3.1.1 (2008/09/04) 新規追加(taglib呼出専用)
109         *
110         * @param   appInfo アプリ情報オブジェクト
111         */
112        public void setAppInfo( final ApplicationInfo appInfo ) {
113                this.appInfo = appInfo;
114        }
115
116        /**
117         * プロセスの初期化を行います。初めに一度だけ、呼び出されます。
118         * 初期処理(ファイルオープン、DBオープン等)に使用します。
119         *
120         * @og.rev 4.3.1.1 (2008/09/04) taglib呼出時は、ConnectionFactoryの初期化を行わない
121         *
122         * @param   paramProcess データベースの接続先情報などを持っているオブジェクト
123         */
124        public void init( final ParamProcess paramProcess ) {
125                // 4.3.1.1 (2008/09/04) taglib呼出時は、ConnectionFactoryの初期化を行わない
126                if( appInfo == null ) {
127                        Argument arg = getArgument();
128
129                        String infoUSER = arg.getProparty( "infoUSER" );        // DB接続履歴取得用の実行ユーザー
130                        String infoPGID = arg.getProparty( "infoPGID" );        // DB接続履歴取得用の実行プログラムID
131                        String configFile = arg.getProparty( "configFile" );    // DB接続情報設定 XMLファイル
132
133                        appInfo = new ApplicationInfo();
134                        // JavaVM 起動時のユーザーID,IPアドレス,ホスト名をセットします。
135                        appInfo.setClientInfo( infoUSER,HOST_ADRS,HOST_NAME );
136
137                        // 画面ID,操作,プログラムID
138                        appInfo.setModuleInfo( infoPGID,null,"fukurou" );
139
140                        // DBID接続情報の取得先の設定
141                        ConnectionFactory.init( null,configFile );
142                }
143        }
144
145        /**
146         * 指定の 接続先ID に対する コネクションを返します。
147         *
148         * @param       key     接続先ID
149         *
150         * @return      コネクション
151         * @throws      RuntimeException DB接続先が未設定の場合
152         */
153        public Connection getConnection( final String key ) {
154                return ConnectionFactory.connection( key,appInfo );
155        }
156
157        /**
158         * 検索した結果が設定された Set オブジェクトを設定します。
159         *
160         * @og.rev 5.3.4.0 (2011/04/01) 新規追加
161         *
162         * @param       bulkData        検索した結果が設定された Setオブジェクト
163         */
164        public void setBulkData( final Set<String> bulkData ) {
165                this.bulkData = bulkData;
166        }
167
168        /**
169         * 検索した結果が設定された Set オブジェクトを返します。
170         *
171         * @og.rev 5.3.4.0 (2011/04/01) 新規追加
172         *
173         * @return      検索した結果が設定された Setオブジェクト
174         */
175        public Set<String> getBulkData() {
176                return bulkData ;
177        }
178
179        /**
180         * プロセスの終了を行います。最後に一度だけ、呼び出されます。
181         * 終了処理(ファイルクローズ、DBクローズ等)に使用します。
182         *
183         * @og.rev 4.0.0.0 (2007/11/27) commit,rollback,remove 処理を追加
184         *
185         * @param   isOK トータルで、OKだったかどうか[true:成功/false:失敗]
186         */
187        public void end( final boolean isOK ) {
188                // 何もありません。(PMD エラー回避)
189        }
190
191        /**
192         * プロセスの処理結果のレポート表現を返します。
193         * 処理プログラム名、入力件数、出力件数などの情報です。
194         * この文字列をそのまま、標準出力に出すことで、結果レポートと出来るような
195         * 形式で出してください。
196         *
197         * @return   処理結果のレポート
198         */
199        public String report() {
200                String report = "[" + getClass().getName() + "]" + CR
201                                + ConnectionFactory.information();
202
203                return report ;
204        }
205
206        /**
207         * このクラスの使用方法を返します。
208         *
209         * @return      このクラスの使用方法
210         */
211        public String usage() {
212                StringBuilder buf = new StringBuilder();
213
214                buf.append( "Process_DBParam は、他のプロセスへ共通のデータベース接続を割り当てる為の、"     ).append( CR );
215                buf.append( "ParamProcess インターフェースの実装クラスです。"                                                            ).append( CR );
216                buf.append( CR );
217                buf.append( "DB接続 が必要な Process (DBCountFilter、DBMerge、DBReader、DBWriterなど)を"    ).append( CR );
218                buf.append( "使用して処理する場合に、接続を指定することができます。"                                               ).append( CR );
219                buf.append( "DBID(接続先) は、-configFile で指定する DBConfig.xml ファイルを使用します。"    ).append( CR );
220                buf.append( CR );
221                buf.append( "引数文字列中に空白を含む場合は、ダブルコーテーション(\"\") で括って下さい。" ).append( CR );
222                buf.append( "引数文字列の 『=』の前後には、空白は挟めません。必ず、-key=value の様に"                ).append( CR );
223                buf.append( "繋げてください。"                                                                                                                          ).append( CR );
224                buf.append( CR ).append( CR );
225
226                buf.append( getArgument().usage() ).append( CR );
227
228                return buf.toString();
229        }
230
231        /**
232         * このクラスは、main メソッドから実行できません。
233         *
234         * @param       args    コマンド引数配列
235         */
236        public static void main( final String[] args ) {
237                LogWriter.log( new Process_DBParam().usage() );
238        }
239}