1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 package helper; 24 25 import lib.TestParameters; 26 import java.util.StringTokenizer; 27 import util.utils; 28 29 public class OfficeWatcher extends Thread implements share.Watcher { 30 31 public boolean finish; 32 private TestParameters params; 33 private int StoredPing = 0; 34 private boolean debug = false; 35 36 /** Creates new OfficeWatcher 37 * @param param 38 */ OfficeWatcher(TestParameters param)39 public OfficeWatcher(TestParameters param) { 40 finish = false; 41 this.params = param; 42 debug = params.getBool(util.PropertyName.DEBUG_IS_ACTIVE); 43 } 44 45 /** 46 * pings the office watcher to check for changes 47 */ ping()48 public void ping() { 49 try { 50 StoredPing++; 51 } catch (Exception e) { 52 StoredPing = 0; 53 } 54 } 55 56 /** 57 * returns the amount of pings 58 * @return returns the amount of pings 59 */ getPing()60 public int getPing() { 61 return StoredPing; 62 } 63 run()64 public void run() { 65 dbg("started"); 66 boolean isDone = false; 67 final ProcessHandler ph = (ProcessHandler) params.get("AppProvider"); 68 int timeOut = params.getInt("TimeOut"); 69 if (ph == null) { 70 isDone = true; 71 } 72 while (!isDone) { 73 timeOut = params.getInt("TimeOut"); 74 final int previous = StoredPing; 75 shortWait(timeOut == 0 ? 30000 : timeOut); 76 // a timeout with value 0 lets watcher not react. 77 if ((StoredPing == previous) && timeOut != 0) { 78 isDone = true; 79 } 80 // execute in case the watcher is not needed anymore 81 if (finish) { 82 return; 83 } 84 } 85 if (ph != null) { 86 dbg("the Office is idle for " + timeOut / 1000 + 87 " seconds, it probably hangs and is killed NOW."); 88 final String AppKillCommand = (String) params.get(util.PropertyName.APP_KILL_COMMAND); 89 if (AppKillCommand != null) { 90 final StringTokenizer aKillCommandToken = new StringTokenizer(AppKillCommand, ";"); 91 while (aKillCommandToken.hasMoreTokens()) { 92 final String sKillCommand = aKillCommandToken.nextToken(); 93 94 dbg("User defined an application to destroy the started process."); 95 dbg("Trying to execute: " + sKillCommand); 96 97 final ProcessHandler pHdl = new ProcessHandler(sKillCommand); 98 pHdl.executeSynchronously(); 99 // dbg("---> Output of killoffice:"); 100 // dbg(pHdl.getOutputText()); 101 // dbg("<--- Output of killoffice"); 102 // dbg("---> Error output of killoffice:"); 103 // dbg(pHdl.getErrorText()); 104 // dbg("<--- Error output of killoffice"); 105 106 } 107 } 108 ph.kill(); 109 } else { 110 dbg("reaeched timeout but ProcessHandler is NULL"); 111 } 112 shortWait(timeOut == 0 ? 30000 : timeOut); 113 dbg("finished"); 114 } 115 shortWait(int timeOut)116 protected void shortWait(int timeOut) { 117 try { 118 OfficeWatcher.sleep(timeOut); 119 } catch (java.lang.InterruptedException ie) { 120 } 121 } 122 dbg(String message)123 protected void dbg(String message) { 124 if (debug) { 125 System.out.println(utils.getDateTime() + "OfficeWatcher: " + message); 126 } 127 } 128 } 129