Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.samples.config.factories;
 
 import java.io.File;
 
 
 import  junit.framework.TestCase;
 
 import  org.jboss.logging.Logger;
These methods illustrate configuring socket factories and server socket factories on the server side and on the client side. The numbered options mentioned refer to the lists of configuration options discussed in the Remoting documentation in the subsections "Server side configuration" and "Client side configuration" of the section called "Socket factories and server socket factories".

The configuration options illustrated in this class are specific to SSL sockets and server sockets. The methods getDefaultServerSocketFactory(), getDefaultSocketFactory(), getDefaultCallbackServerSocketFactory() and getDefaultCallbackSocketFactory() illustrate the use of the class SSLSocketBuilder to create custom SSL socket factories and SSL server socket factories.

Author(s):
Ron Sigal

Copyright (c) Jul 20, 2006

 
 public class FactoryConfigSSLSample extends TestCase
 {
    private static Logger log = Logger.getLogger(FactoryConfigSSLSample.class);
   
   
   
   
This test illustrates the following set of configuration options:

sidefactoryoption
server sideserver socket4
server sidesocket 4
client sideserver socket1
client sidesocket 1
 
    public void testFactoriesByPassingMBeanInXml()
    {
       try
       {
          /////////////////////////////////////
          /////    Set up server side.     //// 
          /////////////////////////////////////
          HashMap sconfig = new HashMap();
          
          // Make callback Client use remote invoker.
          sconfig.put(."true");
            
         // Create ServerSocketFactory MBean.
         ServerSocketFactory service = getDefaultServerSocketFactory();
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
         ObjectName objName = new ObjectName(serverSocketFactoryName);
         MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
         mbeanServer.registerMBean(serviceobjName);
         
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(sconfig);
         mbeanServer.registerMBean(connectornew ObjectName("test:type=connector"));
         
         // Set xml configuration element.
         StringBuffer buf = new StringBuffer();
         buf.append("<?xml version=\"1.0\"?>\n");
         buf.append("<config>");
         buf.append(   "<invoker transport=\"" + getTransport() +"\">");
         buf.append(      "<attribute name=\"serverBindAddress\">" + getHostName() + "</attribute>");
         buf.append(      "<attribute name=\"serverBindPort\">" + freeport + "</attribute>");
         buf.append(      "<attribute name=\"serverSocketFactory\">" );
         buf.append(          serverSocketFactoryName);
         buf.append(      "</attribute>");
         buf.append(   "</invoker>");
         buf.append("</config>");
         ByteArrayInputStream bais = new ByteArrayInputStream(buf.toString().getBytes());
         Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
         connector.setConfiguration(xml.getDocumentElement());
         
         connector.create();
         connector.addInvocationHandler("sample"new SampleInvocationHandler());
         connector.start();
         
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         // Make Client use remote invoker.
         cconfig.put(."true");
         
         // Create Client.
         Client client = new Client(locatorcconfig);
         
         // Set SocketFactory in Client.
         // Note. There is no provision for using xml configuration on client side.
         SocketFactory sf = getDefaultSocketFactory();
         client.setSocketFactory(sf);
         client.connect();
         ..println(getName() + ": " + client.invoke("test invoke()"));
         
         //////////////////////////////////////////////
         /////       Set up callback handling.     //// 
         //////////////////////////////////////////////
         // Start callback Connector.
         // Note: there is no provision for using MBeanServer on client side.
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
         callbackConnector.setServerSocketFactory(ssf);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         callbackConnector.start();
         
         // Add callback handler.
         CallbackHandler callbackHandler = new FactoryConfigSample.CallbackHandler();
         String callbackHandleObject = "myCallbackHandleObject";
         client.addListener(callbackHandlercallbackLocatorcallbackHandleObject);
         
         client.disconnect();
         callbackConnector.stop();
         connector.stop();
      }
      catch (Throwable t)
      {
         .error(t);
         t.printStackTrace();
         fail();
      }
   }
   
   
   
This test illustrates the following set of configuration options:

sidefactoryoption
server sideserver socket6
server sidesocket 5
client sideserver socket1
client sidesocket 1

Note. There is no provision for using an MBeanServer on the client side.

   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         
         // Put ServerSocketFactory MBean name in config map.
         ServerSocketFactory service = getDefaultServerSocketFactory();
         String serverSocketFactoryName = "jboss:type=serversocketfactory";
         ObjectName objName = new ObjectName(serverSocketFactoryName);
         MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer();
         mbeanServer.registerMBean(serviceobjName);
         sconfig.put(.serverSocketFactoryName);
         
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         // Start Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         mbeanServer.registerMBean(connectornew ObjectName("test:type=connector"));
         connector.create();
         connector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         connector.start();
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         
         // Make Client use remote invoker.
         cconfig.put(."true");
         
         // Create Client.
         Client client = new Client(locatorcconfig);
         
         // Set server socket factory on Client.
         // Note: There is no provision for using MBeanServer on client side.         
         SocketFactory sf = getDefaultSocketFactory();
         client.setSocketFactory(sf);
         client.connect();
         ..println(getName() + ": " + client.invoke("test invoke()"));
         
         //////////////////////////////////////////////
         /////       Set up callback handling.     //// 
         //////////////////////////////////////////////
         // Start callback Connector.
         // Note: there is no provision for using MBeanServer on client side.
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
         callbackConnector.setServerSocketFactory(ssf);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         callbackConnector.start();
         
         // Add callback handler.
         CallbackHandler callbackHandler = new FactoryConfigSample.CallbackHandler();
         String callbackHandleObject = "myCallbackHandleObject";
         client.addListener(callbackHandlercallbackLocatorcallbackHandleObject);
         client.disconnect();
         callbackConnector.stop();
         connector.stop();
      }
      catch (Throwable t)
      {
         .error(t);
         t.printStackTrace();
         fail();
      }
   }
   
   
   
This test illustrates the following set of configuration options:

sidefactoryoption
server sideserver socket8
server sidesocket 8
client sideserver socket6
client sidesocket 5
   public void testFactoriesFromSSLParameters()
   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Put SSL keystore parameters in config map.
         sconfig.put(."false");
         sconfig.put(."JKS");
         String keyStoreFilePath = getKeystoreFilePath();
         sconfig.put(.keyStoreFilePath);
         sconfig.put(."unit-tests-server");
         
         // Start Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         connector.create();
         connector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         connector.start();
         
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         
         // Make Client use remote invoker.
         cconfig.put(."true");
         
         // Put SSL parameters in config map.
         cconfig.put(."true");
         cconfig.put(."JKS");
         String trustStoreFilePath = getTruststoreFilePath();
         cconfig.put(.trustStoreFilePath);
         cconfig.put(."unit-tests-client");
         
         // Create Client.
         Client client = new Client(locatorcconfig);
         client.connect();
         ..println(getName() + ": " + client.invoke("test invoke()"));
   
         
         //////////////////////////////////////////////
         /////       Set up callback handling.     //// 
         //////////////////////////////////////////////
         // Start callback Connector.
         HashMap cbconfig = new HashMap();
         
         // Put SSL parameters in config map.
         cbconfig.put(."true");
         cbconfig.put(."JKS");
         cbconfig.put(.trustStoreFilePath);
         cbconfig.put(."unit-tests-client");
         
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         callbackConnector.start();
         
         // Add callback handler.
         CallbackHandler callbackHandler = new CallbackHandler();
         String callbackHandleObject = "myCallbackHandleObject";
         client.addListener(callbackHandlercallbackLocatorcallbackHandleObject);
 
         client.disconnect();
         callbackConnector.stop();
         connector.stop();
      }
      catch (Throwable t)
      {
         .error(t);
         t.printStackTrace();
         fail();
      }
   }
   
   
   
This test illustrates the following set of configuration options:

sidefactoryoption
server sideserver socket9
server sidesocket 9
client sideserver socket7
client sidesocket 6
   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Set SSL system properties.
         System.setProperty(."JKS");
         String keyStoreFilePath = getKeystoreFilePath();
         System.setProperty(.keyStoreFilePath);
         System.setProperty(."unit-tests-server");
         System.setProperty(."JKS");
         String trustStoreFilePath = getTruststoreFilePath();
         System.setProperty(.trustStoreFilePath);
         System.setProperty(."unit-tests-client");
         
         // Start Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         connector.create();
         connector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         connector.start();
         
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         
         // Make Client use remote invoker.
         cconfig.put(."true");
     
         // Create Client.
         Client client = new Client(locatorcconfig);
         client.connect();
         ..println(getName() + ": " + client.invoke("test invoke()"));
         
         
         //////////////////////////////////////////////
         /////       Set up callback handling.     //// 
         //////////////////////////////////////////////
         // Start callback Connector.
         HashMap cbconfig = new HashMap();
         
         // Make callback Connector server socket run in client mode.
         cbconfig.put(."true");
         
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new FactoryConfigSample.SampleInvocationHandler());
         callbackConnector.start();
         
         // Add callback handler.
         CallbackHandler callbackHandler = new CallbackHandler();
         String callbackHandleObject = "myCallbackHandleObject";
         client.addListener(callbackHandlercallbackLocatorcallbackHandleObject);
         client.disconnect();
         callbackConnector.stop();
         connector.stop();
      }
      catch (Throwable t)
      {
         .error(t);
         t.printStackTrace();
         fail();
      }
   }
   
   
   protected String getHostName()
   {
      return "localhost";
   }
   
   
   protected String getTransport()
   {
      return "sslsocket";
   }
   
   
   protected String getKeystoreFilePath()
   {
      File file = new File(FactoryConfigSSLSample.class.getResource("keystore").getFile());
      return file.getPath();
   }
   
   
   protected static String getTruststoreFilePath()
   {
      File file = new File(FactoryConfigSSLSample.class.getResource("truststore").getFile());
      return file.getPath();
   }
   
   
   {
      HashMap config = new HashMap();
      config.put(."JKS");
      String keyStoreFilePath = getKeystoreFilePath();
      config.put(.keyStoreFilePath);
      config.put(."unit-tests-server");
      config.put(."SSL");
      SSLSocketBuilder builder = new SSLSocketBuilder(config);
      builder.setUseSSLServerSocketFactory(false);
      service.setSSLSocketBuilder(builder);
      service.start();
      return service;
   }
   
   {
      HashMap config = new HashMap();
      String trustStoreFilePath = getTruststoreFilePath();
      config.put(.trustStoreFilePath);
      config.put(."unit-tests-client");
      config.put(."SSL");
      SSLSocketBuilder builder = new SSLSocketBuilder(config);
      builder.setUseSSLSocketFactory(false);
      SSLSocketFactoryService service = new SSLSocketFactoryService();
      service.setSSLSocketBuilder(builder);
      service.start();
      return service;
   }
   
   {
      HashMap config = new HashMap();
      String trustStoreFilePath = getTruststoreFilePath();
      config.put(.trustStoreFilePath);
      config.put(."unit-tests-client");
      config.put(."SSL");
      SSLSocketBuilder builder = new SSLSocketBuilder(config);
      builder.setUseSSLServerSocketFactory(false);
      service.setSSLSocketBuilder(builder);
      service.start();
      return service;
   }
   
   {
      HashMap config = new HashMap();
      config.put(."JKS");
      String keyStoreFilePath = getKeystoreFilePath();
      config.put(.keyStoreFilePath);
      config.put(."unit-tests-server");
      config.put(."SSL");
      SSLSocketBuilder builder = new SSLSocketBuilder(config);
      builder.setUseSSLSocketFactory(false);
      SSLSocketFactoryService service = new SSLSocketFactoryService();
      service.setSSLSocketBuilder(builder);
      service.start();
      return builder.createSSLSocketFactory();
   }
New to GrepCode? Check out our FAQ X