On Tue, Jul 20, 2010 at 1:51 PM, Matt Benson <[email protected]> wrote:
> Not quite. I would like to move the RecordedInvocation class to some sort of
> support package where it can be reused beyond InvocationRecorder, e.g. in the
> code I'll be working on. Perhaps a rename of the class too, though I'm not
> sure what makes the most sense.
>
Yeah, I think all the recording stuff should be in its own module,
commons-proxy-recorder perhaps? I have also been thinking a bit about
how to best create proxies to things that have to be looked up (such
as Spring beans) when they're deserialized. Basically, you need to
use the writeReplace()/readResolve() mechanism so that you don't write
out actual references to the Spring bean. That gave me another idea,
commons-flyweight. Basically, a flyweight would be a stand-in for a
more heavyweight object and it can be used to retrieve it (or
readResolve() it). Here's some code I threw together really quick:
public class Flyweight<T> implements Serializable
{
//**********************************************************************************************************************
// Fields
//**********************************************************************************************************************
private static Map<String, Object> flyweights = new
TreeMap<String, Object>();
private final String id;
//**********************************************************************************************************************
// Static Methods
//**********************************************************************************************************************
public static <T> Flyweight<T> register(T object)
{
final String id = UUID.randomUUID().toString();
flyweights.put(id, object);
return new Flyweight<T>(id);
}
//**********************************************************************************************************************
// Constructors
//**********************************************************************************************************************
private Flyweight(String id)
{
this.id = id;
}
//**********************************************************************************************************************
// Other Methods
//**********************************************************************************************************************
protected T readResolve()
{
return getObject();
}
public T getObject()
{
return (T) flyweights.get(id);
}
}
I haven't thought through the memory "leaks" and stuff yet. This is
just a rough idea. Maybe this sort of thing belongs in [lang] rather
as its own module.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]