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.process;
017    
018    import org.opengion.fukurou.db.ConnectionFactory;
019    import org.opengion.fukurou.util.Argument;
020    import org.opengion.fukurou.util.ApplicationInfo;
021    import org.opengion.fukurou.util.LogWriter;
022    
023    import java.util.Set ;
024    import java.util.Map ;
025    import java.util.LinkedHashMap ;
026    import java.net.InetAddress;
027    import java.net.UnknownHostException;
028    
029    import 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=実行?ログラ?D     ??DB接続履歴取得用の実行?ログラ?D(?GE1234)
044     *     -configFile=実行?ログラ?D   ??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     */
052    public 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            static {
059                    String dmnHost ;
060                    String dnmAdrs ;
061                    try {
062                            InetAddress address = InetAddress.getLocalHost();
063                            dmnHost = address.getHostName() ;
064                            dnmAdrs = address.getHostAddress() ;
065                    }
066                    catch( UnknownHostException ex ) {
067                            dmnHost = "Unknown";
068                            dnmAdrs = "Unknown";
069                    }
070                    HOST_NAME = dmnHost;
071                    HOST_ADRS = dnmAdrs;
072            }
073    
074            private ApplicationInfo appInfo = null;
075    
076            // 5.3.4.0 (2011/04/01) bulkData 関係?メソ?を追?
077            private Set<String> bulkData ;
078    
079            private static final Map<String,String> mustProparty   ;  // ?プロパティ???チェ?用 Map
080            private static final Map<String,String> usableProparty ;  // ?プロパティ?整合?チェ? Map
081    
082            static {
083                    mustProparty = new LinkedHashMap<String,String>();
084                    mustProparty.put( "infoUSER",   "DB接続履歴取得用の実行ユーザー" );
085                    mustProparty.put( "infoPGID",   "DB接続履歴取得用の実行?ログラ?D" );
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             * 初期処?ファイルオープン??オープン?に使用します?
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接続履歴取得用の実行?ログラ?D
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,操?プログラ?D
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             * 終???ファイルクローズ??クローズ?に使用します?
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    }