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.db.ApplicationInfo;
021import org.opengion.fukurou.system.LogWriter;
022import org.opengion.fukurou.system.HybsConst;                                           // 8.0.0.0 (2021/07/31) HOST_NAME と HOST_ADRS は、HybsConst に移植
023
024import java.util.Set ;
025import java.util.Map ;
026import java.util.LinkedHashMap ;
027// import java.net.InetAddress;                                                                         // 8.0.0.0 (2021/07/31) Delete
028// import java.net.UnknownHostException;
029// import java.net.NetworkInterface;                                                            // 7.3.1.1 (2021/02/25) 8.0.0.0 (2021/07/31) Delete
030// import java.util.Enumeration;                                                                        // 7.3.1.1 (2021/02/25) 8.0.0.0 (2021/07/31) Delete
031
032import java.sql.Connection;
033
034/**
035 * Process_DBParam は、他のプロセスへ共通のデータベース接続を割り当てる為の、
036 * ParamProcess インターフェースの実装クラスです。
037 *
038 * DB接続 が必要な Process (DBCountFilter、DBMerge、DBReader、DBWriterなど)を
039 * 使用して処理する場合に、接続を指定することができます。
040 * DBID(接続先) は、Process_DBParam の -configFile で指定する DBConfig.xml ファイルを使用します。
041 *
042 * @og.formSample
043 *  Process_DBParam -infoUSER=C00000 -infoPGID=GE1234 -configFile=DBConfig.xml
044 *
045 *   [ -infoUSER=実行ユーザー       ] : DB接続履歴取得用の実行ユーザー(例:C00000)
046 *   [ -infoPGID=実行プログラムID   ] : DB接続履歴取得用の実行プログラムID(例:GE1234)
047 *   [ -configFile=実行プログラムID ] : DB接続情報設定 XMLファイル(例:DBConfig.xml)
048 *   [ -display=[false/true]        ] : trueは、接続状況を詳細表示します(初期値:false)
049 *
050 * @og.rev 4.0.0.0 (2007/11/22) DBConfig.xml による DBID(接続先)指定に変更。
051 * @og.rev 6.3.1.0 (2015/06/28) 履歴取得用パラメータの必須解除
052 *
053 * @version  4.0
054 * @author   Kazuhiko Hasegawa
055 * @since    JDK5.0,
056 */
057public class Process_DBParam extends AbstractProcess implements ParamProcess {
058//      /** 実行しているサーバーの名称 */
059//      private static final String HOST_NAME ;
060//      /** 実行しているサーバーのIPアドレス */
061//      private static final String HOST_ADRS ;
062
063//      static {
064//              String dmnHost ;
065//              String dnmAdrs ;
066//              try {
067//                      final InetAddress address = InetAddress.getLocalHost();
068//                      dmnHost = address.getHostName() ;
069//                      dnmAdrs = address.getHostAddress() ;
070//              }
071//              catch( final UnknownHostException ex ) {
072//                      dmnHost = "Unknown";
073//                      dnmAdrs = "Unknown";
074//              }
075//              HOST_NAME = dmnHost;
076//              HOST_ADRS = dnmAdrs;
077//      }
078
079//      /**
080//       * ホスト名と、IPアドレスを取得
081//       *
082//       * Java VM が実行しているホスト名と、IPアドレスを取得します。
083//       * InetAddress.getLocalHost().getHostName() で求められる値は、Virtual アドレスなどの
084//       * 複数考えられる為、出来るだけ直接設定されているIPアドレスに近い値を取得するようにします。
085//       * でも、完全には特定できないと思われます。
086//       *
087//       * @og.rev 7.3.1.1 (2021/02/25) ホスト名と、IPアドレスを取得
088//       * @og.rev 8.0.0.0 (2021/07/31) fukurou.system.HybsConst に移植
089//       */
090//      static {
091//              String dmnHost = "Unknown" ;
092//              String dmnAdrs = "Unknown" ;
093//              try {
094//                      boolean isNext = true;
095//                      final Enumeration<NetworkInterface> enuIfs = NetworkInterface.getNetworkInterfaces();
096//                      while( isNext && enuIfs.hasMoreElements() ) {
097//                              final NetworkInterface ni = enuIfs.nextElement();
098//
099//                              final String displayName = ni.getDisplayName();
100//                              if( displayName.contains("Virtual") ) { continue; }
101//
102//                              final Enumeration<InetAddress> enuIP = ni.getInetAddresses();
103//                              while( isNext && enuIP.hasMoreElements() ) {
104//                                      final InetAddress adrs = enuIP.nextElement();
105//
106//                                      if( adrs.isLinkLocalAddress() || adrs.isLoopbackAddress() ) { continue; }
107//
108//                                      dmnHost = adrs.getHostName() ;                  // adrs.getCanonicalHostName() はとりあえず使わないでおく。
109//                                      dmnAdrs = adrs.getHostAddress() ;
110//                                      isNext = false;
111//      //                              break;                                                                  // Avoid using a branching statement as the last in a loop. ()
112//                              }
113//                      }
114//              }
115//              catch( final Throwable th ) {
116//                      System.err.println( "HOST_NAME and HOST_ADRS Unknown!" );
117//              }
118//              HOST_NAME = dmnHost;
119//              HOST_ADRS = dmnAdrs;
120//      }
121
122        private ApplicationInfo appInfo ;
123        private boolean                 display ;                       // 表示しない
124
125        // 5.3.4.0 (2011/04/01) bulkData 関係のメソッドを追加
126        private Set<String> bulkData ;
127
128        /** staticイニシャライザ後、読み取り専用にするので、ConcurrentHashMap を使用しません。 */
129        private static final Map<String,String> MUST_PROPARTY   ;       // [プロパティ]必須チェック用 Map
130        /** staticイニシャライザ後、読み取り専用にするので、ConcurrentHashMap を使用しません。 */
131        private static final Map<String,String> USABLE_PROPARTY ;       // [プロパティ]整合性チェック Map
132
133        static {
134                MUST_PROPARTY = new LinkedHashMap<>();
135                // 6.3.1.0 (2015/06/28) 必須から外します。
136
137                USABLE_PROPARTY = new LinkedHashMap<>();
138                USABLE_PROPARTY.put( "infoUSER" , "DB接続履歴取得用の実行ユーザー" );
139                USABLE_PROPARTY.put( "infoPGID" , "DB接続履歴取得用の実行プログラムID" );
140                USABLE_PROPARTY.put( "configFile", "DB接続情報設定 XMLファイル" );
141                USABLE_PROPARTY.put( "display"  , "trueは、接続状況を詳細表示します(初期値:false)" );            // 6.3.1.0 (2015/06/28) 追加
142        }
143
144        /**
145         * デフォルトコンストラクター。
146         * このクラスは、動的作成されます。デフォルトコンストラクターで、
147         * super クラスに対して、必要な初期化を行っておきます。
148         *
149         */
150        public Process_DBParam() {
151                super( "org.opengion.fukurou.process.Process_DBParam",MUST_PROPARTY,USABLE_PROPARTY );
152        }
153
154        /**
155         * ApplicationInfoオブジェクトを登録します。
156         * これは、通常の初期処理ではなく、タグリブから起動される場合のみ
157         * 呼ばれるメソッドです。
158         * 初期処理メソッド(init)では、appInfo がセット済みの場合は、
159         * ConnectionFactoryの初期化を行いません。
160         *
161         * @og.rev 4.3.1.1 (2008/09/04) 新規追加(taglib呼出専用)
162         *
163         * @param   appInfo アプリ情報オブジェクト
164         */
165        public void setAppInfo( final ApplicationInfo appInfo ) {
166                this.appInfo = appInfo;
167        }
168
169        /**
170         * プロセスの初期化を行います。初めに一度だけ、呼び出されます。
171         * 初期処理(ファイルオープン、DBオープン等)に使用します。
172         *
173         * @og.rev 4.3.1.1 (2008/09/04) taglib呼出時は、ConnectionFactoryの初期化を行わない
174         * @og.rev 6.3.1.0 (2015/06/28) display属性の追加
175         *
176         * @param   paramProcess データベースの接続先情報などを持っているオブジェクト
177         */
178        @Override
179        public void init( final ParamProcess paramProcess ) {
180                final Argument arg = getArgument();                     // 6.3.1.0 (2015/06/28) display属性の追加のため。
181
182                // 4.3.1.1 (2008/09/04) taglib呼出時は、ConnectionFactoryの初期化を行わない
183                if( appInfo == null ) {
184
185                        final String infoUSER   = arg.getProparty( "infoUSER" );                // DB接続履歴取得用の実行ユーザー
186                        final String infoPGID   = arg.getProparty( "infoPGID" );                // DB接続履歴取得用の実行プログラムID
187                        final String configFile = arg.getProparty( "configFile" );              // DB接続情報設定 XMLファイル
188
189                        appInfo = new ApplicationInfo();
190                        // JavaVM 起動時のユーザーID,IPアドレス,ホスト名をセットします。
191//                      appInfo.setClientInfo( infoUSER,HOST_ADRS,HOST_NAME );
192                        appInfo.setClientInfo( infoUSER,HybsConst.HOST_ADRS,HybsConst.HOST_NAME );              // 8.0.0.0 (2021/07/31)
193
194                        // 画面ID,操作,プログラムID
195                        appInfo.setModuleInfo( infoPGID,null,"fukurou" );
196
197                        // DBID接続情報の取得先の設定
198                        ConnectionFactory.init( null,configFile );
199                }
200
201                display = arg.getProparty( "display",display );         // 6.3.1.0 (2015/06/28)
202        }
203
204        /**
205         * 指定の 接続先ID に対する コネクションを返します。
206         *
207         * @param       key     接続先ID
208         *
209         * @return      コネクション
210         * @throws      RuntimeException DB接続先が未設定の場合
211         * @og.rtnNotNull
212         */
213        @Override       // ParamProcess
214        public Connection getConnection( final String key ) {
215                return ConnectionFactory.connection( key,appInfo );
216        }
217
218        /**
219         * 検索した結果が設定された Set オブジェクトを設定します。
220         *
221         * @og.rev 5.3.4.0 (2011/04/01) 新規追加
222         *
223         * @param       bulkData        検索した結果が設定されたSetオブジェクト
224         */
225        @Override       // ParamProcess
226        public void setBulkData( final Set<String> bulkData ) {
227                this.bulkData = bulkData;
228        }
229
230        /**
231         * 検索した結果が設定された Set オブジェクトを返します。
232         *
233         * @og.rev 5.3.4.0 (2011/04/01) 新規追加
234         *
235         * @return      検索した結果が設定された Setオブジェクト
236         */
237        @Override       // ParamProcess
238        public Set<String> getBulkData() {
239                return bulkData ;
240        }
241
242        /**
243         * プロセスの終了を行います。最後に一度だけ、呼び出されます。
244         * 終了処理(ファイルクローズ、DBクローズ等)に使用します。
245         *
246         * @og.rev 4.0.0.0 (2007/11/27) commit,rollback,remove 処理を追加
247         *
248         * @param   isOK トータルで、OKだったかどうか[true:成功/false:失敗]
249         */
250        @Override
251        public void end( final boolean isOK ) {
252                // 何もありません。(PMD エラー回避)
253        }
254
255        /**
256         * プロセスの処理結果のレポート表現を返します。
257         * 処理プログラム名、入力件数、出力件数などの情報です。
258         * この文字列をそのまま、標準出力に出すことで、結果レポートと出来るような
259         * 形式で出してください。
260         *
261         * @return   処理結果のレポート
262         */
263        @Override
264        public String report() {
265                // 7.2.9.5 (2020/11/28) PMD:Consider simply returning the value vs storing it in local variable 'XXXX'
266                return "[" + getClass().getName() + "]" + CR
267//              final String report = "[" + getClass().getName() + "]" + CR
268                                                        + ConnectionFactory.information( display ) ;
269
270//              return report ;
271        }
272
273        /**
274         * このクラスの使用方法を返します。
275         *
276         * @return      このクラスの使用方法
277         * @og.rtnNotNull
278         */
279        @Override
280        public String usage() {
281                final StringBuilder buf = new StringBuilder( BUFFER_LARGE )
282                        .append( "Process_DBParam は、他のプロセスへ共通のデータベース接続を割り当てる為の、"        ).append( CR )
283                        .append( "ParamProcess インターフェースの実装クラスです。"                                                               ).append( CR )
284                        .append( CR )
285                        .append( "DB接続 が必要な Process (DBCountFilter、DBMerge、DBReader、DBWriterなど)を"       ).append( CR )
286                        .append( "使用して処理する場合に、接続を指定することができます。"                                          ).append( CR )
287                        .append( "DBID(接続先) は、-configFile で指定する DBConfig.xml ファイルを使用します。"       ).append( CR )
288                        .append( CR )
289                        .append( "引数文字列中に空白を含む場合は、ダブルコーテーション(\"\") で括って下さい。"    ).append( CR )
290                        .append( "引数文字列の 『=』の前後には、空白は挟めません。必ず、-key=value の様に"           ).append( CR )
291                        .append( "繋げてください。"                                                                                                                             ).append( CR )
292                        .append( CR )
293                        .append( "[ -infoUSER=実行ユーザー       ] : DB接続履歴取得用の実行ユーザー(例:C00000)"              ).append( CR )
294                        .append( "[ -infoPGID=実行プログラムID   ] : DB接続履歴取得用の実行プログラムID(例:GE1234)"    ).append( CR )
295                        .append( "[ -configFile=実行プログラムID ] : DB接続情報設定 XMLファイル(例:DBConfig.xml)"         ).append( CR )
296                        .append( "[ -display=[false/true]        ] : trueは、接続状況を詳細表示します(初期値:false)"     ).append( CR )
297                        .append( CR ).append( CR )
298                        .append( getArgument().usage() ).append( CR );
299
300                return buf.toString();
301        }
302
303        /**
304         * このクラスは、main メソッドから実行できません。
305         *
306         * @param       args    コマンド引数配列
307         */
308        public static void main( final String[] args ) {
309                LogWriter.log( new Process_DBParam().usage() );
310        }
311}