vinayc      2003/08/28 11:04:59

  Added:       client/impl/src/java/org/apache/altrmi/client/impl/rmi
                        RmiClientInvocationHandler.java
                        RmiFactoryHelper.java RmiHostContext.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/rmi/RmiClientInvocationHandler.java
  
  Index: RmiClientInvocationHandler.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 [EMAIL PROTECTED]
   *
   * 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.rmi;
  
  import java.net.MalformedURLException;
  import java.rmi.ConnectException;
  import java.rmi.ConnectIOException;
  import java.rmi.Naming;
  import java.rmi.NotBoundException;
  import java.rmi.RemoteException;
  import org.apache.altrmi.client.ClientMonitor;
  import org.apache.altrmi.client.impl.AbstractClientInvocationHandler;
  import org.apache.altrmi.common.ConnectionException;
  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.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.BadConnectionException;
  import org.apache.altrmi.common.RmiInvocationHandler;
  import org.apache.altrmi.common.ThreadPool;
  
  /**
   * Class RmiClientInvocationHandler
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public final class RmiClientInvocationHandler extends 
AbstractClientInvocationHandler
  {
  
      private RmiInvocationHandler m_rmiInvocationHandler;
      private String m_URL;
      private long m_lastRealRequest = System.currentTimeMillis();
  
      /**
       * Constructor RmiClientInvocationHandler
       *
       *
       * @param host
       * @param port
       *
       * @throws ConnectionException
       *
       */
      public RmiClientInvocationHandler( ThreadPool threadPool, ClientMonitor 
clientMonitor,
                                         ConnectionPinger connectionPinger,
                                         String host, int port ) throws 
ConnectionException
      {
  
          super(threadPool, clientMonitor, connectionPinger);
  
          m_URL = "rmi://" + host + ":" + port + "/" + 
RmiInvocationHandler.class.getName();
  
          try
          {
              m_rmiInvocationHandler = (RmiInvocationHandler)Naming.lookup( 
m_URL );
          }
          catch( NotBoundException nbe )
          {
              throw new ConnectionException(
                  "Cannot bind to the remote RMI service.  Either an IP or RMI 
issue." );
          }
          catch( MalformedURLException mfue )
          {
              throw new ConnectionException( "Malformed URL, host/port (" + 
host + "/" + port
                                                   + ") must be wrong: " + 
mfue.getMessage() );
          }
          catch( ConnectIOException cioe )
          {
              throw new BadConnectionException( "Cannot connect to remote RMI 
server. "
                      + "It is possible that transport mismatch");
          }
          catch( RemoteException re )
          {
              throw new ConnectionException( "Unknown Remote Exception : " + 
re.getMessage() );
          }
      }
  
      /**
       * Method tryReconnect
       *
       * @return
       *
       */
      protected boolean tryReconnect()
      {
  
          try
          {
              m_rmiInvocationHandler = (RmiInvocationHandler)Naming.lookup( 
m_URL );
  
              return true;
          }
          catch( Exception e )
          {
              return false;
          }
      }
  
      /**
       * Method handleInvocation
       *
       *
       * @param request
       *
       * @return
       *
       */
      public synchronized Reply handleInvocation( Request request )
      {
  
          if( request.getRequestCode() != RequestConstants.PINGREQUEST )
          {
              m_lastRealRequest = System.currentTimeMillis();
          }
  
          boolean again = true;
          Reply reply = null;
          int tries = 0;
          long start = 0;
  
          if( m_methodLogging )
          {
              start = System.currentTimeMillis();
          }
  
          while( again )
          {
              tries++;
  
              again = false;
  
              try
              {
                  reply = m_rmiInvocationHandler.handleInvocation( request );
  
                  if( reply.getReplyCode() >= 100 )
                  {
                      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 NotPublishedReply )
                      {
                          PublishedNameRequest pnr = 
(PublishedNameRequest)request;
  
                          throw new NotPublishedException( 
pnr.getPublishedServiceName(),
                                                           pnr.getObjectName() 
);
                      }
                  }
              }
              catch( RemoteException re )
              {
                  if( re instanceof ConnectException | re instanceof 
ConnectIOException )
                  {
                      int retryConnectTries = 0;
  
                      m_rmiInvocationHandler = null;
  
                      while( !tryReconnect() )
                      {
                          m_clientMonitor.serviceAbend(this.getClass(), 
retryConnectTries, re);
  
                          retryConnectTries++;
                      }
                  }
                  else
                  {
                      throw new InvocationException( "Unknown RMI problem : "
                                                           + re.getMessage() );
                  }
              }
          }
  
          if( m_methodLogging )
          {
              if( request instanceof MethodRequest )
              {
                  m_clientMonitor.methodCalled(
                          this.getClass(), ( (MethodRequest)request 
).getMethodSignature(),
                      System.currentTimeMillis() - start, "" );
              }
          }
  
          return reply;
      }
  
      /**
       * Method getLastRealRequest
       *
       *
       * @return
       *
       */
      public long getLastRealRequest()
      {
          return m_lastRealRequest;
      }
  }
  
  
  
  1.1                  
incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/rmi/RmiFactoryHelper.java
  
  Index: RmiFactoryHelper.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 [EMAIL PROTECTED]
   *
   * 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.rmi;
  
  import org.apache.altrmi.client.ClientMonitor;
  import org.apache.altrmi.client.ConnectionPinger;
  import org.apache.altrmi.client.Factory;
  import org.apache.altrmi.client.HostContext;
  import org.apache.altrmi.client.InterfaceLookup;
  import org.apache.altrmi.client.impl.AbstractFactoryHelper;
  import org.apache.altrmi.common.ConnectionException;
  import org.apache.altrmi.common.ThreadPool;
  
  /**
   * Class RmiFactoryHelper
   *
   *   "RMI:abcde.com:1234"
   *            0         :  1      : 2
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public class RmiFactoryHelper extends AbstractFactoryHelper
  {
      private ThreadPool m_threadPool;
      private ClientMonitor m_clientMonitor;
      private ConnectionPinger m_connectionPinger;
  
      /**
       * Method getInterfaceLookup
       *
       *
       * @param factoryString
       * @param interfacesClassLoader
       *
       * @return
       *
       */
      public InterfaceLookup getInterfaceLookup(
              String factoryString, ClassLoader interfacesClassLoader, boolean 
optimize)
              throws ConnectionException
      {
  
          // TODO maybe we should cache these.  Or the abstract parent class 
should.
          String[] terms = processFactoryString(factoryString);
          HostContext hc = new RmiHostContext(m_threadPool,  m_clientMonitor, 
m_connectionPinger, terms[1], Integer.parseInt(terms[2]));
          Factory af = createFactory(terms[3], hc, optimize);
  
          return af;
      }
  
      public void setThreadPool(ThreadPool threadPool)
      {
          m_threadPool = threadPool;
      }
  
      public void setClientMonitor(ClientMonitor clientMonitor)
      {
          m_clientMonitor = clientMonitor;
      }
  
      public void setConnectionPinger(ConnectionPinger connectionPinger)
      {
          m_connectionPinger = connectionPinger;
      }
  }
  
  
  
  1.1                  
incubator-altrmi/client/impl/src/java/org/apache/altrmi/client/impl/rmi/RmiHostContext.java
  
  Index: RmiHostContext.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 [EMAIL PROTECTED]
   *
   * 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.rmi;
  
  import org.apache.altrmi.client.impl.AbstractHostContext;
  import org.apache.altrmi.client.impl.DefaultClientMonitor;
  import org.apache.altrmi.client.impl.PerpetualConnectionPinger;
  import org.apache.altrmi.client.ConnectionPinger;
  import org.apache.altrmi.client.ClientMonitor;
  import org.apache.altrmi.common.ConnectionException;
  import org.apache.altrmi.common.DefaultThreadPool;
  import org.apache.altrmi.common.ThreadPool;
  
  /**
   * Class RmiHostContext
   *
   *
   * @author Paul Hammant
   * @version $Revision: 1.1 $
   */
  public class RmiHostContext extends AbstractHostContext
  {
  
      //TODO constr with classloader
  
      /**
       * Constructor RmiHostContext
       *
       *
       * @param host
       * @param port
       *
       * @throws ConnectionException
       *
       */
      public RmiHostContext( ThreadPool threadPool, ClientMonitor 
clientMonitor, ConnectionPinger connectionPinger, String host, int port ) 
throws ConnectionException
      {
          super( new RmiClientInvocationHandler( threadPool, clientMonitor, 
connectionPinger, host, port ) );
      }
  
      public static class WithSimpleDefaults extends RmiHostContext {
          public WithSimpleDefaults(String host, int port) throws 
ConnectionException
          {
              super(new DefaultThreadPool(), new DefaultClientMonitor(), new 
PerpetualConnectionPinger(),
                      host, port);
          }
  
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to