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.coyote.ssl;
 
 import  org.apache.tomcat.util.net.ServerSocketFactory;
 import  org.jboss.logging.Logger;
 
 import java.util.Map;

Author(s):
Tom Elrod
 
 public class RemotingServerSocketFactory extends ServerSocketFactory
 {
    private Map mbeanServerMap = null;
 
    private static Map serverSocketFactories = new HashMap();
 
    protected final Logger log = Logger.getLogger(getClass());
 
    private boolean performHandshake = false;
 
    {
 
    }
 
    public RemotingServerSocketFactory(Map mbeanServerMap)
    {
       //TODO: -TME - THIS IS A TOTAL HACK!!!
       // Am having to do this because need to get reference to
       // MBeanServer from original CoyoteInvoker so can lookup
       // the real ServerSocketFactory to use (which will be registered
       // within the mbean server).
       this. = mbeanServerMap;
    }
 
    private void init() throws InstantiationException
    {
       //TODO: -TME - check for nulls and fallback defaults (or throw exception)
       String locator = (String) attributes.get("locator");
 
       // first look to see if there is socket factory to use already set
       if( == null)
       {
 
 
       MBeanServer mbeanserver = (MBeanServer.get(locator);
       String serverSocketObjName = (String) attributes.get("serverSocketFactory");
 
       if(locator == null || mbeanserver == null || serverSocketObjName == null)
       {
          throw new InstantiationException("Can not create ServerSocketFactory with SSL support due " +
                                           "to one of the following being null." +
                                           "\nlocator = " + locator + "\nmbeanserver = " + mbeanserver +
                                           "\nserverSocketObjName = " + serverSocketObjName);
       }
 
       try
       {
          ServerSocketFactoryMBean serverSocketFactoryMBean = (ServerSocketFactoryMBean)
                MBeanServerInvocationHandler.newProxyInstance(mbeanserver,
                                                             new ObjectName(serverSocketObjName),
                                                             ServerSocketFactoryMBean.class,
                                                             false);
          = new ServerSocketFactoryWrapper(serverSocketFactoryMBean);
          = true;
      }
      catch(Exception e)
      {
         InstantiationException iex = new InstantiationException("Error creating ServerSocketFactory proxy via MBeanServer");
         iex.setStackTrace(e.getStackTrace());
         throw iex;
      }
      }
   }

   
Returns a server socket which uses all network interfaces on the host, and is bound to a the specified port. The socket is configured with the socket options (such as accept timeout) given to this factory.

Parameters:
port the port to listen to
Throws:
java.io.IOException for networking errors
InstantiationException for construction errors
   public ServerSocket createSocket(int portthrows IOExceptionInstantiationException
   {
      if( == null)
      {
         init();
      }
      return .createServerSocket(port);
   }

   
Returns a server socket which uses all network interfaces on the host, is bound to a the specified port, and uses the specified connection backlog. The socket is configured with the socket options (such as accept timeout) given to this factory.

Parameters:
port the port to listen to
backlog how many connections are queued
Throws:
java.io.IOException for networking errors
InstantiationException for construction errors
   public ServerSocket createSocket(int portint backlogthrows IOExceptionInstantiationException
   {
      if( == null)
      {
         init();
      }
      return .createServerSocket(portbacklog);
   }

   
Returns a server socket which uses only the specified network interface on the local host, is bound to a the specified port, and uses the specified connection backlog. The socket is configured with the socket options (such as accept timeout) given to this factory.

Parameters:
port the port to listen to
backlog how many connections are queued
ifAddress the network interface address to use
Throws:
java.io.IOException for networking errors
InstantiationException for construction errors
   public ServerSocket createSocket(int portint backlogInetAddress ifAddressthrows IOExceptionInstantiationException
   {
      if( == null)
      {
         init();
      }
      ServerSocket svrSocket = .createServerSocket(portbacklogifAddress);
      return svrSocket;
   }

   
Wrapper function for accept(). This allows us to trap and translate exceptions if necessary

Throws:
java.io.IOException;
   public Socket acceptSocket(ServerSocket serverSocketthrows IOException
   {
      return accept(serverSocket);
   }

   
Extra function to initiate the handshake. Sometimes necessary for SSL

Throws:
java.io.IOException;
   public void handshake(Socket sockthrows IOException
   {
      if()
      {
         ((SSLSocketsock).startHandshake();
      }
   }
   public static void setServerSocketFactory(String locatorURIjavax.net.ServerSocketFactory svrSocketFactory)
   {
      .put(locatorURIsvrSocketFactory);
   }
   
   static private Socket accept(final ServerSocket ssthrows IOException
   {
      if (SecurityUtility.skipAccessControl())
      {
         return ss.accept();
      }
      
      try
      {
          return (Socket)AccessController.doPrivilegednew PrivilegedExceptionAction()
          {
             public Object run() throws Exception
             {
                 return ss.accept();
             }
          });
      }
      catch (PrivilegedActionException e)
      {
          throw (IOExceptione.getCause();
      }
   }
New to GrepCode? Check out our FAQ X