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.taglib;
017
018import org.opengion.hayabusa.common.HybsSystemException;
019
020import static org.opengion.fukurou.util.StringUtil.nval ;
021import org.opengion.fukurou.util.StringUtil;
022import org.opengion.fukurou.process.HybsProcess;
023import org.opengion.fukurou.process.Process_DBParam;
024
025import java.io.ObjectOutputStream;
026import java.io.ObjectInputStream;
027import java.io.IOException;
028
029/**
030 * MainProcess で実行される Processクラスを構築します。
031 *
032 * 親クラス(Process)は、org.opengion.fukurou.process パッケージの HybsProcess
033 * インターフェースを実装したクラスの、Process_****.java の **** 部分を指定します。
034 * 共通的な パラメータは、このTagクラスに実装しますが、それぞれ、個別に必要な
035 * パラメータは、ParamTag を使用して指定します。
036 * このタグは、MainProcess タグの内部にのみ、記述可能です。
037 *
038 * @og.formSample
039 * ●形式:<og:process processID="ZZZ" >
040 *             <og:param key="AAA" value="111" />
041 *         </og:process >
042 * ●body:あり(EVAL_BODY_BUFFERED:BODYを評価し、{@XXXX} を解析します)
043 *
044 * ●Tag定義:
045 *   <og:process
046 *       processID        ○【TAG】リクエスト情報 に登録するキーをセットします(必須)。
047 *       debug              【TAG】デバッグ情報を出力するかどうか[true/false]を指定します(初期値:false)
048 *   >   ... Body ...
049 *   </og:process>
050 *
051 * ●使用例
052 *     一般的な変数の渡し方
053 *   <og:mainProcess >
054 *     <og:process processID="DBReader" >
055 *        <og:param key="dbid" value="FROM" />
056 *        <og:param key="sql"  value="select * from GE02" />
057 *     </og:process >
058 *     <og:process processID="DBWriter" >
059 *        <og:param key="dbid"  value="TO" />
060 *        <og:param key="table" value="GE02" />
061 *     </og:process >
062 *   </og:mainProcess >
063 *
064 *     BODY 部に記述した変数の渡し方
065 *     <og:process processID="DBReader" >
066 *        <og:param key="SQL" >
067 *              SELECT COUNT(*) FROM GEA03
068 *              WHERE SYSTEM_ID=[SYSTEM_ID]
069 *              AND   CLM=[CLM]
070 *              AND   FGJ = '1'
071 *        </og:param>
072 *     </og:process >
073 *
074 * @og.group リアルバッチ系
075 *
076 * @version  4.0
077 * @author       Kazuhiko Hasegawa
078 * @since    JDK5.0,
079 */
080public class ProcessTag extends CommonTagSupport {
081        //* このプログラムのVERSION文字列を設定します。   {@value} */
082        private static final String VERSION = "4.3.1.1 (2008/09/04)" ;
083
084        private static final long serialVersionUID = 431120080904L ;
085
086        private static final String PRCS = "org.opengion.fukurou.process.Process_" ;
087
088        private String          processID       = null;
089        private transient HybsProcess   process         = null;
090
091        /**
092         * Taglibの開始タグが見つかったときに処理する doStartTag() を オーバーライドします。
093         *
094         * @og.rev 4.3.1.1 (2008/09/04) DBParam 使用時は、専用の初期化メソッドを呼ぶ
095         *
096         * @return      後続処理の指示( EVAL_BODY_BUFFERED )
097         */
098        @Override
099        public int doStartTag() {
100                MainProcessTag mainProcess = (MainProcessTag)findAncestorWithClass( this,MainProcessTag.class );
101                if( mainProcess == null ) {
102                        String errMsg = "<b>" + getTagName() + "タグは、MainProcessTagの内側(要素)に記述してください。</b>"
103                                                                + "processID=" + processID ;
104                        throw new HybsSystemException( errMsg );
105                }
106
107                // 4.3.1.1 (2008/09/04) DBParam 使用時は、専用の初期化メソッドを呼ぶ
108                if( "DBParam".equals( processID ) ) {
109                        process = new Process_DBParam();
110                        ((Process_DBParam)process).setAppInfo( getApplicationInfo() );
111                }
112                else {
113                        process = (HybsProcess)StringUtil.newInstance( PRCS + processID );
114                }
115
116                mainProcess.addProcess( process );
117
118                return EVAL_BODY_BUFFERED ;             // Body を評価する
119        }
120
121        /**
122         * タグリブオブジェクトをリリースします。
123         * キャッシュされて再利用されるので、フィールドの初期設定を行います。
124         *
125         */
126        @Override
127        protected void release2() {
128                super.release2();
129                processID       = null;
130                process         = null;
131        }
132
133        /**
134         * 【TAG】リクエスト情報 に登録するキーをセットします。
135         *
136         * @og.tag
137         * processID は、org.opengion.fukurou.process.HybsProcess インターフェースを実装した
138         * Process_**** クラスの **** を与えます。
139         * これらは、HybsProcess インターフェースを継承したサブクラスである必要があります。
140         * 標準で、org.opengion.fukurou.process 以下の Process_**** クラスが、Process_**** 宣言 と
141         * して、定義されています。
142         * 属性クラス定義の {@link org.opengion.fukurou.process.HybsProcess HybsProcess} を参照願います。
143         * {@og.doc03Link process Process_**** クラス}
144         *
145         * @param       pid リクエスト情報に登録するキー
146         * @see         org.opengion.fukurou.process.HybsProcess  HybsProcessのサブクラス
147         */
148        public void setProcessID( final String pid ) {
149                processID = nval( getRequestParameter( pid ),processID ) ;
150        }
151
152        /**
153         * 親クラスに登録するキーをセットします。
154         *
155         * @param       key             登録するキー
156         * @param       value   登録する値
157         */
158        protected void addParam( final String key,final String value ) {
159                process.putArgument( key,value );
160        }
161
162        /**
163         * シリアライズ用のカスタムシリアライズ書き込みメソッド
164         *
165         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
166         * @serialData 一部のオブジェクトは、シリアライズされません。
167         *
168         * @param       strm    ObjectOutputStreamオブジェクト
169         * @throws IOException  入出力エラーが発生した場合
170         */
171        private void writeObject( final ObjectOutputStream strm ) throws IOException {
172                strm.defaultWriteObject();
173        }
174
175        /**
176         * シリアライズ用のカスタムシリアライズ読み込みメソッド
177         *
178         * ここでは、transient 宣言された内部変数の内、初期化が必要なフィールドのみ設定します。
179         *
180         * @og.rev 4.0.0.0 (2006/09/31) 新規追加
181         * @serialData 一部のオブジェクトは、シリアライズされません。
182         *
183         * @param       strm    ObjectInputStreamオブジェクト
184         * @see #release2()
185         * @throws IOException  シリアライズに関する入出力エラーが発生した場合
186         * @throws ClassNotFoundException       クラスを見つけることができなかった場合
187         */
188        private void readObject( final ObjectInputStream strm ) throws IOException , ClassNotFoundException {
189                strm.defaultReadObject();
190        }
191
192        /**
193         * このオブジェクトの文字列表現を返します。
194         * 基本的にデバッグ目的に使用します。
195         *
196         * @return このクラスの文字列表現
197         */
198        @Override
199        public String toString() {
200                return org.opengion.fukurou.util.ToString.title( this.getClass().getName() )
201                                .println( "VERSION"                             ,VERSION                        )
202                                .println( "processID"                   ,processID                      )
203                                .fixForm().toString() ;
204        }
205}