001/* 002 * (c) 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 13-Oct-2009 010 */ 011package proxytoys.examples.overview; 012 013import com.thoughtworks.proxy.factory.CglibProxyFactory; 014import com.thoughtworks.proxy.toys.future.Future; 015import org.w3c.dom.Document; 016import org.xml.sax.InputSource; 017import org.xml.sax.SAXException; 018 019import javax.xml.parsers.DocumentBuilder; 020import javax.xml.parsers.DocumentBuilderFactory; 021import javax.xml.parsers.ParserConfigurationException; 022import java.io.IOException; 023import java.io.Reader; 024import java.io.StringReader; 025 026 027/** 028 * @author Paul Hammant 029 * @author Joerg Schaible 030 */ 031public class FutureToyExample { 032 033 public static void packageOverviewExample1() throws InterruptedException, ParserConfigurationException, SAXException, IOException { 034 DocumentBuilder documentBuilder = Future.proxy(DocumentBuilderFactory.newInstance().newDocumentBuilder()) 035 .build(new CglibProxyFactory()); 036 Document document = documentBuilder.parse(new SlowInputSource(new StringReader("<root/>"))); 037 System.out.println("Root document name: " + document.getDocumentElement().getNodeName()); 038 Thread.sleep(200); // should do something more useful here 039 System.out.println("Root document name: " + document.getDocumentElement().getNodeName()); 040 } 041 042 043 public static void main(String[] args) throws InterruptedException, ParserConfigurationException, SAXException, IOException { 044 System.out.println(); 045 System.out.println(); 046 System.out.println("Running Future Toy Example"); 047 System.out.println(); 048 System.out.println("Example 1 of Package Overview:"); 049 packageOverviewExample1(); 050 } 051 052 private static class SlowInputSource extends InputSource { 053 public SlowInputSource(Reader characterStream) { 054 super(characterStream); 055 } 056 057 @Override 058 public Reader getCharacterStream() { 059 try { 060 Thread.sleep(100); 061 } catch (InterruptedException e) { 062 } 063 return super.getCharacterStream(); 064 } 065 066 } 067}