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  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 applicable to any kind of socket and server socket, so the SampleServerSocketFactory and SampleSocketFactory classes create ordinary sockets and server sockets.

Author(s):
Ron Sigal

Copyright (c) Jul 20, 2006

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

sidefactoryoption
server sideserver socket1
server sidesocket 1
client sideserver socket1
client sidesocket 1
 
    public void testFactoriesBySettingInvokers()
    {
       try
       {
          /////////////////////////////////////
          /////    Set up server side.     //// 
          /////////////////////////////////////
          HashMap sconfig = new HashMap();
          
          // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         connector.create();
         
         // Set ServerSocketFactory and SocketFactory in ServerInvoker.
         ServerInvoker serverInvoker = connector.getServerInvoker();
         serverInvoker.setServerSocketFactory(ssf1);
         serverInvoker.setSocketFactory(sf1);
         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);
         client.connect();
         
         // Set SocketFactory in ClientInvoker.
         SocketFactory sf2 = getDefaultSocketFactory();
         ClientInvoker clientInvoker = client.getInvoker();
         clientInvoker.setSocketFactory(sf2);
         ..println(getName() + ": " + client.invoke("test invoke()"));
         
         
         //////////////////////////////////////////////
         /////       Set up callback handling.     //// 
         //////////////////////////////////////////////
         // Start callback Connector.
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
         callbackConnector.create();
         ServerInvoker callbackServerInvoker = callbackConnector.getServerInvoker();
         callbackServerInvoker.setServerSocketFactory(ssf2);
         callbackConnector.addInvocationHandler("sample"new 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 socket2
server sidesocket 2
client sideserver socket2
client sidesocket 2
   {
      try
      {
         /////////////////////////////////////
         /////     Set up server side.    //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         
         // Set ServerSocketFactory and SocketFactory in Connector.
         connector.setServerSocketFactory(ssf1);
         connector.setSocketFactory(sf1);
         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.
         SocketFactory sf2 = getDefaultSocketFactory();
         client.setSocketFactory(sf2);
         client.connect();
         ..println(getName() + ": " + client.invoke("test invoke()"));
         
         
         //////////////////////////////////////////////
         /////      Set up callback handling.      //// 
         //////////////////////////////////////////////
         // Get callback Connector.
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI());
        
         // Set ServerSocketFactory in callback Connector
         callbackConnector.setServerSocketFactory(ssf2);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new 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 socket3
server sidesocket 3
client sideserver socket3
client sidesocket 3
   public void testFactoriesByPassingInConfig()
   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         
         // Put ServerSocketFactory and SocketFactory in config map.
         sconfig.put(.ssf1);
         sconfig.put(.sf1);
         
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         connector.create();
         connector.addInvocationHandler("sample"new SampleInvocationHandler());
         connector.start();
         
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         
         // Put SocketFactory in config map.
         SocketFactory sf2 = getDefaultSocketFactory();
         cconfig.put(.sf2);
         
         // 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.     //// 
         //////////////////////////////////////////////
         
         // Get callback Connector.
         HashMap cbconfig = new HashMap();
         cbconfig.put(.ssf2);
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new 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 socket5
server sidesocket 6
client sideserver socket4
client sidesocket 1
   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         
         // Make callback Client use remote invoker.
         sconfig.put(."true");
           
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(sconfig);
         
         // 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(          getDefaultServerSocketFactoryClass().getName());
         buf.append(      "</attribute>");
         buf.append(      "<attribute name=\"socketFactory\">" );
         buf.append(         getDefaultSocketFactoryClass().getName());
         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.     //// 
         //////////////////////////////////////////////
         
         // Get callback Connector.
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector();
         
         // Set xml configuration element.
         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(         getDefaultCallbackServerSocketFactoryClass().getName());
         buf.append(      "</attribute>");
         buf.append(   "</invoker>");
         buf.append("</config>");
         bais = new ByteArrayInputStream(buf.toString().getBytes());
         xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(bais);
         callbackConnector.setConfiguration(xml.getDocumentElement());
         
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new 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 socket7
server sidesocket 7
client sideserver socket5
client sidesocket 4
   {
      try
      {
         /////////////////////////////////////
         /////    Set up server side.     //// 
         /////////////////////////////////////
         HashMap sconfig = new HashMap();
         
         // Put class names of ServerSocketFactory and SocketFactory in config map.
         
         // Make callback Client use remote invoker.
         sconfig.put(."true");
         
         // Get Connector.
         int freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator locator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector connector = new Connector(locatorsconfig);
         connector.create();
         connector.addInvocationHandler("sample"new SampleInvocationHandler());
         connector.start();
         
         
         /////////////////////////////////////
         /////    Set up client side.     //// 
         /////////////////////////////////////
         HashMap cconfig = new HashMap();
         
         // Put SocketFactory class name in config map.
         
         // 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.     //// 
         //////////////////////////////////////////////
         
         // Get callback Connector.
         HashMap cbconfig = new HashMap();
         freeport = PortUtil.findFreePort(getHostName());
         InvokerLocator callbackLocator = new InvokerLocator(getTransport() + "://" + getHostName() + ":" + freeport);
         Connector callbackConnector = new Connector(callbackLocator.getLocatorURI(), cbconfig);
         callbackConnector.create();
         callbackConnector.addInvocationHandler("sample"new 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 getTransport()
   {
      return "socket";
   }
   
   
   protected String getHostName()
   {
      return "localhost";
   }
   
   {
      return new SampleServerSocketFactory();
   }
   
   
   {
      return new SampleSocketFactory();
   }
   
   
   {
      return new SampleServerSocketFactory();
   }
   
   
   {
      return new SampleSocketFactory();
   }
   
   
   {
      return SampleServerSocketFactory.class;
   }
   
   
   protected Class getDefaultSocketFactoryClass() throws Exception
   {
      return SampleSocketFactory.class;
   }
   
   
   {
      return SampleServerSocketFactory.class;
   }
   
   
   {
      return SampleSocketFactory.class;
   }
   
   
   public static class SampleServerSocketFactory
   extends ServerSocketFactory
   {
      public ServerSocket createServerSocket(int arg0throws IOException
      {
         return new ServerSocket(arg0);
      }
      public ServerSocket createServerSocket(int arg0int arg1throws IOException
      {
         return new ServerSocket(arg0arg1);
      }
      public ServerSocket createServerSocket(int arg0int arg1InetAddress arg2throws IOException
      {
         return new ServerSocket(arg0arg1arg2);
      }
   }
   
   
   public static class SampleSocketFactory
   extends SocketFactory
   {
      public Socket createSocket(String arg0int arg1throws IOExceptionUnknownHostException
      {
         return new Socket(arg0arg1);
      }
      public Socket createSocket(String arg0int arg1InetAddress arg2int arg3throws IOExceptionUnknownHostException
      {
         return new Socket(arg0arg1arg2arg3);
      }
      public Socket createSocket(InetAddress arg0int arg1throws IOException
      {
         return new Socket(arg0arg1);
      }
      public Socket createSocket(InetAddress arg0int arg1InetAddress arg2int arg3throws IOException
      {
         return new Socket(arg0arg1arg2arg3);
      }
   }
   
   
   public static class SampleInvocationHandler implements ServerInvocationHandler
   {
      
      public SampleInvocationHandler()
      {   
      }
      
      public Object invoke(InvocationRequest invocationthrows Throwable
      {
         return invocation.getParameter();
      }
      
      public void addListener(InvokerCallbackHandler callbackHandler)
      {
         .info("entering addListener()");
         this. = callbackHandler;
         
         try
         {
            Callback callback = new Callback(new Integer(1));
            callbackHandler.handleCallback(callback);
            .info("sent first callback");
         }
         catch(Exception e)
         {
            e.printStackTrace();
         }
      }
      
      public void removeListener(InvokerCallbackHandler callbackHandler)
      {
      }
      
      public void setMBeanServer(MBeanServer server)
      {
         // NO OP as do not need reference to MBeanServer for this handler
      }
      
      public void setInvoker(ServerInvoker invoker)
      {
         // NO OP as do not need reference back to the server invoker
      }
      
      {
         return ;
      }
   }
   
   
   public static class CallbackHandler implements InvokerCallbackHandler
   {
      
Will take the callback and print out its values.

Parameters:
callback
Throws:
org.jboss.remoting.callback.HandleCallbackException
      public void handleCallback(Callback callbackthrows HandleCallbackException
      {
         ..println("Received callback value of: " + callback.getCallbackObject());
      }
   }
New to GrepCode? Check out our FAQ X