A toy to hot swap instances.
The package provides a proxy factory creating proxies, that can hot swap instances. Main component is the {@linkplain com.thoughtworks.proxy.toys.hotswap.HotSwapping HotSwapping toy}, a utility class creating these proxies. Such a proxy contains an instance of a {@link com.thoughtworks.proxy.toys.hotswap.HotSwappingInvoker} that delegates all calls. The implementation subclasses the {@linkplain com.thoughtworks.proxy.toys.delegate.Delegating Delegating toy}. The proxy itself implements the additional {@link com.thoughtworks.proxy.toys.hotswap.Swappable 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 = (OutputStream)HotSwapping.object(OutputStream.class, new CglibProxyFactory(), null); PrintWriter writer = new PrintWriter(out); for(int i = 0; i < 10; ++i) { Swappable swappable = (Swappable)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.