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 25-Jul-2005
010 */
011package proxytoys.examples.overview;
012
013import com.thoughtworks.proxy.factory.CglibProxyFactory;
014import com.thoughtworks.proxy.factory.StandardProxyFactory;
015import com.thoughtworks.proxy.toys.failover.Failover;
016
017import java.io.ByteArrayInputStream;
018import java.io.DataInput;
019import java.io.DataInputStream;
020import java.io.IOException;
021import java.text.DateFormat;
022import java.text.Format;
023import java.text.MessageFormat;
024import java.text.NumberFormat;
025import java.util.Date;
026
027
028/**
029 * @author Jörg Schaible
030 */
031public class FailoverToyExample {
032
033    public static void packageOverviewExample1() {
034        Format[] formats = new Format[]{
035                NumberFormat.getInstance(),
036                DateFormat.getDateInstance(),
037                new MessageFormat("{1}, {0}")
038        };
039        Format format = Failover.proxy(Format.class).with(formats).excepting(RuntimeException.class)
040                .build(new CglibProxyFactory());
041        System.out.println("Format a date: " + format.format(new Date()));
042        System.out.println("Format a message: " + format.format(new String[]{"John", "Doe"}));
043        System.out.println("Format a number: " + format.format(42));
044    }
045
046    public static void packageOverviewExample2() {
047        DataInput[] dataInputs = new DataInput[]{
048                new DataInputStream(new ByteArrayInputStream(new byte[]{0, 'A', 0, 'n', 0, ' '})),
049                new DataInputStream(new ByteArrayInputStream(new byte[]{
050                        0, 'e', 0, 'x', 0, 'a', 0, 'm', 0, 'p', 0, 'l', 0, 'e'})),};
051        DataInput dataInput = Failover.proxy(DataInput.class).with(dataInputs).excepting(IOException.class)
052                .build(new StandardProxyFactory());
053        StringBuffer buffer = new StringBuffer();
054        try {
055            while (buffer.append(dataInput.readChar()) != null)
056                ;
057        } catch (IOException e) {
058        }
059        System.out.println("Read: " + buffer.toString());
060    }
061
062    public static void main(String[] args) {
063        System.out.println();
064        System.out.println();
065        System.out.println("Running Failover Toy Examples");
066        System.out.println();
067        System.out.println("Example 1 of Package Overview:");
068        packageOverviewExample1();
069        System.out.println();
070        System.out.println("Example 2 of Package Overview:");
071        packageOverviewExample2();
072    }
073}