001/* 002 * (c) 2005, 2009, 2010 ThoughtWorks Ltd 003 * All rights reserved. 004 * 005 * The software in this package is published under the terms of the BSD 006 * style license a copy of which has been included with this distribution in 007 * the LICENSE.txt file. 008 * 009 * Created on 26-Jul-2005 010 */ 011package proxytoys.examples.overview; 012 013import java.io.ByteArrayOutputStream; 014import java.io.OutputStream; 015import java.io.PrintWriter; 016 017import com.thoughtworks.proxy.factory.CglibProxyFactory; 018import com.thoughtworks.proxy.toys.hotswap.HotSwapping; 019import com.thoughtworks.proxy.toys.hotswap.Swappable; 020 021 022/** 023 * @author Jörg Schaible 024 */ 025public class HotSwapToyExample { 026 027 public static void packageOverviewExample1() { 028 ByteArrayOutputStream outStreamOdd = new ByteArrayOutputStream(); 029 ByteArrayOutputStream outStreamEven = new ByteArrayOutputStream(); 030 OutputStream out = HotSwapping.proxy(OutputStream.class) 031 .with(null) 032 .build(new CglibProxyFactory()); 033 PrintWriter writer = new PrintWriter(out); 034 for (int i = 0; i < 10; ++i) { 035 Swappable swappable = Swappable.class.cast(out); 036 if (i % 2 > 0) { 037 swappable.hotswap(outStreamEven); 038 } else { 039 swappable.hotswap(outStreamOdd); 040 } 041 writer.println("Line " + (i + 1)); 042 writer.flush(); 043 } 044 System.out.println(); 045 System.out.println("Odd lines output:"); 046 System.out.println(outStreamOdd.toString()); 047 System.out.println("Even lines output:"); 048 System.out.println(outStreamEven.toString()); 049 } 050 051 public static void main(String[] args) { 052 System.out.println(); 053 System.out.println(); 054 System.out.println("Running HotSwap Toy Examples"); 055 System.out.println(); 056 System.out.println("Example 1 of Package Overview:"); 057 packageOverviewExample1(); 058 } 059}