View Javadoc

1   /**
2    *  BlueCove - Java library for Bluetooth
3    *  Copyright (C) 2006-2008 Vlad Skarzhevskyy
4    * 
5    *  Licensed to the Apache Software Foundation (ASF) under one
6    *  or more contributor license agreements.  See the NOTICE file
7    *  distributed with this work for additional information
8    *  regarding copyright ownership.  The ASF licenses this file
9    *  to you under the Apache License, Version 2.0 (the
10   *  "License"); you may not use this file except in compliance
11   *  with the License.  You may obtain a copy of the License at
12   *
13   *    http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing,
16   *  software distributed under the License is distributed on an
17   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   *  KIND, either express or implied.  See the License for the
19   *  specific language governing permissions and limitations
20   *  under the License.
21   *
22   *  @author vlads
23   *  @version $Id: TestTimeOutMonitor.java 2607 2008-12-17 23:51:33Z skarzhevskyy $
24   */
25  package net.sf.bluecove;
26  
27  import org.bluecove.tester.log.Logger;
28  import org.bluecove.tester.util.RuntimeDetect;
29  
30  /**
31   * 
32   */
33  public class TestTimeOutMonitor implements Runnable {
34  
35  	private boolean testFinished = false;
36  
37  	private boolean shutdownCalled = false;
38  
39  	private CanShutdown testThread;
40  
41  	private String name;
42  
43  	int gracePeriodSeconds = 0;
44  
45  	private Thread monitorThread;
46  
47  	private TestTimeOutMonitor(String name, CanShutdown testThread, int gracePeriodSeconds) {
48  		this.name = name;
49  		this.testThread = testThread;
50  		this.gracePeriodSeconds = gracePeriodSeconds;
51  	}
52  
53  	public static TestTimeOutMonitor create(String name, CanShutdown testThread, int gracePeriodSeconds) {
54  		TestTimeOutMonitor monitor = new TestTimeOutMonitor(name, testThread, gracePeriodSeconds);
55  
56  		if (gracePeriodSeconds != 0) {
57  			monitor.monitorThread = RuntimeDetect.cldcStub.createNamedThread(monitor, name + "Monitor");
58  			monitor.monitorThread.start();
59  		}
60  		return monitor;
61  	}
62  
63  	private int getTestGracePeriod() {
64  		if (testThread instanceof ConnectionHolder) {
65  			return ((ConnectionHolder) testThread).getTestTimeOutSec();
66  		} else {
67  			return 0;
68  		}
69  	}
70  
71  	public void run() {
72  		if (gracePeriodSeconds == 0) {
73  			return;
74  		}
75  
76  		while ((!testFinished)
77  				&& (System.currentTimeMillis() < (testThread.lastActivityTime() + (getTestGracePeriod() + this.gracePeriodSeconds) * 1000))) {
78  			try {
79  				Thread.sleep(10 * 1000);
80  			} catch (InterruptedException e) {
81  				return;
82  			}
83  		}
84  
85  		if (!testFinished) {
86  			shutdownCalled = true;
87  			Logger.info("shutdown " + name + " by TimeOut");
88  			testThread.shutdown();
89  		}
90  	}
91  
92  	public void finish() {
93  		testFinished = true;
94  	}
95  
96  	public boolean isShutdownCalled() {
97  		return shutdownCalled;
98  	}
99  }