1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 package helper;
28 
29 import lib.TestParameters;
30 import java.util.StringTokenizer;
31 import util.utils;
32 
33 public class OfficeWatcher extends Thread implements share.Watcher {
34 
35     public boolean finish;
36     private TestParameters params;
37     private int StoredPing = 0;
38     private boolean debug = false;
39 
40     /** Creates new OfficeWatcher
41      * @param param
42      */
43     public OfficeWatcher(TestParameters param) {
44         finish = false;
45         this.params = param;
46         debug = params.getBool(util.PropertyName.DEBUG_IS_ACTIVE);
47     }
48 
49     /**
50      * pings the office watcher to check for changes
51      */
52     public void ping() {
53         try {
54             StoredPing++;
55         } catch (Exception e) {
56             StoredPing = 0;
57         }
58     }
59 
60     /**
61      * returns the amount of pings
62      * @return returns the amount of pings
63      */
64     public int getPing() {
65         return StoredPing;
66     }
67 
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;
75         }
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;
83             }
84             // execute in case the watcher is not needed anymore
85             if (finish) {
86                 return;
87             }
88         }
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();
97 
98                     dbg("User defined an application to destroy the started process.");
99                     dbg("Trying to execute: " + sKillCommand);
100 
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");
109 
110                 }
111             }
112             ph.kill();
113         } else {
114             dbg("reaeched timeout but ProcessHandler is NULL");
115         }
116         shortWait(timeOut == 0 ? 30000 : timeOut);
117         dbg("finished");
118     }
119 
120     protected void shortWait(int timeOut) {
121         try {
122             OfficeWatcher.sleep(timeOut);
123         } catch (java.lang.InterruptedException ie) {
124         }
125     }
126 
127     protected void dbg(String message) {
128         if (debug) {
129             System.out.println(utils.getDateTime() + "OfficeWatcher: " + message);
130         }
131     }
132 }
133