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 28 package com.sun.star.comp.beans; 29 30 31 //--------------------------------------------------------------------------- 32 /** Helper class to watch calls into OOo with a timeout. 33 */ 34 //Do not add the thread instances to a threadgroup. When testing the bean in 35 //an applet it turned out the the ThreadGroup was in an inconsistent state 36 //after navigating off the site that contains the applet and back to it. 37 //That was tested with a Sun JRE 1.4.2_06 38 public class CallWatchThread extends Thread 39 { 40 private static boolean DEBUG = false; 41 42 private Thread aWatchedThread; 43 private String aTag; 44 private boolean bAlive; 45 private long nTimeout; 46 47 public CallWatchThread(long nTimeout) 48 { 49 this(nTimeout, ""); 50 } 51 52 public CallWatchThread( long nTimeout, String aTag ) 53 { 54 super(aTag); 55 this.aWatchedThread = Thread.currentThread(); 56 this.nTimeout = nTimeout; 57 58 this.aTag = aTag; 59 setDaemon( true ); 60 dbgPrint( "CallWatchThread(" + this + ").start(" + aTag + ")" ); 61 start(); 62 } 63 64 public void cancel() 65 throws java.lang.InterruptedException 66 { 67 dbgPrint( "CallWatchThread(" + this + ".cancel(" + aTag + ")" ); 68 if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() ) 69 throw new RuntimeException( "wrong thread" ); 70 aWatchedThread = null; 71 if ( interrupted() ) 72 throw new InterruptedException(); 73 } 74 75 public synchronized void restart() 76 throws java.lang.InterruptedException 77 { 78 dbgPrint( "CallWatchThread(" + this + ".restart(" + aTag + ")" ); 79 if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() ) 80 throw new RuntimeException( "wrong thread" ); 81 bAlive = true; 82 if ( interrupted() ) 83 throw new InterruptedException(); 84 notify(); 85 } 86 87 public void run() 88 { 89 dbgPrint( "CallWatchThread(" + this + ".run(" + aTag + ") ***** STARTED *****" ); 90 long n = 0; 91 while ( aWatchedThread != null ) 92 { 93 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") running #" + ++n ); 94 synchronized(this) 95 { 96 bAlive = false; 97 try 98 { 99 wait( nTimeout ); 100 } 101 catch ( java.lang.InterruptedException aExc ) 102 { 103 bAlive = false; 104 } 105 106 // watched thread seems to be dead (not answering)? 107 if ( !bAlive && aWatchedThread != null ) 108 { 109 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") interrupting" ); 110 aWatchedThread.interrupt(); 111 aWatchedThread = null; 112 } 113 } 114 } 115 116 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") terminated" ); 117 } 118 119 private void dbgPrint( String aMessage ) 120 { 121 if (DEBUG) 122 System.err.println( "OOoBean: " + aMessage ); 123 } 124 } 125 126 127 128 129