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 * @og.rev 5.6.9.1 (2013/10/11) 新規作成 041 * @og.group デーモン 042 * 043 * @version 4.0 044 * @author Takahashi Masakazu 045 * @since JDK5.0, 046 */ 047public class Daemon_RunShell extends HybsTimerTask { 048 /** このプログラムのVERSION文字列を設定します。 {@value} */ 049 private static final String VERSION = "6.4.2.0 (2016/01/29)" ; 050 051 private static final int LOOP_COUNTER = 24; // カウンタを24回に設定 052 053 // 3.6.1.0 (2005/01/05) タイムアウト時間を設定 054 private final int timeout = HybsSystem.sysInt( "SHELL_TIMEOUT" ); 055 056 private int loopCnt ; 057 private String program ; 058 private boolean useBatch ; // BATCHプロセスを実行するのかどうか(初期値:false) 059 private boolean stdout ; // 標準出を出力するかどうか(初期値:false) 060 private boolean stderr ; // エラー出力を出力するかどうか(初期値:false) 061 private boolean wait = true; // プロセスの終了を待つ(true)/待たない(false) (初期値:true) 062 private File workDir ; 063 private boolean debug ; 064 065 private Shell shell; 066 067 /** 068 * デフォルトコンストラクター 069 * 070 * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor. 071 */ 072 public Daemon_RunShell() { super(); } // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。 073 074 /** 075 * このタイマータスクによって初期化されるアクションです。 076 * パラメータを使用した初期化を行います。 077 * 078 */ 079 @Override 080 public void initDaemon() { 081 debug = StringUtil.nval( getValue( "DEBUG" ),debug ) ; 082 program = StringUtil.nval( getValue( "program" ), program ); 083 useBatch = StringUtil.nval( getValue( "useBatch" ), useBatch ); 084 stdout = StringUtil.nval( getValue( "stdout" ), stdout ); 085 stderr = StringUtil.nval( getValue( "stderr" ), stderr ); 086 wait = StringUtil.nval( getValue( "wait" ), wait ); 087 if( getValue( "workDir" ) != null ){ workDir = new File(getValue( "workDir" )); } 088 shell = new Shell(); 089 shell.setCommand( program,useBatch ); 090 shell.setWait( wait ); 091 shell.setTimeout( timeout ); // 3.6.1.0 (2005/01/05) 092 shell.setWorkDir( workDir ); 093 if( debug ) { System.out.println(program+"/"+useBatch+"/"+wait+"/"+timeout+"/"+workDir.toString()); } 094 } 095 096 /** 097 * タイマータスクのデーモン処理の開始ポイントです。 098 * 099 */ 100 @Override 101 protected void startDaemon() { 102 if( loopCnt % LOOP_COUNTER == 0 ) { 103 loopCnt = 1; 104 System.out.println( toString() + " " + new Date() + " " ); 105 } 106 else { 107 loopCnt++ ; 108 } 109 // 実行 110 final int rtnCode = shell.exec(); // 0 は正常終了を示す 111 if( rtnCode < 0 ){System.out.println( "Shell Run Error:" + program );} 112 } 113}