View Javadoc
1 /* 2 Copyright (c) 2003, Laurent Caillette. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without modifica- 6 tion, are permitted provided that the following conditions are met: 7 8 1. Redistributions of source code must retain the above copyright notice, 9 this list of conditions and the following disclaimer. 10 11 2. Redistributions in binary form must reproduce the above copyright notice, 12 this list of conditions and the following disclaimer in the documentation 13 and/or other materials provided with the distribution. 14 15 3. The end-user documentation included with the redistribution, if any, must 16 include the following acknowledgment: "This product includes software 17 written by Laurent Caillette." 18 Alternately, this acknowledgment may appear in the software itself, if 19 and wherever such third-party acknowledgments normally appear. 20 21 4. The name "Laurent Caillette" must not be used to endorse or 22 promote products derived from this software without 23 prior written permission. For written permission, please contact 24 laurent.caillette@laposte.net 25 26 5. Products derived from this software may not be called 27 "Laurent Caillette", nor may "Laurent Caillette" appear 28 in their name, without prior written permission of the 29 author. 30 31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 41 */ 42 package tweed.invocation; 43 import java.io.ByteArrayInputStream; 44 import java.io.ByteArrayOutputStream; 45 import java.io.IOException; 46 import java.io.ObjectInputStream; 47 import java.io.ObjectOutputStream; 48 import tweed.context.Context; 49 50 /*** 51 * This invoker calls a {@link CommandExecutor} which resides in the same VM. 52 * Travel of the {@link ExternalizedCommand} between client and server 53 * is simulated with serialization / deserialization in memory. 54 * <p> 55 * This is especially useful for building tests. 56 * Command implementors should not manipulate this class directly. 57 * 58 * @author Laurent Caillette 59 * @version $Id$ 60 */ 61 public class LoopbackInvoker extends CommandInvoker { 62 63 private CommandExecutor executor ; 64 65 public LoopbackInvoker( Context context ) { 66 super( context ) ; 67 } 68 69 public CommandExecutor getExecutor() { 70 return executor ; 71 } 72 73 public void setExecutor( CommandExecutor executor ) { 74 this.executor = executor ; 75 } 76 77 /*** 78 * Serializes / deserializes <code>ExternalizedCommand</code> in memory 79 * to simulate travel between client and server. 80 * 81 * @param externalizedCommand 82 * @return an other instance of the <code>ExternalizedCommand</code> 83 * passed in input, after serialization /deserialization. 84 */ 85 protected ExternalizedCommand simulateTravel( 86 ExternalizedCommand externalizedCommand 87 ) throws IOException { 88 ByteArrayOutputStream baos = new ByteArrayOutputStream() ; 89 ObjectOutputStream oos = new ObjectOutputStream( baos ) ; 90 oos.writeObject( externalizedCommand ) ; 91 oos.close() ; 92 ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ) ; 93 ObjectInputStream ois = new ObjectInputStream( bais ) ; 94 ExternalizedCommand deserialized = null ; 95 try { 96 deserialized = 97 ( ExternalizedCommand ) ois.readObject() ; 98 } catch( ClassNotFoundException ex ) { 99 throw new RuntimeException( ex ); 100 } 101 ois.close() ; 102 return deserialized ; 103 } 104 105 protected ExternalizedCommand doInvoke( 106 ExternalizedCommand externalizedCommand 107 ) throws IOException { 108 ExternalizedCommand toExecute = simulateTravel( externalizedCommand ) ; 109 executor.execute( toExecute ) ; 110 ExternalizedCommand returned = simulateTravel( toExecute ) ; 111 return returned ; 112 } 113 114 }

This page was automatically generated by Maven