[concurrency-interest] additional class suggested

Patrick Moore pmoore@brocade.com
Tue, 10 Dec 2002 11:04:49 -0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C2A07F.030ED670
Content-Type: text/plain

Hi there --
 
This is a subclass that I use all the time and I would suggest that
something equivalent needs to be in the java.util.concurrency package. This
class is invaluable for retrofitting existing code to use Future(Result).
 
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import EDU.oswego.cs.dl.util.concurrent.FutureResult;
import java.lang.reflect.Proxy;
/**
 * This class is useful to when retrofitting existing code that uses
interfaces
 * to handle future results.
 * @author  pmoore
 * @version
 */
public class ProxiedFutureResult extends FutureResult implements
InvocationHandler {
    private long timedGet = -1L;
    public ProxiedFutureResult(){
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws
Throwable {
        Object obj;
        try {
            if ( timedGet < 0 ) {
                obj = this.get();
            } else {
                obj = this.timedGet(timedGet);
            }
        } catch(InvocationTargetException e) {
            // since the wrapped exception is more meaningful to the
            // code using the proxy.
            throw e.getCause();
        }
        return method.invoke(obj, args);
    }
    public void setTimedGetTime(long msecs) {
        this.timedGet = msecs;
    }
    public Object getProxy(Class[] proxyFor) {
        return Proxy.newProxyInstance(null, proxyFor, this);
    }
    public Object getProxy(ClassLoader classLoader, Class[] proxyFor) {
        return Proxy.newProxyInstance(classLoader, proxyFor, this);
    }
}

------_=_NextPart_001_01C2A07F.030ED670
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<TITLE>RE: [concurrency-interest] Re: 'Future' definition</TITLE>

<META content="MSHTML 5.00.3315.2870" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT color=#008080 face=Arial size=2><SPAN class=114570019-10122002>Hi 
there --</SPAN></FONT></DIV>
<DIV><FONT color=#008080 face=Arial size=2><SPAN 
class=114570019-10122002></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#008080 face=Arial size=2><SPAN class=114570019-10122002>This 
is&nbsp;a subclass that I use all the time and I would suggest that something 
equivalent needs to be in the java.util.concurrency package. This class is 
invaluable for retrofitting existing code to use 
Future(Result).</SPAN></FONT></DIV>
<DIV><FONT color=#008080 face=Arial size=2><SPAN 
class=114570019-10122002></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#008080 face=Arial size=2><SPAN class=114570019-10122002>import 
java.lang.reflect.InvocationHandler;<BR>import 
java.lang.reflect.InvocationTargetException;<BR>import 
java.lang.reflect.Method;<BR>import 
EDU.oswego.cs.dl.util.concurrent.FutureResult;<BR>import 
java.lang.reflect.Proxy;<BR>/**<BR>&nbsp;* This class is useful to when 
retrofitting existing code that uses interfaces<BR>&nbsp;* to handle future 
results.<BR>&nbsp;* @author&nbsp; pmoore<BR>&nbsp;* 
@version<BR>&nbsp;*/<BR>public class ProxiedFutureResult extends FutureResult 
implements InvocationHandler {<BR>&nbsp;&nbsp;&nbsp; private long timedGet = 
-1L;<BR>&nbsp;&nbsp;&nbsp; public ProxiedFutureResult(){<BR>&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp; public Object invoke(Object proxy, Method method, 
Object[] args) throws Throwable {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
Object obj;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( 
timedGet &lt; 0 ) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
obj = 
this.get();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
} else 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
obj = 
this.timedGet(timedGet);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } 
catch(InvocationTargetException e) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // since 
the wrapped exception is more meaningful to 
the<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 
code using the 
proxy.<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
throw e.getCause();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return method.invoke(obj, 
args);<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public void 
setTimedGetTime(long msecs) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
this.timedGet = msecs;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public 
Object getProxy(Class[] proxyFor) 
{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
Proxy.newProxyInstance(null, proxyFor, this);<BR>&nbsp;&nbsp;&nbsp; 
}<BR>&nbsp;&nbsp;&nbsp; public Object getProxy(ClassLoader classLoader, Class[] 
proxyFor) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 
Proxy.newProxyInstance(classLoader, proxyFor, this);<BR>&nbsp;&nbsp;&nbsp; 
}<BR>}</SPAN></FONT></DIV></BODY></HTML>

------_=_NextPart_001_01C2A07F.030ED670--