LocalTransport.java :  » Rule-Engine » Mandarax » org » mandarax » jdbc » client » local » Java Open Source

Java Open Source » Rule Engine » Mandarax 
Mandarax » org » mandarax » jdbc » client » local » LocalTransport.java
package org.mandarax.jdbc.client.local;

/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.*;
import org.mandarax.jdbc.server.*;
import org.mandarax.jdbc.server.local.*;
import org.mandarax.jdbc.rpc.*;


/**
 * A dummy that uses local method calls. Mainly used for testing the jdbc
 * client-server implementation.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.2 <29 December 2004>
 * @since 3.0
 */
public class LocalTransport implements Transport {
  private Serializer serializer = new XMLSerializer();
  private ServerFacade serverFacade = new LocalServerFacade();
  
  /**
   * Constructor.
   */
  public LocalTransport() {
    super();
  }
  /**
   * Invoke a call.
   * @param call a call
   * @throws CallException
   * @throws java.io.IOException
   */
  public Object perform(Call call) throws CallException {
    try {
      // serialize, use a byte buffer for simulation - this is client code  
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      serializer.write(call,out);
      out.close();
    
      // simulated transport happens here - this is server code
      byte[] data = out.toByteArray();
      ByteArrayInputStream in = new ByteArrayInputStream(data);
      Call receivedCall = (Call)serializer.read(in);
    
      // invoke call - this is server code
      CallResult result = serverFacade.perform(receivedCall);
      out = new ByteArrayOutputStream();
      serializer.write(result,out);
      out.close();
    
      // deserialize - this is client code
      data = out.toByteArray();
      in = new ByteArrayInputStream(data);
      CallResult receivedResult = (CallResult)serializer.read(in);
      in.close();
    
      if (receivedResult instanceof ExceptionResult) {
        throw new CallException(((ExceptionResult)receivedResult).getMessage());
      }
      if (receivedResult instanceof ReturnValue) {
        return ((ReturnValue)receivedResult).getValue();
      }
      return null;
    }
    catch (IOException x) {
      throw new CallException(x.getMessage());
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.