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