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.plugin.daemon;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.fukurou.util.Shell;
020    import org.opengion.fukurou.util.StringUtil;
021    import org.opengion.fukurou.util.HybsTimerTask;
022    import java.io.File;
023    import java.util.Date;
024    
025    /**
026     * ã€Shell実行ã?
027     * æŒ?®šã—ãŸãƒ‘ラメータã§Shellを実行ã—ã¾ã™ã?
028     * ã“ã?クラスã¯ã€HybsTimerTask を継承ã—㟠タイマã?タスククラスã§ã™ã?
029     * startDaemon() ãŒã‚¿ã‚¤ãƒžã?タスクã«ã‚ˆã£ã¦ã€å‘¼ã³å‡ºã•れã¾ã™ã?
030     *
031     * 接続ã?ãŸã‚ã®ãƒ‘ラメータã¯ä»¥ä¸‹ã§ã?
032     * fukurouã®Shellã‚’ã‚­ãƒ?‚¯ã™ã‚‹ãƒ‘ラメータã¨åŒã˜ã§ã™ã?
033     * program       : 動作ã?オグラãƒ?
034     * workDir       : 実行ディレクトリ
035     * useBatch      : BATCHプロセスを実行ã™ã‚‹ã?ã‹ã©ã?‹(åˆæœŸå€¤:false)
036     * stdout        : 標準å?ã‚’å?力ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:false)
037     * stderr        : エラー出力を出力ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:false)
038     * wait          : プロセスã®çµ‚äº?‚’å¾?¤(true)/å¾?Ÿãªã?false) (åˆæœŸå€¤:true)
039     *      
040     *
041     *
042     * @og.rev 5.6.9.1 (2013/10/11) æ–°è¦ä½œæ?
043     * @og.group �?モン
044     *
045     * @version  4.0
046     * @author   Takahashi Masakazu
047     * @since    JDK5.0,
048     */
049    public class Daemon_RunShell extends HybsTimerTask {
050    
051            private static final int LOOP_COUNTER = 24; // カウンタã‚?4回ã«è¨­å®?
052    
053            private int loopCnt = 0;
054    
055            private boolean   debug = false;
056            private String    program  = null;
057            private boolean   useBatch = false;     // BATCHプロセスを実行ã™ã‚‹ã?ã‹ã©ã?‹(åˆæœŸå€¤:false)
058            private boolean   stdout   = false;     // 標準å?ã‚’å?力ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:false)
059            private boolean   stderr   = false;     // エラー出力を出力ã™ã‚‹ã‹ã©ã?‹(åˆæœŸå€¤:false)
060            private boolean   wait     = true;      // プロセスã®çµ‚äº?‚’å¾?¤(true)/å¾?Ÿãªã?false) (åˆæœŸå€¤:true)
061            private File      workDir  = null;
062            // 3.6.1.0 (2005/01/05) タイãƒ?‚¢ã‚¦ãƒˆæ™‚間を設å®?
063            private int timeout                     = HybsSystem.sysInt( "SHELL_TIMEOUT" );
064            
065            private Shell shell;
066    
067            /**
068             * ã“ã?タイマã?タスクã«ã‚ˆã£ã¦åˆæœŸåŒ–ã•れるアクションã§ã™ã?
069             * パラメータを使用ã—ãŸåˆæœŸåŒ–を行ã„ã¾ã™ã?
070             *
071             */
072            @Override
073            public void initDaemon() {
074                    debug = StringUtil.nval( getValue( "DEBUG" ),debug ) ;
075                    program         = StringUtil.nval( getValue( "program" ), program );
076                    useBatch        = StringUtil.nval( getValue( "useBatch" ), useBatch );
077                    stdout          = StringUtil.nval( getValue( "stdout" ), stdout );
078                    stderr          = StringUtil.nval( getValue( "stderr" ), stderr );
079                    wait            = StringUtil.nval( getValue( "wait" ), wait   );
080                    if( getValue( "workDir" ) != null ){ workDir = new File(getValue( "workDir" ));  }
081                    shell = new Shell();
082                    shell.setCommand( program,useBatch );
083                    shell.setWait( wait );
084                    shell.setTimeout( timeout );            // 3.6.1.0 (2005/01/05)
085                    shell.setWorkDir( workDir );
086                    if(debug){System.out.println(program+"/"+useBatch+"/"+wait+"/"+timeout+"/"+workDir.toString());}
087            }
088    
089            /**
090             * タイマã?タスクã®ãƒ??モン処ç??é–‹å§‹ã?イントã§ã™ã?
091             *
092             */
093            @Override
094            protected void startDaemon() {
095                    if( loopCnt % LOOP_COUNTER == 0 ) {
096                            loopCnt = 1;
097                            System.out.println( toString() + " " + new Date()  + " " );
098                    }
099                    else {
100                            loopCnt++ ;
101                    }
102                    // 実�
103                    int rtnCode = shell.exec();             // 0 ã¯æ­£å¸¸çµ‚äº?‚’示ã?
104                    if( rtnCode < 0 ){System.out.println( "Shell Run Error:" + program );}
105            }
106    
107            /**
108             * ã“ã?タイマã?タスクã®cancel() メソãƒ?ƒ‰ã‚’オーãƒã?ライドã—ã¾ã™ã?
109             * HybsTimerTaskManager#cancelTask( int ) を実行ã—ã¾ã™ã?
110             *
111             * @og.rev 4.3.1.1 (2008/08/23) super.cancel() ã®ã¿å®Ÿè¡Œãªã‚‰ã?オーãƒã?ライドã?å¿?¦ã?ãªã?
112             *
113             * @see java.util.TimerTask#cancel()
114             */
115    //      public boolean cancel() {
116    //              return super.cancel();
117    //      }
118    
119    }
120