merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / helper / OfficeWatcher.java
blob2068a7f5246c62f660d76af667cdf4026234b2a3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package helper;
29 import lib.TestParameters;
30 import java.util.StringTokenizer;
31 import util.utils;
33 public class OfficeWatcher extends Thread implements share.Watcher {
35 public boolean finish;
36 private TestParameters params;
37 private int StoredPing = 0;
38 private boolean debug = false;
40 /** Creates new OfficeWatcher
41 * @param param
43 public OfficeWatcher(TestParameters param) {
44 finish = false;
45 this.params = param;
46 debug = params.getBool(util.PropertyName.DEBUG_IS_ACTIVE);
49 /**
50 * pings the office watcher to check for changes
52 public void ping() {
53 try {
54 StoredPing++;
55 } catch (Exception e) {
56 StoredPing = 0;
60 /**
61 * returns the amount of pings
62 * @return returns the amount of pings
64 public int getPing() {
65 return StoredPing;
68 public void run() {
69 dbg("started");
70 boolean isDone = false;
71 final ProcessHandler ph = (ProcessHandler) params.get("AppProvider");
72 int timeOut = params.getInt("TimeOut");
73 if (ph == null) {
74 isDone = true;
76 while (!isDone) {
77 timeOut = params.getInt("TimeOut");
78 final int previous = StoredPing;
79 shortWait(timeOut == 0 ? 30000 : timeOut);
80 // a timeout with value 0 lets watcher not react.
81 if ((StoredPing == previous) && timeOut != 0) {
82 isDone = true;
84 // execute in case the watcher is not needed anymore
85 if (finish) {
86 return;
89 if (ph != null) {
90 dbg("the Office is idle for " + timeOut / 1000 +
91 " seconds, it probably hangs and is killed NOW.");
92 final String AppKillCommand = (String) params.get(util.PropertyName.APP_KILL_COMMAND);
93 if (AppKillCommand != null) {
94 final StringTokenizer aKillCommandToken = new StringTokenizer(AppKillCommand, ";");
95 while (aKillCommandToken.hasMoreTokens()) {
96 final String sKillCommand = aKillCommandToken.nextToken();
98 dbg("User defined an application to destroy the started process.");
99 dbg("Trying to execute: " + sKillCommand);
101 final ProcessHandler pHdl = new ProcessHandler(sKillCommand);
102 pHdl.executeSynchronously();
103 // dbg("---> Output of killoffice:");
104 // dbg(pHdl.getOutputText());
105 // dbg("<--- Output of killoffice");
106 // dbg("---> Error output of killoffice:");
107 // dbg(pHdl.getErrorText());
108 // dbg("<--- Error output of killoffice");
112 ph.kill();
113 } else {
114 dbg("reaeched timeout but ProcessHandler is NULL");
116 shortWait(timeOut == 0 ? 30000 : timeOut);
117 dbg("finished");
120 protected void shortWait(int timeOut) {
121 try {
122 OfficeWatcher.sleep(timeOut);
123 } catch (java.lang.InterruptedException ie) {
127 protected void dbg(String message) {
128 if (debug) {
129 System.out.println(utils.getDateTime() + "OfficeWatcher: " + message);