Mercurial Hosting > nabble
comparison src/fschmidt/util/java/ProxyIntoThread.java @ 68:00520880ad02
add fschmidt source
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Sun, 05 Oct 2025 17:24:15 -0600 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 67:9d0fefce6985 | 68:00520880ad02 |
|---|---|
| 1 /* | |
| 2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com> | |
| 3 | |
| 4 Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 5 of this software and associated documentation files (the "Software"), to deal | |
| 6 in the Software without restriction, including without limitation the rights | |
| 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 8 copies of the Software, and to permit persons to whom the Software is | |
| 9 furnished to do so, subject to the following conditions: | |
| 10 | |
| 11 The above copyright notice and this permission notice shall be included in | |
| 12 all copies or substantial portions of the Software. | |
| 13 | |
| 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
| 20 THE SOFTWARE. | |
| 21 */ | |
| 22 | |
| 23 package fschmidt.util.java; | |
| 24 | |
| 25 import org.slf4j.Logger; | |
| 26 import org.slf4j.LoggerFactory; | |
| 27 | |
| 28 import java.lang.reflect.Method; | |
| 29 import java.lang.reflect.InvocationTargetException; | |
| 30 | |
| 31 | |
| 32 public class ProxyIntoThread<T> extends ProxyClass<T> { | |
| 33 private static final Logger logger = LoggerFactory.getLogger(ProxyIntoThread.class); | |
| 34 private final long timeout; | |
| 35 private volatile Thread thread; | |
| 36 private final Object lock = new Object(); | |
| 37 private boolean isCalling = false; | |
| 38 private Method method; | |
| 39 private Object[] args; | |
| 40 private Object rtn; | |
| 41 private Throwable ex; | |
| 42 | |
| 43 public ProxyIntoThread(String threadName,long timeout,final T obj,Class<T> cls) { | |
| 44 super(obj,cls); | |
| 45 this.timeout = timeout; | |
| 46 logger.info("Name = " + threadName + " / timeout = " + timeout + " / isCalling = " + isCalling); | |
| 47 thread = new Thread( | |
| 48 new Runnable(){public void run(){ | |
| 49 synchronized(lock) { | |
| 50 while(thread!=null) { | |
| 51 while( !isCalling ) { | |
| 52 try { | |
| 53 lock.wait(ProxyIntoThread.this.timeout); | |
| 54 } catch(InterruptedException e) { | |
| 55 logger.error("",e); | |
| 56 throw new RuntimeException(e); | |
| 57 } | |
| 58 if( !isCalling ) { | |
| 59 logger.error("timed out"); | |
| 60 throw new RuntimeException("timed out"); | |
| 61 } | |
| 62 } | |
| 63 try { | |
| 64 rtn = invoke(method,args); | |
| 65 } catch(Throwable e) { | |
| 66 ex = e; | |
| 67 } finally { | |
| 68 isCalling = false; | |
| 69 lock.notifyAll(); | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 }} | |
| 74 , threadName | |
| 75 ); | |
| 76 thread.start(); | |
| 77 } | |
| 78 | |
| 79 public Object invoke(Object proxy,Method method,Object[] args) | |
| 80 throws Throwable | |
| 81 { | |
| 82 synchronized(lock) { | |
| 83 if( thread==null ) | |
| 84 throw new RuntimeException("thread died"); | |
| 85 if( isCalling ) | |
| 86 throw new RuntimeException("Already calling " + this.method.getName()); | |
| 87 this.method = method; | |
| 88 this.args = args; | |
| 89 this.ex = null; | |
| 90 isCalling = true; | |
| 91 lock.notifyAll(); | |
| 92 while( isCalling ) { | |
| 93 try { | |
| 94 lock.wait(timeout); | |
| 95 } catch(InterruptedException e) { | |
| 96 logger.error("",e); | |
| 97 throw new RuntimeException(e); | |
| 98 } | |
| 99 if( isCalling ) { | |
| 100 logger.error("timed out"); | |
| 101 throw new RuntimeException("timed out"); | |
| 102 } | |
| 103 } | |
| 104 if( ex != null ) | |
| 105 throw ex; | |
| 106 return this.rtn; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 public void stop() { | |
| 111 thread = null; | |
| 112 } | |
| 113 } |
