1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: CallWatchThread.java,v $
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
;
47 private boolean bAlive
;
48 private long nTimeout
;
50 public CallWatchThread(long nTimeout
)
55 public CallWatchThread( long nTimeout
, String aTag
)
58 this.aWatchedThread
= Thread
.currentThread();
59 this.nTimeout
= nTimeout
;
63 dbgPrint( "CallWatchThread(" + this + ").start(" + aTag
+ ")" );
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;
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" );
86 throw new InterruptedException();
92 dbgPrint( "CallWatchThread(" + this + ".run(" + aTag
+ ") ***** STARTED *****" );
94 while ( aWatchedThread
!= null )
96 dbgPrint( "CallWatchThread(" + this + ").run(" + aTag
+ ") running #" + ++n
);
104 catch ( java
.lang
.InterruptedException aExc
)
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
)
125 System
.err
.println( "OOoBean: " + aMessage
);