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.util.Argument;
019import org.opengion.fukurou.system.HybsConst;                           // 6.1.0.0 (2014/12/26) refactoring
020
021import java.util.Map;
022
023/**
024 * AbstractProcess は、ChainProcess インターフェースを実装した、Abstract クラスです。
025 * ChainProcess を用いて、順次、バッチプロセスを実行することができます。
026 *
027 * @version  4.0
028 * @author   Kazuhiko Hasegawa
029 * @since    JDK5.0,
030 */
031public abstract class AbstractProcess implements HybsProcess {
032
033        /** システムの改行コードを設定します。*/
034        protected static final String CR                 = HybsConst.CR;                        // 6.1.0.0 (2014/12/26) refactoring
035        /** StringBilderなどの初期値を設定します。   {@value} */
036        protected static final int BUFFER_MIDDLE = HybsConst.BUFFER_MIDDLE;     // 6.1.0.0 (2014/12/26) refactoring
037        /** StringBilderなどの初期値を設定します。   {@value} */
038        protected static final int BUFFER_LARGE  = HybsConst.BUFFER_LARGE;      // 6.1.0.0 (2014/12/26) refactoring
039
040        /** タブセパレータ  */
041        public static final char TAB = '\t';    // 6.0.2.5 (2014/10/31) タブ区切り文字を char 化します。
042
043        private final Argument argments ;
044        private LoggerProcess logger    ;
045
046        /**
047         * コンストラクター
048         *
049         * @param       name                    このクラス(サブクラス)のクラス名称
050         * @param   MUST_PROPARTY       必須チェックMap
051         * @param   USABLE_PROPARTY     整合性チェックMap
052         */
053        public AbstractProcess( final String name , final Map<String,String> MUST_PROPARTY ,final Map<String,String> USABLE_PROPARTY ) {
054                argments = new Argument( name ) ;
055                argments.setMustProparty( MUST_PROPARTY );
056                argments.setUsableProparty( USABLE_PROPARTY );
057        }
058
059        /**
060         * 引数形式を解析する 引数オブジェクトに、引数を設定します。
061         * Argument の文字列から、引数かプロパティをセットします。
062         * [プロパティ]のキー部の大文字・小文字は、厳格に判定しています。
063         * Argument の文字列には、タイプがあります。
064         *
065         * [コメント]  : # で始まる引数で、使用されません。(登録もされません。)
066         * [引数]      : #,-,= 以外で始まる通常の文字列。登録の順番が指定されます。
067         * [プロパティ]: - で始まり、キーと値を=で区切っているパラメータです。順序は無関係。
068         *
069         * @param   arg 引数
070         */
071        public void putArgument( final String arg ) {
072                argments.putArgument( arg ) ;
073        }
074
075        /**
076         * Argument の文字列から、プロパティをセットします。
077         * [プロパティ]のキー部の大文字・小文字は、厳格に判定しています。
078         * このメソッドは、引数 や コメントの判断を行いません。プロパティ のみ
079         * 設定されるものとして、処理します。
080         * プロパティの key=val が初めから分割されている場合の簡易メソッドです。
081         *
082         * @param   key キー
083         * @param   val 値
084         */
085        public void putArgument( final String key,final String val ) {
086                argments.putArgument( key,val ) ;
087        }
088
089        /**
090         * 引数形式を解析する 引数オブジェクトを返します。
091         *
092         * @return      引数オブジェクト
093         */
094        public Argument getArgument() {
095                return argments ;
096        }
097
098        /**
099         * ディスプレイにメッセージを表示します。
100         *
101         * @param       msg     表示するメッセージ
102         */
103        public void println( final String msg ) {
104                if( logger != null ) {
105                        logger.println( msg ) ;
106                }
107        }
108
109        /**
110         * ディスプレイにメッセージを表示します。
111         *
112         * @param       msg     表示するメッセージ
113         */
114        public void logging( final String msg ) {
115                if( logger != null ) {
116                        logger.logging( msg ) ;
117                }
118        }
119
120        /**
121         * ディスプレイ出力する LoggerProcess オブジェクトをセットします。
122         *
123         * @param logger LoggerProcessオブジェクト
124         */
125        public final void setLoggerProcess( final LoggerProcess logger ) {
126                this.logger = logger ;
127        }
128
129        /**
130         * エラー発生時に、RuntimeException を throw するか、ログ出力します。
131         * 引数に、エラーメッセージを指定します。
132         * isAbend引数は、RuntimeException を throw するか、ログ出力するかを指定する
133         * 引数です。true で、RuntimeException を throw し、false で、ログ出力
134         * のみとします。(つまり、継続処理されます。)
135         *
136         * @og.rev 6.3.1.1 (2015/07/10) RuntimeException を throwする機能を追加
137         *
138         * @param       errMsg  エラーメッセージ
139         * @param       isAbend 異常発生時に、処理を中断(true)するか、継続(false)するか
140         */
141        public void throwException( final String errMsg , final boolean isAbend ) {
142                throwException( errMsg,null,isAbend );
143        }
144
145        /**
146         * エラー発生時に、RuntimeException を throw するか、ログ出力します。
147         * 引数に、エラーメッセージと、発生元 Throwable を指定します。
148         * 発生元 Throwable が null の場合は、エラーメッセージのみで throw します。
149         * isAbend引数は、RuntimeException を throw するか、ログ出力するかを指定する
150         * 引数です。true で、RuntimeException を throw し、false で、ログ出力
151         * のみとします。(つまり、継続処理されます。)
152         *
153         * @og.rev 6.3.1.1 (2015/07/10) RuntimeException を throwする機能を追加
154         *
155         * @param       errMsg  エラーメッセージ
156         * @param       th              発生元 Throwable
157         * @param       isAbend 異常発生時に、処理を中断(true)するか、継続(false)するか
158         */
159        public void throwException( final String errMsg,final Throwable th,final boolean isAbend ) {
160                if( isAbend ) {
161                        throw th == null ? new RuntimeException( errMsg ) : new RuntimeException( errMsg,th );
162                }
163                else {
164                        logging( "=================================================================" );
165                        if( errMsg != null ) { logging( errMsg ); }
166                        if( th     != null ) { logging( th.getMessage() ); }
167                }
168        }
169
170        /**
171         * プロセスの内容表示を行います。
172         * Argument#toString() を呼び出しています。
173         *
174         * @return 内容表示
175         */
176        @Override
177        public String toString() {
178                return argments.toString();
179        }
180}