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