[jboss-cvs] JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi ...

Tom Elrod tom.elrod at jboss.com
Wed Jul 12 00:49:51 EDT 2006


  User: telrod  
  Date: 06/07/12 00:49:51

  Added:       src/main/org/jboss/remoting/transport/sslrmi     
                        SSLRMIClientInvoker.java SSLRMIServerInvoker.java
                        SerializableSSLClientSocketFactory.java
                        TransportClientFactory.java
                        TransportServerFactory.java
  Log:
  JBREM-496 & JBREM-492 & JBREM-535 - changed invoker loading to use a client and server factory instead of loading classes directly.  This allows users ability to plugin custom transport implementation with zero config.  Also added config for forcing remote call when client and server within same jvm (which was able to do previously using BYVALUE config, but not any more).  Also changed way to get hint if transport supports ssl to be provided by the transport factory instead of the transport itself.
  
  Revision  Changes    Path
  1.1      date: 2006/07/12 04:49:51;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi/SSLRMIClientInvoker.java
  
  Index: SSLRMIClientInvoker.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, 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.sslrmi;
  
  import org.jboss.remoting.ConnectionFailedException;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.Remoting;
  import org.jboss.remoting.transport.rmi.RMIClientInvoker;
  
  import java.util.HashMap;
  import java.util.Map;
  
  
  /**
   * 
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   * @version $Revision: 1.1 $
   * <p>
   * Copyright (c) Jun 11, 2006
   * </p>
   */
  public class SSLRMIClientInvoker extends RMIClientInvoker
  {
  
     public SSLRMIClientInvoker(InvokerLocator locator, Map configuration)
     {
        super(locator, configuration);
     }
  
     public SSLRMIClientInvoker(InvokerLocator locator)
     {
        super(locator);
     }
  
     protected void handleConnect() throws ConnectionFailedException
     {
        HashMap localConfig = new HashMap(configuration);
  
        // If a specific SocketFactory was passed in, use it.  If a SocketFactory was
        // generated from SSL parameters, discard it.  It will be recreated later by
        // SerializableSSLClientSocketFactory with any additional parameters sent
        // from server.
        if (socketFactory != null && !socketFactoryCreatedFromSSLParameters)
           localConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, socketFactory);
  
        // Save configuration for SerializableSSLClientSocketFactory.
        SerializableSSLClientSocketFactory.addLocalConfiguration(locator, localConfig);
  
        super.handleConnect();
     }
  }
  
  
  
  1.1      date: 2006/07/12 04:49:51;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi/SSLRMIServerInvoker.java
  
  Index: SSLRMIServerInvoker.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, 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.sslrmi;
  
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.security.SSLSocketBuilder;
  import org.jboss.remoting.transport.rmi.RMIServerInvoker;
  
  import javax.net.ServerSocketFactory;
  import javax.net.SocketFactory;
  import java.io.IOException;
  import java.rmi.server.RMIClientSocketFactory;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   *
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   * @version $Revision: 1.1 $
   * <p>
   * Copyright (c) Jun 9, 2006
   * </p>
   */
  public class SSLRMIServerInvoker extends RMIServerInvoker
  {
     public SSLRMIServerInvoker(InvokerLocator locator)
     {
        super(locator);
     }
  
  
     public SSLRMIServerInvoker(InvokerLocator locator, Map configuration)
     {
        super(locator, configuration);
     }
  
     protected RMIClientSocketFactory getRMIClientSocketFactory(String ignored)
     {
        // Remove from config map any properties relating to keystore and truststore.
        HashMap remoteConfig = new HashMap(configuration);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_ALIAS);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_PASSWORD);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_STORE_ALGORITHM);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_STORE_FILE_PATH);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_STORE_PASSWORD);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_KEY_STORE_TYPE);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_TRUST_STORE_ALGORITHM);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_TRUST_STORE_FILE_PATH);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_TRUST_STORE_PASSWORD);
        remoteConfig.remove(SSLSocketBuilder.REMOTING_TRUST_STORE_TYPE);
  
        // If server socket should use client mode, then default behavior will be for socket
        // to not use client mode.
        String serverSocketUseClientModeString
           = (String) configuration.get(SSLSocketBuilder.REMOTING_SERVER_SOCKET_USE_CLIENT_MODE);
        if (serverSocketUseClientModeString != null)
        {
           boolean serverSocketUseClientMode = Boolean.valueOf(serverSocketUseClientModeString).booleanValue();
           if (serverSocketUseClientMode)
           {
              String socketUseClientModeString
                 = (String) configuration.get(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE);
              if (socketUseClientModeString == null)
              {
                 remoteConfig.put(SSLSocketBuilder.REMOTING_SOCKET_USE_CLIENT_MODE, "false");
              }
           }
        }
  
        return new SerializableSSLClientSocketFactory(locator, getTimeout(), remoteConfig);
     }
  
  
     protected ServerSocketFactory getDefaultServerSocketFactory() throws IOException
     {
        SSLSocketBuilder builder = new SSLSocketBuilder(configuration);
        builder.setUseSSLServerSocketFactory(false);
        return builder.createSSLServerSocketFactory();
     }
     
     
     protected SocketFactory createSocketFactory(Map configuration)
     {
        return null;
     }
  }
  
  
  
  1.1      date: 2006/07/12 04:49:51;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi/SerializableSSLClientSocketFactory.java
  
  Index: SerializableSSLClientSocketFactory.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2006, 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.sslrmi;
  
  import org.jboss.logging.Logger;
  import org.jboss.remoting.Client;
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.Remoting;
  import org.jboss.remoting.security.SSLSocketBuilder;
  import org.jboss.remoting.transport.rmi.RemotingRMIClientSocketFactory;
  import org.jboss.remoting.util.socket.HandshakeRepeater;
  
  import javax.net.SocketFactory;
  import javax.net.ssl.HandshakeCompletedListener;
  import javax.net.ssl.SSLSocket;
  import java.io.IOException;
  import java.net.Socket;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * 
   * @author <a href="mailto:ron.sigal at jboss.com">Ron Sigal</a>
   * @version $Revision: 1.1 $
   * <p>
   * Copyright (c) Jun 9, 2006
   * </p>
   */
  public class SerializableSSLClientSocketFactory extends RemotingRMIClientSocketFactory
  {
     private static final long serialVersionUID = 3242156275483606618L;
     private static Logger log = Logger.getLogger(SerializableSSLClientSocketFactory.class);
     private static HashMap configMaps = new HashMap();
     private static Map socketFactories = new HashMap();
     
     private Map configuration;
     private InvokerLocator invokerLocator;
  
     
     static void addLocalConfiguration(InvokerLocator invokerLocator, Map localConfig)
     {
        configMaps.put(new ComparableHolder(invokerLocator), localConfig);
     }
     
     
     public SerializableSSLClientSocketFactory(InvokerLocator invokerLocator,
                                               int timeout,
                                               Map configuration)
     {
        super(null, invokerLocator.getHost(), timeout);
        this.invokerLocator = invokerLocator;
        this.configuration = new HashMap(configuration);
     }
     
     
     public Socket createSocket(String host, int port) throws IOException
     {
        // If invokerLocator isn't in configMaps, an SSLRMICLientInvoker has not been created
        // yet.  This call was probably made by an RMI thread, and is premature.
        ComparableHolder holder = new ComparableHolder(invokerLocator);
        if (!configMaps.containsKey(holder))
           return null;
        
        socketFactory = (SocketFactory) socketFactories.get(this);
        if (socketFactory == null)
        {
           try
           {
              // 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(configuration);
              Map localConfig = (Map) configMaps.get(holder);
              if (localConfig != null)
                 tempConfig.putAll(localConfig);
              
              if (tempConfig.containsKey(Remoting.CUSTOM_SOCKET_FACTORY))
              {
                 socketFactory = (SocketFactory) tempConfig.get(Remoting.CUSTOM_SOCKET_FACTORY);
              }
              else
              {
                 SSLSocketBuilder socketBuilder = new SSLSocketBuilder(tempConfig);
                 socketBuilder.setUseSSLSocketFactory( false );
                 socketFactory = socketBuilder.createSSLSocketFactory();
              }
           }
           catch (IOException e)
           {
              log.error(e);
              e.printStackTrace();
              throw new RuntimeException("Unable to create customized SSL socket factory", e);
           }
        }
        
        Socket s = super.createSocket(host, port);
        socketFactories.put(this, socketFactory);
        socketFactory = null;
  
        // need to check for handshake listener and add them if there is one
        Object obj = configuration.get(Client.HANDSHAKE_COMPLETED_LISTENER);
        if (obj != null && obj instanceof HandshakeCompletedListener)
        {
           SSLSocket sslSocket = (SSLSocket) s;
           HandshakeCompletedListener listener = (HandshakeCompletedListener) obj;
           establishHandshake(sslSocket, listener);
        }
        
        return s;
     }
  
  
     private void establishHandshake(SSLSocket sslSocket, HandshakeCompletedListener listener)
           throws IOException
     {
        HandshakeRepeater repeater = new HandshakeRepeater(listener);
        sslSocket.addHandshakeCompletedListener(repeater);
        sslSocket.getSession();
        repeater.waitForHandshake();
     }
  
     
     private static class ComparableHolder
     {
        private String protocol;
        private String host;
        private int port;
        private int hashCode;
        
        public ComparableHolder(InvokerLocator invokerLocator)
        {
           protocol = invokerLocator.getProtocol().toLowerCase();
           host = invokerLocator.getHost();
           port = invokerLocator.getPort();
           hashCode = protocol.hashCode() * host.hashCode() * port;
        }
        
        public boolean equals(Object obj)
        {
           if (obj == null || !(obj instanceof ComparableHolder))
              return false;
           
           ComparableHolder holder = (ComparableHolder) obj;
           
           return protocol.equals(holder.protocol.toLowerCase())
                  && host.equals(holder.host)
                  && port == holder.port;
        }
        
        public int hashCode()
        {
           return hashCode;
        }
        
     }
  }
  
  
  1.1      date: 2006/07/12 04:49:51;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi/TransportClientFactory.java
  
  Index: TransportClientFactory.java
  ===================================================================
  /*
  * 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.sslrmi;
  
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.transport.ClientFactory;
  import org.jboss.remoting.transport.ClientInvoker;
  
  import java.io.IOException;
  import java.util.Map;
  
  /**
   * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
   */
  public class TransportClientFactory implements ClientFactory
  {
     public ClientInvoker createClientInvoker(InvokerLocator locator, Map config) throws IOException
     {
        return new SSLRMIClientInvoker(locator, config);
     }
  
     public boolean supportsSSL()
     {
        return true;
     }
  
  }
  
  
  1.1      date: 2006/07/12 04:49:51;  author: telrod;  state: Exp;JBossRemoting/src/main/org/jboss/remoting/transport/sslrmi/TransportServerFactory.java
  
  Index: TransportServerFactory.java
  ===================================================================
  package org.jboss.remoting.transport.sslrmi;
  
  import org.jboss.remoting.InvokerLocator;
  import org.jboss.remoting.ServerInvoker;
  import org.jboss.remoting.transport.ServerFactory;
  
  import java.util.Map;
  
  /**
   * @author <a href="mailto:tom.elrod at jboss.com">Tom Elrod</a>
   */
  public class TransportServerFactory implements ServerFactory
  {
     public ServerInvoker createServerInvoker(InvokerLocator locator, Map config)
     {
        return new SSLRMIServerInvoker(locator, config);
     }
  
     public boolean supportsSSL()
     {
        return true;
     }
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list