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.plugin.daemon;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.fukurou.util.Shell;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.fukurou.util.HybsTimerTask;
022import java.io.File;
023import 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 */
049public class Daemon_RunShell extends HybsTimerTask {
050
051        private static final int LOOP_COUNTER = 24; // カウンタを24回に設定
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