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