Update ooo320-m1
[ooovba.git] / bean / com / sun / star / comp / beans / CallWatchThread.java
blob17ef36e0eebb89d2498f3edf2a8609dab0d2e92c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CallWatchThread.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.comp.beans;
34 //---------------------------------------------------------------------------
35 /** Helper class to watch calls into OOo with a timeout.
37 //Do not add the thread instances to a threadgroup. When testing the bean in
38 //an applet it turned out the the ThreadGroup was in an inconsistent state
39 //after navigating off the site that contains the applet and back to it.
40 //That was tested with a Sun JRE 1.4.2_06
41 public class CallWatchThread extends Thread
43 private static boolean DEBUG = false;
45 private Thread aWatchedThread;
46 private String aTag;
47 private boolean bAlive;
48 private long nTimeout;
50 public CallWatchThread(long nTimeout)
52 this(nTimeout, "");
55 public CallWatchThread( long nTimeout, String aTag )
57 super(aTag);
58 this.aWatchedThread = Thread.currentThread();
59 this.nTimeout = nTimeout;
61 this.aTag = aTag;
62 setDaemon( true );
63 dbgPrint( "CallWatchThread(" + this + ").start(" + aTag + ")" );
64 start();
67 public void cancel()
68 throws java.lang.InterruptedException
70 dbgPrint( "CallWatchThread(" + this + ".cancel(" + aTag + ")" );
71 if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() )
72 throw new RuntimeException( "wrong thread" );
73 aWatchedThread = null;
74 if ( interrupted() )
75 throw new InterruptedException();
78 public synchronized void restart()
79 throws java.lang.InterruptedException
81 dbgPrint( "CallWatchThread(" + this + ".restart(" + aTag + ")" );
82 if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() )
83 throw new RuntimeException( "wrong thread" );
84 bAlive = true;
85 if ( interrupted() )
86 throw new InterruptedException();
87 notify();
90 public void run()
92 dbgPrint( "CallWatchThread(" + this + ".run(" + aTag + ") ***** STARTED *****" );
93 long n = 0;
94 while ( aWatchedThread != null )
96 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") running #" + ++n );
97 synchronized(this)
99 bAlive = false;
102 wait( nTimeout );
104 catch ( java.lang.InterruptedException aExc )
106 bAlive = false;
109 // watched thread seems to be dead (not answering)?
110 if ( !bAlive && aWatchedThread != null )
112 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") interrupting" );
113 aWatchedThread.interrupt();
114 aWatchedThread = null;
119 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") terminated" );
122 private void dbgPrint( String aMessage )
124 if (DEBUG)
125 System.err.println( "OOoBean: " + aMessage );