Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This 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.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
 package org.jboss.remoting.transport.rmi;
 
 import java.util.Map;
 
 
 import  org.jboss.logging.Logger;
RemotingRMIClientSocketFactory provides two services to RMIServerInvoker.
  1. It can be parameterized by a host name, allowing RMIServerInvoker to supply RMI with a factory which creates sockets connected to a specified host name.

  2. It can be parameterized by a SocketFactory allowing RMIServerInvoker to supply RMI with a factory facility which creates specialized sockets.

if the SocketFactory parameter is specified, then the RemotingRMIClientSocketFactory should be used with a matching instance of RemotingRMIServerSocketFactory with a compatible ServerSocketFactory.

If the SocketFactory parameter is not specified, an instance of java.net.Socket will be created by default.

Although there is no apparent need for the host name parameter, since the createSocket() method receives a host name, it seems that for a server object bound to localhost, the RMI runtime will pass to createSocket() one of the IP addresses for which the host is configured (other than 127.0.0.1), resulting in a failure to retrieve the object from the Registry. If a host name is passed to a RemotingRMIClientFactory constructor, it will override the host name passed to createSocket() In particular, parameterizing RemotingRMIClientSocketFactory with localhost will allow the retrieval of objects bound to localhost.

Author(s):
Ron Sigal
Version:
$Revision: 2389 $

Copyright (c) 2005

 
 
 {
    static final long serialVersionUID;
 
    protected static Logger log = Logger.getLogger(RemotingRMIClientSocketFactory.class);
    protected static HashMap configMaps = new HashMap();
    protected static Map socketFactories = new HashMap();
 
    protected Map configuration;
    protected InvokerLocator invokerLocator;
    
    transient protected SocketFactory socketFactory;
    private int timeout = 60000;
 
    // The commented code below is from an attempt to incorporate a <code>java.lang.reflect.Constructor</code>
    // parameter to provide a very general way to create sockets.  The problem is that
    // <code>Constructor</code> does not implement <code>Serializable</code>, which is necessary to
    // allow the <code>RemotingRMIClientSocketFactory</code> to be transmitted to the client.  The
    // code is left in place because it could be resurrected by passing in a class name and parameter
    // types to specify a constructor.  Fortunately, <code>java.lang.Class</code> does implement
    // <code>Serializable</code>.
 
 //   private Constructor constructor;
 //   private Object[] args;
 //   private int hostPosition;
//   private int portPosition;
   protected String hostName;
   static
   {
      if(Version.getDefaultVersion() == .)
      {
          = -7491556589517716155L;
      }
      else
      {
          = -3039839695840773968L;
      }
   }
   
   public static void addLocalConfiguration(InvokerLocator invokerLocatorMap localConfig)
   {
      .debug("adding local configuration for: " + invokerLocator);
      .put(new ComparableHolder(invokerLocator), localConfig);
   }


   

Parameters:
locator
hostName
timeout
config
                                         String hostName,
                                         int timeout,
                                         Map config)
   {
      this. = locator;
      this. = hostName;
      this. = timeout;
      this. = new HashMap(config);
   }
//   public RemotingRMISocketFactory(Constructor constructor, Object[] args, int hostPosition, int portPosition)
//   throws ClassNotFoundException, NoSuchMethodException
//   {
//      this.constructor = constructor;
//      this.args = args;
//      this.portPosition = portPosition;
//   }


   
Creates a new socket. If a SocketFactory was passed to the constructor, it will be used. Otherwise, a java.net.Socket will be created by default.

Parameters:
host host to which socket should be connected
port port to which socket should be connected
Returns:
new socket
Throws:
IOException if there is a problem creating a socket
   public Socket createSocket(String hostint portthrows IOException
   {
      // If invokerLocator isn't in configMaps, an RMICLientInvoker has not been created
      // yet.  This call was probably made by an RMI thread, and is premature.  Best attempt
      // is to return a vanilla socket.
      
      // Note: the previous observation might be incorrect.  The phenomenon of missing
      // entries in configMaps might simply have been an error in the definition of
      // ComparableHolder.equals.  See JBREM-697.
      if ( != null)
      {
         ComparableHolder holder = new ComparableHolder();
         if (!.containsKey(holder))
         {
            .warn("unrecognized invoker locator: " + );
            .warn("unable to retrieve socket factory: returning plain socket");
            return new Socket(hostport);
         }
          = retrieveSocketFactory(holder);
      }
      String effectiveHost =  != null ?  : host;
      Socket socket = null;
      
      if( != null)
      {
         socket = .createSocket(effectiveHostport);
      }
//      if (constructor != null)
//      {
//         if (hostPosition != -1)
//            args[hostPosition] = host;
//
//         if (portPosition != -1)
//            args[portPosition] = new Integer(port);
//
//         try
//         {
//            return (Socket) constructor.newInstance(args);
//         }
//         catch (Exception e)
//         {
//            throw new IOException(e.getMessage());
//         }
//      }
      else
      {
         socket = new Socket(effectiveHostport);
      }
      socket.setSoTimeout();
       = null;
      return socket;
   }
      throws IOException
   {
      SocketFactory sf = (SocketFactory.get(this);
      if (sf == null)
      {
         // We want to keep the local configuration map, which might contain a
         // SocketFactory, separate from the configuration map, which is meant
         // to contain only serializable objects.
         Map tempConfig = new HashMap();
         Map localConfig = (Map.get(holder);
         if (localConfig != null)
            tempConfig.putAll(localConfig);
         if (tempConfig.containsKey(.))
         {
            sf = (SocketFactorytempConfig.get(.);
         }
         
         if (sf == null)
         {
            sf = SocketFactory.getDefault();
            sf = AbstractInvoker.wrapSocketFactory(sftempConfig);
         }
         
         .put(thissf);
      }
      return sf;
   }
   
   protected static class ComparableHolder
   {
      private String protocol;
      private InetAddress host;
      private int port;
      private int hashCode;
      public ComparableHolder(InvokerLocator invokerLocator)
      {
          = invokerLocator.getProtocol().toLowerCase();
         
         try
         {
             = InetAddress.getByName(invokerLocator.getHost());
         }
         catch (UnknownHostException e)
         {
            .error("unable to resolve host: " + invokerLocator.getHost());
         }
         
          = invokerLocator.getPort();
          = .hashCode() * .hashCode() * ;
      }
      public boolean equals(Object obj)
      {
         if (obj == null || !(obj instanceof ComparableHolder))
            return false;
         ComparableHolder holder = (ComparableHolderobj;
         return .equals(holder.protocol.toLowerCase())
                && .equals(holder.host)
                &&  == holder.port;
      }
      public int hashCode()
      {
         return ;
      }
   }
New to GrepCode? Check out our FAQ X