incubator-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vin...@apache.org
Subject cvs commit: incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/stream AbstractStreamClientInvocationHandler.java ClientCustomStreamReadWriter.java ClientObjectStreamReadWriter.java StreamInvocationHandler.java
Date Thu, 28 Aug 2003 18:06:57 GMT
vinayc      2003/08/28 11:06:57

  Added:       client/impl/src/java/org/apache/altrmi/client/impl/stream
                        AbstractStreamClientInvocationHandler.java
                        ClientCustomStreamReadWriter.java
                        ClientObjectStreamReadWriter.java
                        StreamInvocationHandler.java
  Log:
  Refactorize (includes modularize,mavenize & rest of the nice's)
  
  Revision  Changes    Path
  1.1                  incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/stream/AbstractStreamClientInvocationHandler.java
  
  Index: AbstractStreamClientInvocationHandler.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software
   *    itself, if and wherever such third-party acknowledgments
   *    normally appear.
   *
   * 4. The names "Incubator", "AltRMI", and "Apache Software Foundation"
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.altrmi.client.impl.stream;
  
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InterruptedIOException;
  import java.net.SocketException;
  import org.apache.altrmi.client.ClientMonitor;
  import org.apache.altrmi.client.impl.AbstractClientInvocationHandler;
  import org.apache.altrmi.client.impl.ClientStreamReadWriter;
  import org.apache.altrmi.client.InvocationException;
  import org.apache.altrmi.common.Reply;
  import org.apache.altrmi.common.Request;
  import org.apache.altrmi.common.MethodRequest;
  import org.apache.altrmi.client.NoSuchReferenceException;
  import org.apache.altrmi.common.NoSuchReferenceReply;
  import org.apache.altrmi.client.NotPublishedException;
  import org.apache.altrmi.client.NoSuchSessionException;
  import org.apache.altrmi.client.ConnectionPinger;
  import org.apache.altrmi.common.NotPublishedReply;
  import org.apache.altrmi.common.PublishedNameRequest;
  import org.apache.altrmi.common.TryLaterReply;
  import org.apache.altrmi.common.RequestConstants;
  import org.apache.altrmi.common.ReplyConstants;
  import org.apache.altrmi.common.ClientInvocationAbendReply;
  import org.apache.altrmi.common.NoSuchSessionReply;
  import org.apache.altrmi.common.ThreadPool;
  
  import javax.swing.*;
  
  /**
   * Class AbstractStreamClientInvocationHandler
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public abstract class AbstractStreamClientInvocationHandler extends AbstractClientInvocationHandler
  {
  
      private ClientStreamReadWriter m_objectReadWriter;
      private boolean m_methodLogging = false;
      private long m_lastRealRequest = System.currentTimeMillis();
      protected final ClassLoader m_interfacesClassLoader;
  
      /**
       * Constructor AbstractStreamClientInvocationHandler
       *
       * @param threadPool
       * @param clientMonitor
       * @param connectionPinger
       * @param interfacesClassLoader
       */
      public AbstractStreamClientInvocationHandler(ThreadPool threadPool, ClientMonitor clientMonitor,
ConnectionPinger connectionPinger, ClassLoader interfacesClassLoader)
      {
          super(threadPool, clientMonitor, connectionPinger);
          m_interfacesClassLoader = interfacesClassLoader;
      }
  
      /**
       * Method getInterfacesClassLoader
       *
       *
       * @return
       *
       */
      public ClassLoader getInterfacesClassLoader()
      {
          return m_interfacesClassLoader;
      }
  
      protected void setObjectReadWriter( ClientStreamReadWriter objectReadWriter )
      {
          m_objectReadWriter = objectReadWriter;
      }
  
      protected void requestWritten()
      {
      }
  
      /**
       * Method handleInvocation
       *
       *
       * @param request
       *
       * @return
       *
       */
      public synchronized Reply handleInvocation( Request request )
      {
          if( request.getRequestCode() != RequestConstants.PINGREQUEST )
          {
              m_lastRealRequest = System.currentTimeMillis();
          }
  
          try
          {
              while( true )
              {
                  boolean again = true;
                  Reply reply = null;
                  int tries = 0;
                  long start = 0;
  
                  if( m_methodLogging )
                  {
                      start = System.currentTimeMillis();
                  }
  
                  while( again )
                  {
                      tries++;
  
                      again = false;
  
                      try
                      {
                          long t1 = System.currentTimeMillis();
  
                          reply = (Reply)m_objectReadWriter.postRequest( request );
  
                          long t2 = System.currentTimeMillis();
  
                          if( reply.getReplyCode() >= 100 )
                          {
                              // special case for callabcks.
                              if (reply.getReplyCode() == ReplyConstants.CLIENTABEND)
                              {
                                  ClientInvocationAbendReply abendReply = (ClientInvocationAbendReply)
reply;
                                  throw abendReply.getIOException();
                              }
  
                              if( reply instanceof TryLaterReply )
                              {
                                  int millis = ( (TryLaterReply)reply ).getSuggestedDelayMillis();
  
                                  m_clientMonitor.serviceSuspended(this.getClass(), request,
tries,
                                                                              millis );
  
                                  again = true;
                              }
                              else if( reply instanceof NoSuchReferenceReply )
                              {
                                  throw new NoSuchReferenceException( ( (NoSuchReferenceReply)reply
)
                                                                      .getReferenceID() );
                              }
                              else if( reply instanceof NoSuchSessionReply )
                              {
                                  throw new NoSuchSessionException( ( (NoSuchSessionReply)reply
)
                                                                      .getSessionID() );
                              }
                              else if( reply instanceof NotPublishedReply )
                              {
                                  PublishedNameRequest pnr = (PublishedNameRequest)request;
  
                                  throw new NotPublishedException( pnr.getPublishedServiceName(),
                                                                   pnr.getObjectName() );
                              }
                          }
                      }
                      catch( IOException ioe )
                      {
                          if(isSafeEnd(ioe))
                          {
                              int retryConnectTries = 0;
  
                              again = true;
  
                              while( !tryReconnect() )
                              {
                                  m_clientMonitor.serviceAbend(this.getClass(), retryConnectTries,
ioe);
  
                                  retryConnectTries++;
                              }
                          }
                          else
                          {
                              ioe.printStackTrace();
  
                              throw new InvocationException(
                                  "IO Exception during invocation to server :" + ioe.getMessage()
);
                          }
                      }
                  }
                  if( m_methodLogging )
                  {
                      if( request instanceof MethodRequest )
                      {
                          m_clientMonitor.methodCalled(
                                  this.getClass(), ( (MethodRequest)request ).getMethodSignature(),
                              System.currentTimeMillis() - start, "" );
                      }
                  }
  
                  return reply;
              }
          }
          catch( ClassNotFoundException e )
          {
              throw new InvocationException( "Class definition missing on Deserialization:
"
                                                   + e.getMessage() );
          }
      }
  
      private boolean isSafeEnd(IOException ioe) {
          if (ioe instanceof SocketException | ioe instanceof EOFException
                              | ioe instanceof InterruptedIOException) {
                                  return true;
                              }
          if (ioe.getMessage() != null) {
              String msg = ioe.getMessage();
              if (msg.equals("Read end dead") | msg.equals("Pipe closed")) {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Method getLastRealRequest
       *
       *
       * @return
       *
       */
      public long getLastRealRequest()
      {
          return m_lastRealRequest;
      }
  }
  
  
  
  1.1                  incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/stream/ClientCustomStreamReadWriter.java
  
  Index: ClientCustomStreamReadWriter.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software
   *    itself, if and wherever such third-party acknowledgments
   *    normally appear.
   *
   * 4. The names "Incubator", "AltRMI", and "Apache Software Foundation"
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.altrmi.client.impl.stream;
  
  import java.io.BufferedOutputStream;
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import org.apache.altrmi.common.Reply;
  import org.apache.altrmi.common.Request;
  import org.apache.altrmi.common.SerializationHelper;
  import org.apache.altrmi.common.ConnectionException;
  import org.apache.altrmi.client.impl.ClientStreamReadWriter;
  
  /**
   * Class ClientCustomStreamReadWriter
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public class ClientCustomStreamReadWriter implements ClientStreamReadWriter
  {
  
      private DataInputStream m_dataInputStream;
      private DataOutputStream m_dataOutputStream;
      private ClassLoader m_interfacesClassLoader;
  
  
  
      /**
       * Constructor ClientCustomStreamReadWriter
       *
       *
       * @param inputStream
       * @param outputStream
       * @param interfacesClassLoader
       *
       * @throws ConnectionException
       *
       */
      public ClientCustomStreamReadWriter(
          InputStream inputStream, OutputStream outputStream, ClassLoader interfacesClassLoader
)
          throws ConnectionException
      {
  
          m_dataOutputStream = new DataOutputStream( new BufferedOutputStream( outputStream
) );
          m_dataInputStream = new DataInputStream( inputStream );
          m_interfacesClassLoader = interfacesClassLoader;
      }
  
      public synchronized Reply postRequest( Request request )
          throws IOException, ClassNotFoundException
      {
  
          writeRequest( request );
  
          Reply r = readReply();
  
          return r;
      }
  
      private void writeRequest( Request request ) throws IOException
      {
  
          byte[] aBytes = SerializationHelper.getBytesFromInstance( request );
  
          m_dataOutputStream.writeInt( aBytes.length );
          m_dataOutputStream.write( aBytes );
          m_dataOutputStream.flush();
      }
  
      private Reply readReply() throws IOException, ClassNotFoundException
      {
  
          int byteArraySize = m_dataInputStream.readInt();
          byte[] byteArray = new byte[ byteArraySize ];
          int pos = 0;
          int cnt = 0;
          // Loop here until the entire array has been read in.
          while( pos < byteArraySize )
          {
              int read = m_dataInputStream.read( byteArray, pos, byteArraySize - pos );
              pos += read;
              cnt++;
          }
          return (Reply)SerializationHelper.getInstanceFromBytes( byteArray,
                                                                        m_interfacesClassLoader
);
      }
  }
  
  
  
  1.1                  incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/stream/ClientObjectStreamReadWriter.java
  
  Index: ClientObjectStreamReadWriter.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software
   *    itself, if and wherever such third-party acknowledgments
   *    normally appear.
   *
   * 4. The names "Incubator", "AltRMI", and "Apache Software Foundation"
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.altrmi.client.impl.stream;
  
  import org.apache.altrmi.client.impl.ClientStreamReadWriter;
  import org.apache.altrmi.common.BadConnectionException;
  import org.apache.altrmi.common.ConnectionException;
  import org.apache.altrmi.common.Reply;
  import org.apache.altrmi.common.Request;
  
  import javax.swing.*;
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  
  /**
   * Class ClientObjectStreamReadWriter
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public class ClientObjectStreamReadWriter implements ClientStreamReadWriter
  {
  
      private ObjectInputStream m_objectInputStream;
      private ObjectOutputStream m_objectOutputStream;
  
      /**
       * Constructor ClientObjectStreamReadWriter
       *
       *
       * @param inputStream
       * @param outputStream
       * @throws ConnectionException
       *
       */
      public ClientObjectStreamReadWriter(
          InputStream inputStream, OutputStream outputStream)
          throws ConnectionException
      {
  
          try
          {
              m_objectOutputStream = new ObjectOutputStream(outputStream);
              m_objectInputStream = new ObjectInputStream(inputStream);
          }
          catch(EOFException eofe)
          {
              throw new BadConnectionException( "Cannot connect to remote AltRMI server. Have
we a mismatch on transports?");
          }
          catch(IOException ioe)
          {
              throw new ConnectionException( "Some problem instantiating ObjectStream classes:
" + ioe.getMessage() );
          }
      }
  
      public synchronized Reply postRequest( Request request )
          throws IOException, ClassNotFoundException
      {
          writeRequest( request );
          return readReply();
      }
  
      private void writeRequest( Request request ) throws IOException
      {
  
          m_objectOutputStream.writeObject( request );
          m_objectOutputStream.flush();
  
          //m_objectOutputStream.reset();
      }
  
      private Reply readReply() throws IOException, ClassNotFoundException
      {
          return (Reply)m_objectInputStream.readObject();
      }
  }
  
  
  
  1.1                  incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/stream/StreamInvocationHandler.java
  
  Index: StreamInvocationHandler.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software
   *    itself, if and wherever such third-party acknowledgments
   *    normally appear.
   *
   * 4. The names "Incubator", "AltRMI", and "Apache Software Foundation"
   *    must not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation. For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.altrmi.client.impl.stream;
  
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InterruptedIOException;
  import java.net.SocketException;
  import org.apache.altrmi.client.ClientMonitor;
  import org.apache.altrmi.client.impl.AbstractClientInvocationHandler;
  import org.apache.altrmi.client.impl.ClientStreamReadWriter;
  import org.apache.altrmi.client.InvocationException;
  import org.apache.altrmi.common.Reply;
  import org.apache.altrmi.common.Request;
  import org.apache.altrmi.common.MethodRequest;
  import org.apache.altrmi.client.NoSuchReferenceException;
  import org.apache.altrmi.common.NoSuchReferenceReply;
  import org.apache.altrmi.client.NotPublishedException;
  import org.apache.altrmi.client.NoSuchSessionException;
  import org.apache.altrmi.client.ConnectionPinger;
  import org.apache.altrmi.common.NotPublishedReply;
  import org.apache.altrmi.common.PublishedNameRequest;
  import org.apache.altrmi.common.TryLaterReply;
  import org.apache.altrmi.common.RequestConstants;
  import org.apache.altrmi.common.ReplyConstants;
  import org.apache.altrmi.common.ClientInvocationAbendReply;
  import org.apache.altrmi.common.NoSuchSessionReply;
  import org.apache.altrmi.common.ThreadPool;
  
  /**
   * Class StreamInvocationHandler
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public abstract class StreamInvocationHandler extends AbstractClientInvocationHandler
  {
  
      private ClientStreamReadWriter m_objectReadWriter;
      private boolean m_methodLogging = false;
      private long m_lastRealRequest = System.currentTimeMillis();
      protected final ClassLoader m_interfacesClassLoader;
  
      /**
       * Constructor StreamInvocationHandler
       *
       *
       * @param threadPool
       * @param clientMonitor
       * @param connectionPinger
       * @param interfacesClassLoader
       */
      public StreamInvocationHandler( ThreadPool threadPool, ClientMonitor clientMonitor,
ConnectionPinger connectionPinger, ClassLoader interfacesClassLoader )
      {
          super(threadPool, clientMonitor, connectionPinger);
          m_interfacesClassLoader = interfacesClassLoader;
          m_methodLogging = clientMonitor.methodLogging();
  
      }
  
      /**
       * Method getInterfacesClassLoader
       *
       *
       * @return
       *
       */
      public ClassLoader getInterfacesClassLoader()
      {
          return m_interfacesClassLoader;
      }
  
      protected void setObjectReadWriter( ClientStreamReadWriter objectReadWriter )
      {
          m_objectReadWriter = objectReadWriter;
      }
  
      protected void requestWritten()
      {
      }
  
      /**
       * Method handleInvocation
       *
       *
       * @param request
       *
       * @return
       *
       */
      public synchronized Reply handleInvocation( Request request )
      {
  
          if( request.getRequestCode() != RequestConstants.PINGREQUEST )
          {
              m_lastRealRequest = System.currentTimeMillis();
          }
  
          try
          {
              while( true )
              {
                  boolean again = true;
                  Reply reply = null;
                  int tries = 0;
                  long start = 0;
  
                  if( m_methodLogging )
                  {
                      start = System.currentTimeMillis();
                  }
  
                  while( again )
                  {
                      tries++;
  
                      again = false;
  
                      try
                      {
                          long t1 = System.currentTimeMillis();
  
                          reply = (Reply)m_objectReadWriter.postRequest( request );
  
                          long t2 = System.currentTimeMillis();
  
                          if( reply.getReplyCode() >= 100 )
                          {
                              // special case for callabcks.
                              if (reply.getReplyCode() == ReplyConstants.CLIENTABEND)
                              {
                                  ClientInvocationAbendReply abendReply = (ClientInvocationAbendReply)
reply;
                                  throw abendReply.getIOException();
                              }
  
                              if( reply instanceof TryLaterReply )
                              {
                                  int millis = ( (TryLaterReply)reply ).getSuggestedDelayMillis();
  
                                  m_clientMonitor.serviceSuspended(this.getClass(), request,
tries,
                                                                              millis );
  
                                  again = true;
                              }
                              else if( reply instanceof NoSuchReferenceReply )
                              {
                                  throw new NoSuchReferenceException( ( (NoSuchReferenceReply)reply
)
                                                                      .getReferenceID() );
                              }
                              else if( reply instanceof NoSuchSessionReply )
                              {
                                  throw new NoSuchSessionException( ( (NoSuchSessionReply)reply
)
                                                                      .getSessionID() );
                              }
                              else if( reply instanceof NotPublishedReply )
                              {
                                  PublishedNameRequest pnr = (PublishedNameRequest)request;
  
                                  throw new NotPublishedException( pnr.getPublishedServiceName(),
                                                                   pnr.getObjectName() );
                              }
                          }
                      }
                      catch( IOException ioe )
                      {
                          if(isSafeEnd(ioe))
                          {
                              int retryConnectTries = 0;
  
                              again = true;
  
                              while( !tryReconnect() )
                              {
                                  m_clientMonitor.serviceAbend(this.getClass(), retryConnectTries,
ioe);
  
                                  retryConnectTries++;
                              }
                          }
                          else
                          {
                              ioe.printStackTrace();
  
                              throw new InvocationException(
                                  "IO Exception during invocation to server :" + ioe.getMessage()
);
                          }
                      }
                  }
  
                  if( m_methodLogging )
                  {
                      if( request instanceof MethodRequest )
                      {
                          m_clientMonitor.methodCalled(
                                  this.getClass(), ( (MethodRequest)request ).getMethodSignature(),
                              System.currentTimeMillis() - start, "" );
                      }
                  }
  
                  return reply;
              }
          }
          catch( ClassNotFoundException e )
          {
              throw new InvocationException( "Class definition missing on Deserialization:
"
                                                   + e.getMessage() );
          }
      }
  
      private boolean isSafeEnd(IOException ioe) {
          if (ioe instanceof SocketException | ioe instanceof EOFException
                              | ioe instanceof InterruptedIOException) {
                                  return true;
                              }
          if (ioe.getMessage() != null) {
              String msg = ioe.getMessage();
              if (msg.equals("Read end dead") | msg.equals("Pipe closed")) {
                  return true;
              }
          }
          return false;
      }
  
      /**
       * Method getLastRealRequest
       *
       *
       * @return
       *
       */
      public long getLastRealRequest()
      {
          return m_lastRealRequest;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@incubator.apache.org
For additional commands, e-mail: cvs-help@incubator.apache.org


Mime
View raw message