Skip navigation links

Package com.thoughtworks.proxy.toys.hotswap

A toy to hot swap instances.

See: Description

Package com.thoughtworks.proxy.toys.hotswap Description

A toy to hot swap instances.

The package provides a proxy factory creating proxies, that can hot swap instances. Main component is the HotSwapping toy, a utility class creating these proxies. Such a proxy contains an instance of a HotSwappingInvoker that delegates all calls. The implementation subclasses the Delegating toy. The proxy itself implements the additional Swappable interface that can be utilized to exchange the delegate without further notice for any other object using that instance.

Following example demonstrates this with a PrintWriter, that will not take any notice, that his delegated OutputStream will be hot swapped for every line separating odd and even lines:

ByteArrayOutputStream outStreamOdd = new ByteArrayOutputStream();
ByteArrayOutputStream outStreamEven = new ByteArrayOutputStream();
OutputStream out = HotSwapping.proxy(OutputStream.class)
    .with(null)
    .build(new CglibProxyFactory());
PrintWriter writer = new PrintWriter(out);
for (int i = 0; i < 10; ++i) {
    Swappable swappable = Swappable.class.cast(out);
    if (i % 2 > 0) {
        swappable.hotswap(outStreamEven);
    } else {
        swappable.hotswap(outStreamOdd);
    }
    writer.println("Line " + (i + 1));
    writer.flush();
}
System.out.println();
System.out.println("Odd lines output:");
System.out.println(outStreamOdd.toString());
System.out.println("Even lines output:");
System.out.println(outStreamEven.toString());

Note that the first delegate is even the null object.

Skip navigation links

Copyright © 2005–2016 Codehaus. All rights reserved.