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.transfer;
017    
018    import java.util.Date;
019    import java.util.HashSet;
020    import java.util.Set;
021    
022    import org.opengion.fukurou.db.ConnectionFactory;
023    import org.opengion.fukurou.db.Transaction;
024    import org.opengion.fukurou.db.TransactionReal;
025    import org.opengion.fukurou.util.ApplicationInfo;
026    import org.opengion.fukurou.util.LogWriter;
027    import org.opengion.fukurou.util.StringUtil;
028    
029    /**
030     * 伝é?処ç�?‚’実行するためã?クラスですã?
031     *
032     * 伝é?ãƒ??モンでセãƒ?ƒˆされた読取方法ã?実行方法ã?基づき伝é?処ç�?‚’実行しますã?
033     * 伝é?処ç�??以下ã?フローで実行されますã?
034     *
035     * â‘?ƒ‡ータの読み取り
036     * ã€?ª­取方法に対応する各実è£?‚¯ラスの処ç�?‚’呼び出しデータを読み取りますã?
037     * ②処ç�?®Ÿè¡?
038     * ã€?®Ÿ行方法に対応する各実è£?‚¯ラスの処ç�?‚’呼び出し①で取得したデータに対して処ç�?‚’実行しますã?
039     * ③終äº??ç�?
040     * ã€?‘ 、②が正常終äº?�—たå?合,読取方法ã?クラスで定義された終äº??ç�?‚’実行しますã?
041     * ã€?‘ 、②でエラーが発生したå?合ã?読取方法ã?クラスで定義されたエラー処ç�?‚’実行しますã?
042     *
043     * @og.group 伝é?シスãƒ?ƒ 
044     *
045     * @version  5.0
046     * @author   Hiroki.Nakamura
047     * @since    JDK1.6
048     */
049    public class TransferProcess {
050    
051            // 実行方法に対応する実è£?‚¯ラスの基準名
052            private static final String READ_CLASS_BASE = "org.opengion.fukurou.transfer.TransferRead_" ;
053    
054            // 実行方法に対応する実è£?‚¯ラスの基準名
055            private static final String EXEC_CLASS_BASE = "org.opengion.fukurou.transfer.TransferExec_" ;
056    
057            // 実行対象の伝é?セãƒ?ƒˆオブジェクトã?セãƒ?ƒˆ
058            private final Set<TransferConfig> configSet;
059    
060            // æœ?¾Œに実行した伝é?設定オブジェクãƒ?
061            private TransferConfig config = null;
062    
063            // 実行デーモンå�?
064            private String dmnName = null;
065    
066            // DB接続情報記録
067            private ApplicationInfo appInfo;
068    
069            // ãƒ?ƒ�ãƒ?‚°æƒ??をå?力するかどã�?�‹
070            private boolean isDebug = false;
071    
072            /**
073             * コンストラクタですã?
074             *
075             * @param configSet 伝é?設定オブジェクトã?セãƒ?ƒˆ
076             */
077            public TransferProcess( final Set<TransferConfig> configSet ) {
078                    this.configSet = configSet;
079            }
080    
081            /**
082             * ãƒ??モン名をセãƒ?ƒˆしますã?
083             *
084             * @param dmnName ãƒ??モンå�?
085             */
086            public void setDmnName( final String dmnName ) {
087                    this.dmnName = dmnName;
088            }
089    
090            /**
091             * DB接続情報をセãƒ?ƒˆしますã?
092             *
093             * @param appInfo DB接続情報
094             */
095            public void setAppInfo( final ApplicationInfo appInfo ) {
096                    this.appInfo = appInfo;
097            }
098    
099            /**
100             * ãƒ?ƒ�ãƒ?‚°æƒ??をå?力しますã?
101             */
102            public void setDebug() {
103                    isDebug = true;
104            }
105    
106            /**
107             * æœ?¾Œに実行した伝é?設定オブジェクトを返しますã?
108             *
109             * @return 伝é?設定オブジェクãƒ?
110             */
111            public TransferConfig getLastConfig() {
112                    return config;
113            }
114    
115            /**
116             * 伝é?処ç�?‚’実行しますã?
117             */
118            public void process() {
119                    Transaction tran = new TransactionReal( appInfo );
120                    TransferRead read = null;
121                    TransferExec exec = null;
122                    try {
123                            for( TransferConfig c : configSet ) {
124                                    config = c;
125    
126                                    // ãƒ?ƒ�ãƒ?‚°æƒ??をå?力しますã?
127                                    if( isDebug ) {
128                                            System.out.println();
129                                            System.out.print( " START = " + new Date() );
130                                            System.out.print( "[" + dmnName + "]:[" + config.toString() + "]" );
131                                    }
132    
133                                    // 読取方法ã?オブジェクトを生æ?しã?伝é?プロセスにセãƒ?ƒˆしますã?
134                                    read = (TransferRead)StringUtil.newInstance( READ_CLASS_BASE + config.getKbRead() );
135    
136                                    // 実行方法ã?オブジェクトを生æ?しã?伝é?プロセスにセãƒ?ƒˆしますã?
137                                    exec = (TransferExec)StringUtil.newInstance( EXEC_CLASS_BASE + config.getKbExec() );
138    
139                                    String[] vals = read.read( config, tran );
140                                    exec.execute( vals, config, tran );
141                                    read.complete( config, tran );
142    
143                                    // ãƒ?ƒ�ãƒ?‚°æƒ??をå?力しますã?
144                                    if( isDebug ) {
145                                            System.out.println();
146                                            System.out.print( " END   = " + new Date() );
147                                            System.out.print( "[" + dmnName + "]:[" + config.toString() + "]" );
148                                    }
149                            }
150                    }
151                    catch( Throwable ex ) {
152                            if( tran != null ) {
153                                    tran.rollback();
154                                    tran.close();
155                                    tran = null; // エラー発生時は、接続を終äº?�—ますã?(次の状況更新でãƒ?ƒƒドロãƒ?‚¯になるたã‚?
156                            }
157    
158                            if( read != null ) {
159                                    read.error( config, appInfo );
160                            }
161    
162                            throw new RuntimeException( ex );
163                    }
164                    finally {
165                            if( tran != null ) { tran.close(); }
166                    }
167            }
168    
169            /**
170             * 実行用のメインメソãƒ?ƒ‰
171             *
172             * Usage: java org.opengion.hayabusa.transfer.TransferProcess
173             *  -kbRead=読取方æ³?-readObj=読取対象 [-readPrm=読取パラメーター]
174             *  -kbExec=実行方æ³?-execObj=実行対象 [-execPrm=実行パラメーター]
175             *  -DBConfig=DBConfig.xml [-execDbid=実行接続å?DBID] [-hfrom=送りå…??ストコード]
176             *
177             * @param       args    コマンド引数配å?
178             */
179            public static void main( final String[] args ) {
180                    try {
181                            if( args.length < 5 ) {
182                                    LogWriter.log( "Usage: java org.opengion.hayabusa.transfer.TransferProcess" );
183                                    LogWriter.log( "   -kbRead=読取方æ³?-readObj=読取対象 [-readPrm=読取パラメーター]" );
184                                    LogWriter.log( "   -kbExec=実行方æ³?-execObj=実行対象 [-execPrm=実行パラメーター]" );
185                                    LogWriter.log( "   -DBConfig=DBConfig.xml [-execDbid=実行接続å?DBID] [-hfrom=送りå…??ストコード]" );
186                                    return;
187                            }
188    
189                            String kbRead   = null;
190                            String readObj  = null;
191                            String readPrm  = null;
192                            String kbExec   = null;
193                            String execObj  = null;
194                            String execPrm  = null;
195                            String dbConfig = null;
196                            String execDbid = null;
197                            String hfrom    = null;
198    
199                            for( int i=0; i<args.length; i++ ) {
200                                    String arg = args[i];
201                                    if( arg.startsWith( "-kbRead=" ) ) {
202                                            kbRead = arg.substring( 8 );
203                                    }
204                                    else if( arg.startsWith( "-readObj=" ) ) {
205                                            readObj = arg.substring( 9 );
206                                    }
207                                    else if( arg.startsWith( "-readPrm=" ) ) {
208                                            readPrm = arg.substring( 9 );
209                                    }
210                                    else if( arg.startsWith( "-kbExec=" ) ) {
211                                            kbExec = arg.substring( 8 );
212                                    }
213                                    else if( arg.startsWith( "-execObj=" ) ) {
214                                            execObj = arg.substring( 9 );
215                                    }
216                                    else if( arg.startsWith( "-execPrm=" ) ) {
217                                            execPrm = arg.substring( 9 );
218                                    }
219                                    else if( arg.startsWith( "-DBConfig=" ) ) {
220                                            dbConfig = arg.substring( 10 );
221                                    }
222                                    else if( arg.startsWith( "-execDbid=" ) ) {
223                                            execDbid = arg.substring( 10 );
224                                    }
225                                    else if( arg.startsWith( "-hfrom=" ) ) {
226                                            hfrom = arg.substring( 7 );
227                                    }
228                            }
229    
230                            if(     kbRead == null || kbRead.length() == 0
231                            ||      readObj == null || readObj.length() == 0
232                            ||      kbExec == null || kbExec.length() == 0
233                            ||      execObj == null || execObj.length() == 0
234                            ||      dbConfig == null || dbConfig.length() == 0 ) {
235                                    LogWriter.log( "以下ã?パラメーターはå¿??ですã?" );
236                                    LogWriter.log( "-kbRead=読取方æ³?-readObj=読取対象" );
237                                    LogWriter.log( "-kbExec=実行方æ³?-execObj=実行対象" );
238                                    LogWriter.log( "-DBConfig=DBConfig.xml" );
239                            }
240    
241    //                      HybsSystem.setInitialData( new HashMap<String,String>() );
242    
243                            // DBID接続情報の取得å?の設å®?
244                            ConnectionFactory.init( null,dbConfig );
245    
246                            // 伝é?設定オブジェクãƒ?
247                            TransferConfig config = new TransferConfig(
248                                                                                    kbRead, readObj, readPrm
249                                                                                    , kbExec, execDbid, execObj, execPrm
250                                                                                    , null, hfrom, null, -1 );
251                            Set<TransferConfig> configSet = new HashSet<TransferConfig>();
252                            configSet.add( config );
253    
254                            // 伝é?処ç�?‚’実行しますã?
255                            TransferProcess proc = new TransferProcess( configSet );
256    
257                            System.out.println( "EXEC START Config=[" + config.toString() + "]" );
258                            proc.process();
259                            System.out.println( "EXEC END   Config=[" + config.toString() + "]" );
260                    }
261                    catch( Throwable ex ) {
262                            ex.printStackTrace();
263                            // 異常終äº??場å�?
264                            System.exit( 1 );
265                    }
266    
267                    // 正常終äº??場å�?
268                    System.exit( 0 );
269            }
270    }