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                                            System.out.println();
132                                    }
133    
134                                    // èª­å–æ–¹æ³•ã?オブジェクトを生æ?ã—ã?ä¼é?プロセスã«ã‚»ãƒ?ƒˆã—ã¾ã™ã?
135                                    read = (TransferRead)StringUtil.newInstance( READ_CLASS_BASE + config.getKbRead() );
136    
137                                    // 実行方法ã?オブジェクトを生æ?ã—ã?ä¼é?プロセスã«ã‚»ãƒ?ƒˆã—ã¾ã™ã?
138                                    exec = (TransferExec)StringUtil.newInstance( EXEC_CLASS_BASE + config.getKbExec() );
139    
140                                    String[] vals = read.read( config, tran );
141                                    exec.execute( vals, config, tran );
142                                    read.complete( config, tran );
143    
144                                    // ãƒ?ƒãƒ?‚°æƒ??ã‚’å?力ã—ã¾ã™ã?
145                                    if( isDebug ) {
146                                            System.out.println();
147                                            System.out.print( " END   = " + new Date() );
148                                            System.out.print( "[" + dmnName + "]:[" + config.toString() + "]" );
149                                            System.out.println();
150                                    }
151                            }
152                    }
153                    catch( Throwable ex ) {
154                            if( tran != null ) {
155                                    tran.rollback();
156                                    tran.close();
157                                    tran = null; // エラー発生時ã¯ã€æŽ¥ç¶šã‚’終äº?—ã¾ã™ã?(次ã®çŠ¶æ³æ›´æ–°ã§ãƒ?ƒƒãƒ‰ãƒ­ãƒ?‚¯ã«ãªã‚‹ãŸã‚?
158                            }
159    
160                            if( read != null ) {
161                                    read.error( config, appInfo );
162                            }
163    
164                            throw new RuntimeException( ex );
165                    }
166                    finally {
167                            if( tran != null ) { tran.close(); }
168                    }
169            }
170    
171            /**
172             * 実行用ã®ãƒ¡ã‚¤ãƒ³ãƒ¡ã‚½ãƒ?ƒ‰
173             *
174             * Usage: java org.opengion.hayabusa.transfer.TransferProcess
175             *  -kbRead=èª­å–æ–¹æ³?-readObj=読å–対象 [-readPrm=読å–パラメーター]
176             *  -kbExec=実行方�-execObj=実行対象 [-execPrm=実行パラメーター]
177             *  -DBConfig=DBConfig.xml [-execDbid=実行接続å?DBID] [-hfrom=é€ã‚Šå…??ストコード]
178             *  [-debug=true/false]
179             *  
180             * @og.rev 5.8.3.2 (2015/01/30) debug追�
181             * @og.rev 5.8.4.2 (2015/02/28) 修正
182             *
183             * @param       args    コマンド引数é…å?
184             */
185            public static void main( final String[] args ) {
186                    boolean debug   = false; // 5.8.3.2 (2015/01/30) 
187                    try {
188                            if( args.length < 5 ) {
189                                    LogWriter.log( "Usage: java org.opengion.hayabusa.transfer.TransferProcess" );
190                                    LogWriter.log( "   -kbRead=èª­å–æ–¹æ³?-readObj=読å–対象 [-readPrm=読å–パラメーター]" );
191                                    LogWriter.log( "   -kbExec=実行方�-execObj=実行対象 [-execPrm=実行パラメーター]" );
192                                    LogWriter.log( "   -DBConfig=DBConfig.xml [-execDbid=実行接続å?DBID] [-hfrom=é€ã‚Šå…??ストコード]" );
193                                    return;
194                            }
195    
196                            String kbRead   = null;
197                            String readObj  = null;
198                            String readPrm  = null;
199                            String kbExec   = null;
200                            String execObj  = null;
201                            String execPrm  = null;
202                            String dbConfig = null;
203                            String execDbid = null;
204                            String hfrom    = null;
205                            
206    
207                            for( int i=0; i<args.length; i++ ) {
208                                    String arg = args[i];
209                                    if( arg.startsWith( "-kbRead=" ) ) {
210                                            kbRead = arg.substring( 8 );
211                                    }
212                                    else if( arg.startsWith( "-readObj=" ) ) {
213                                            readObj = arg.substring( 9 );
214                                    }
215                                    else if( arg.startsWith( "-readPrm=" ) ) {
216                                            readPrm = arg.substring( 9 );
217                                    }
218                                    else if( arg.startsWith( "-kbExec=" ) ) {
219                                            kbExec = arg.substring( 8 );
220                                    }
221                                    else if( arg.startsWith( "-execObj=" ) ) {
222                                            execObj = arg.substring( 9 );
223                                    }
224                                    else if( arg.startsWith( "-execPrm=" ) ) {
225                                            execPrm = arg.substring( 9 );
226                                    }
227                                    else if( arg.startsWith( "-DBConfig=" ) ) {
228                                            dbConfig = arg.substring( 10 );
229                                    }
230                                    else if( arg.startsWith( "-execDbid=" ) ) {
231                                            execDbid = arg.substring( 10 );
232                                    }
233                                    else if( arg.startsWith( "-hfrom=" ) ) {
234                                            hfrom = arg.substring( 7 );
235                                    }
236                                    else if( arg.startsWith( "-debug=" ) ) { // 5.8.3.2 (2015/01/30) 
237                                            if( "true".equals(arg.substring( 7 )) ){ debug = true; }  // 5.8.4.2 (2015/02/28)
238                                    }
239                            }
240    
241                            if(     kbRead == null || kbRead.length() == 0
242                            ||      readObj == null || readObj.length() == 0
243                            ||      kbExec == null || kbExec.length() == 0
244                            ||      execObj == null || execObj.length() == 0
245                            ||      dbConfig == null || dbConfig.length() == 0 ) {
246                                    LogWriter.log( "以下ã?パラメーターã¯å¿??ã§ã™ã?" );
247                                    LogWriter.log( "-kbRead=èª­å–æ–¹æ³?-readObj=読å–対象" );
248                                    LogWriter.log( "-kbExec=実行方�-execObj=実行対象" );
249                                    LogWriter.log( "-DBConfig=DBConfig.xml" );
250                            }
251    
252    //                      HybsSystem.setInitialData( new HashMap<String,String>() );
253    
254                            // DBID接続情報ã®å–å¾—å?ã®è¨­å®?
255                            ConnectionFactory.init( null,dbConfig );
256    
257                            // ä¼é?設定オブジェクãƒ?
258                            TransferConfig config = new TransferConfig(
259                                                                                    kbRead, readObj, readPrm
260                                                                                    , kbExec, execDbid, execObj, execPrm
261                                                                                    , null, hfrom, null, -1 );
262                            Set<TransferConfig> configSet = new HashSet<TransferConfig>();
263                            configSet.add( config );
264    
265                            // ä¼é?処ç?‚’実行ã—ã¾ã™ã?
266                            TransferProcess proc = new TransferProcess( configSet ); 
267                            if( debug ){ // 5.8.3.2 (2015/01/30)
268                                    proc.setDebug();
269                            }
270                            
271    
272                            System.out.println( "EXEC START Config=[" + config.toString() + "]" );
273                            proc.process();
274                            System.out.println( "EXEC END   Config=[" + config.toString() + "]" );
275                    }
276                    catch( Throwable ex ) {
277                            ex.printStackTrace();
278                            // 5.8.3.2 (2015/01/30)
279                            if( debug ){
280                                    System.out.println( ex.toString() );
281                            }
282                            // 異常終äº??å ´å?
283                            System.exit( 1 );
284                    }
285    
286                    // 正常終äº??å ´å?
287                    System.exit( 0 );
288            }
289    }