1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.comp.beans;
25 
26 //---------------------------------------------------------------------------
27 /** Helper class to watch calls into AOO with a timeout.
28  */
29 // Do not add the thread instances to a threadgroup. When testing the bean in
30 // an applet it turned out the ThreadGroup was in an inconsistent state
31 // after navigating off the site that contains the applet and back to it.
32 // That was tested with a Sun JRE 1.4.2_06
33 public class CallWatchThread extends Thread
34 {
35 	private static boolean DEBUG = false;
36 
37 	private Thread aWatchedThread;
38 	private String aTag;
39 	private boolean bAlive;
40 	private long nTimeout;
41 
CallWatchThread(long nTimeout)42 	public CallWatchThread(long nTimeout)
43 	{
44 		this(nTimeout, "");
45 	}
46 
CallWatchThread( long nTimeout, String aTag )47 	public CallWatchThread( long nTimeout, String aTag )
48 	{
49 		super(aTag);
50 		this.aWatchedThread = Thread.currentThread();
51 		this.nTimeout = nTimeout;
52 
53 		this.aTag = aTag;
54 		setDaemon( true );
55 		dbgPrint( "CallWatchThread(" + this + ").start(" + aTag + ")" );
56 		start();
57 	}
58 
cancel()59 	public void cancel()
60 		throws java.lang.InterruptedException
61 	{
62 		dbgPrint( "CallWatchThread(" + this + ".cancel(" + aTag + ")" );
63 		if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() )
64 			throw new RuntimeException( "wrong thread" );
65 		aWatchedThread = null;
66 		if ( interrupted() )
67 			throw new InterruptedException();
68 	}
69 
restart()70 	public synchronized void restart()
71 		throws java.lang.InterruptedException
72 	{
73 		dbgPrint( "CallWatchThread(" + this + ".restart(" + aTag + ")" );
74 		if ( aWatchedThread != null && aWatchedThread != Thread.currentThread() )
75 			throw new RuntimeException( "wrong thread" );
76 		bAlive = true;
77 		if ( interrupted() )
78 			throw new InterruptedException();
79 		notify();
80 	}
81 
run()82 	public void run()
83 	{
84 		dbgPrint( "CallWatchThread(" + this + ".run(" + aTag + ") ***** STARTED *****" );
85 		long n = 0;
86 		while ( aWatchedThread != null )
87 		{
88 			dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") running #" + ++n );
89 			synchronized(this)
90 			{
91 				bAlive = false;
92 				try
93 				{
94 					wait( nTimeout );
95 				}
96 				catch ( java.lang.InterruptedException aExc )
97 				{
98 					bAlive = false;
99 				}
100 
101 				// watched thread seems to be dead (not answering)?
102 				if ( !bAlive && aWatchedThread != null )
103 				{
104 					dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") interrupting" );
105 					aWatchedThread.interrupt();
106 					aWatchedThread = null;
107 				}
108 			}
109 		}
110 
111 		dbgPrint( "CallWatchThread(" + this + ").run(" + aTag + ") terminated" );
112 	}
113 
dbgPrint( String aMessage )114 	private void dbgPrint( String aMessage )
115 	{
116 		if (DEBUG)
117 			System.err.println( "OOoBean: " + aMessage );
118 	}
119 }
120 
121