classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Providing workarounds for com.sun.CORBA.connection.ORB


From: Meskauskas Audrius
Subject: [cp-patches] FYI: Providing workarounds for com.sun.CORBA.connection.ORBSocketFactoryClass property (or ORBConstants.SOCKET_FACTORY_CLASS_PROPERTY field).
Date: Fri, 21 Oct 2005 13:32:25 +0200
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

This property sets a custom socket factory class and is most frequently used to implement communications via SSL. Direct replacement is not possible because that class must implement the non-trivial undocumented interface from the com.sun.corba domain (subject of changes without notice anyway).

The new property gnu.Corba.SocketFactory replaces the socket factory for the ORB being currently instantiated and should help with adaptation. The factory must implement gnu.CORBA.interfaces.gnuSocketFactory.

2005-10-21  Audrius Meskauskas  <address@hidden>

* gnu/CORBA/DefaultSocketFactory.java,
gnu/CORBA/interfaces/gnuSocketFactory.java,
gnu/CORBA/interfaces/package.html: New files.
* gnu/CORBA/Functional_ORB.java,
gnu/CORBA/Focused_ORB.java (getPortServer),
gnu/CORBA/GIOP/ErrorMessage.java (send),
gnu/CORBA/gnuRequest.java (submit): Rewritten to support
gnu.Corba.SocketFactory property.
* org/omg/CORBA/ORB.java: Documentation update.
? gnu/CORBA/DefaultSocketFactory.java
? gnu/CORBA/interfaces
Index: gnu/CORBA/Focused_ORB.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/CORBA/Focused_ORB.java,v
retrieving revision 1.1
diff -u -r1.1 Focused_ORB.java
--- gnu/CORBA/Focused_ORB.java  16 Oct 2005 22:39:05 -0000      1.1
+++ gnu/CORBA/Focused_ORB.java  21 Oct 2005 09:28:50 -0000
@@ -192,7 +192,7 @@
         try
           {
             // Check if the port is ok:
-            ServerSocket s = new ServerSocket(port);
+            ServerSocket s = socketFactory.createServerSocket(port);
             s.close();
 
             portServer shared;
Index: gnu/CORBA/Functional_ORB.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/CORBA/Functional_ORB.java,v
retrieving revision 1.21
diff -u -r1.21 Functional_ORB.java
--- gnu/CORBA/Functional_ORB.java       16 Oct 2005 22:39:05 -0000      1.21
+++ gnu/CORBA/Functional_ORB.java       21 Oct 2005 10:04:54 -0000
@@ -49,6 +49,7 @@
 import gnu.CORBA.NamingService.NameParser;
 import gnu.CORBA.NamingService.NamingServiceTransient;
 import gnu.CORBA.Poa.gnuForwardRequest;
+import gnu.CORBA.interfaces.gnuSocketFactory;
 
 import org.omg.CORBA.BAD_OPERATION;
 import org.omg.CORBA.BAD_PARAM;
@@ -135,7 +136,7 @@
       setDaemon(true);
       try
         {
-          service = new ServerSocket(s_port);
+          service = socketFactory.createServerSocket(s_port);
         }
       catch (IOException ex)
         {
@@ -445,6 +446,11 @@
    * exceeding this limit, the NO_RESOURCES is thrown back to the client.
    */
   private int MAX_RUNNING_THREADS = 256;
+  
+  /**
+   * The producer of the client and server sockets for this ORB.
+   */
+  public gnuSocketFactory socketFactory = DefaultSocketFactory.Singleton;
 
   /**
    * Create the instance of the Functional ORB.
@@ -507,7 +513,7 @@
           {
             Integer free = (Integer) freed_ports.getLast();
             freed_ports.removeLast();
-            s = new ServerSocket(free.intValue());
+            s = socketFactory.createServerSocket(free.intValue());
             s.close();
             return free.intValue();
           }
@@ -523,7 +529,7 @@
       {
         try
           {
-            s = new ServerSocket(a_port);
+            s = socketFactory.createServerSocket(a_port);
             s.close();
             Port = a_port + 1;
             return a_port;
@@ -537,37 +543,28 @@
     Random rand = new Random();
     // Try any random port in the interval RANDOM_PORT_FROM.RANDOM_PORT_TO.
     int range = RANDOM_PORT_TO - RANDOM_PORT_FROM;
+    IOException ioex = null;
     for (int i = 0; i < RANDOM_PORT_ATTEMPTS; i++)
       {
         try
           {
-            a_port = RANDOM_PORT_FROM
-              + rand.nextInt(range);
-            s = new ServerSocket(a_port);
+            a_port = RANDOM_PORT_FROM + rand.nextInt(range);
+            s = socketFactory.createServerSocket(a_port);
             s.close();
             return a_port;
           }
         catch (IOException ex)
           {
             // Repeat the loop if this exception has been thrown.
+            ioex = ex;
           }
       }
 
-    try
-      {
-        // Try the parameterless constructor.
-        s = new ServerSocket();
-        a_port = s.getLocalPort();
-        s.close();
-        return a_port;
-      }
-    catch (IOException ex)
-      {
-        NO_RESOURCES bad = new NO_RESOURCES("Unable to open the server 
socket.");
-        bad.minor = Minor.Ports;
-        bad.initCause(ex);
-        throw bad;
-      }
+    NO_RESOURCES bad = new NO_RESOURCES("Unable to open the server socket.");
+    bad.minor = Minor.Ports;
+    if (ioex != null)
+      bad.initCause(ioex);
+    throw bad;
   }
 
   /**
@@ -576,7 +573,7 @@
    * on this port first. It the port is busy, or if more objects are connected,
    * the subsequent object will receive a larger port values, skipping
    * unavailable ports, if required. The change applies globally.
-   *
+   * 
    * @param a_Port a port, on that the server is listening for requests.
    */
   public static void setPort(int a_Port)
@@ -1625,7 +1622,25 @@
               "'"
             );
           }
-
+        
+        if (props.containsKey(gnuSocketFactory.PROPERTY))
+          {
+            String factory = null;
+            try
+              {
+                factory = props.getProperty(gnuSocketFactory.PROPERTY);
+                if (factory!=null)
+                  socketFactory = (gnuSocketFactory) 
+                    ObjectCreator.forName(factory).newInstance();
+              }
+            catch (Exception ex)
+              {
+                BAD_PARAM p = new BAD_PARAM("Bad socket factory "+factory);
+                p.initCause(ex);
+                throw p;
+              }
+          }
+        
         Enumeration en = props.elements();
         while (en.hasMoreElements())
           {
Index: gnu/CORBA/gnuRequest.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/CORBA/gnuRequest.java,v
retrieving revision 1.13
diff -u -r1.13 gnuRequest.java
--- gnu/CORBA/gnuRequest.java   4 Oct 2005 17:58:15 -0000       1.13
+++ gnu/CORBA/gnuRequest.java   21 Oct 2005 09:33:38 -0000
@@ -722,7 +722,8 @@
    * 
    * @return the server response in binary form.
    */
-public synchronized binaryReply submit() throws ForwardRequest
+  public synchronized binaryReply submit()
+    throws ForwardRequest
   {
     gnu.CORBA.GIOP.MessageHeader header = new gnu.CORBA.GIOP.MessageHeader();
 
@@ -754,15 +755,14 @@
     // This also sets the stream encoding to the encoding, specified
     // in the header.
     rh.write(request_part);
-    
+
     if (m_args != null && m_args.count() > 0)
       {
         write_parameters(header, request_part);
 
         if (m_parameter_buffer != null)
-          throw new BAD_INV_ORDER("Please either add parameters or " +
-            "write them into stream, but not both " + "at once."
-          );
+          throw new BAD_INV_ORDER("Please either add parameters or "
+            + "write them into stream, but not both " + "at once.");
       }
 
     if (m_parameter_buffer != null)
@@ -790,12 +790,15 @@
           {
             // The BindException may be thrown under very heavy parallel
             // load. For some time, just wait, exceptiong the socket to free.
-            Open:
-            for (int i = 0; i < PAUSE_STEPS; i++)
+            Open: for (int i = 0; i < PAUSE_STEPS; i++)
               {
                 try
                   {
-                    socket = new Socket(ior.Internet.host, ior.Internet.port);
+                    if (orb instanceof Functional_ORB)
+                      socket = ((Functional_ORB) 
orb).socketFactory.createClientSocket(
+                        ior.Internet.host, ior.Internet.port);
+                    else
+                      socket = new Socket(ior.Internet.host, 
ior.Internet.port);
                     break Open;
                   }
                 catch (BindException ex)
@@ -817,9 +820,8 @@
           }
 
         if (socket == null)
-          throw new NO_RESOURCES(ior.Internet.host + ":" + ior.Internet.port +
-            " in use"
-          );
+          throw new NO_RESOURCES(ior.Internet.host + ":" + ior.Internet.port
+            + " in use");
         socket.setKeepAlive(true);
 
         OutputStream socketOutput = socket.getOutputStream();
@@ -836,17 +838,17 @@
             MessageHeader response_header = new MessageHeader();
             InputStream socketInput = socket.getInputStream();
             response_header.read(socketInput);
-            
-            byte [] r;
+
+            byte[] r;
             if (orb instanceof Functional_ORB)
               {
                 Functional_ORB fo = (Functional_ORB) orb;
-                r =response_header.readMessage(socketInput, socket, 
+                r = response_header.readMessage(socketInput, socket,
                   fo.TOUT_WHILE_READING, fo.TOUT_AFTER_RECEIVING);
               }
             else
               r = response_header.readMessage(socketInput, null, 0, 0);
-              
+
             return new binaryReply(orb, response_header, r);
           }
         else
@@ -854,11 +856,9 @@
       }
     catch (IOException io_ex)
       {
-        COMM_FAILURE m =
-          new COMM_FAILURE("Unable to open a socket at " + ior.Internet.host + 
":" +
-            ior.Internet.port, 0xC9,
-            CompletionStatus.COMPLETED_NO
-          );
+        COMM_FAILURE m = new COMM_FAILURE("Unable to open a socket at "
+          + ior.Internet.host + ":" + ior.Internet.port, 0xC9,
+          CompletionStatus.COMPLETED_NO);
         m.initCause(io_ex);
         throw m;
       }
Index: gnu/CORBA/GIOP/ErrorMessage.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/CORBA/GIOP/ErrorMessage.java,v
retrieving revision 1.4
diff -u -r1.4 ErrorMessage.java
--- gnu/CORBA/GIOP/ErrorMessage.java    4 Oct 2005 17:58:14 -0000       1.4
+++ gnu/CORBA/GIOP/ErrorMessage.java    21 Oct 2005 09:37:22 -0000
@@ -38,6 +38,7 @@
 
 package gnu.CORBA.GIOP;
 
+import gnu.CORBA.Functional_ORB;
 import gnu.CORBA.IOR;
 import gnu.CORBA.Minor;
 
@@ -47,6 +48,7 @@
 import java.net.Socket;
 
 import org.omg.CORBA.MARSHAL;
+import org.omg.CORBA.ORB;
 
 /**
  * The error message is sent in response to the message, encoded
@@ -60,6 +62,11 @@
 public class ErrorMessage
   extends MessageHeader
 {
+  /** 
+   * Use serialVersionUID for interoperability. 
+   */
+  private static final long serialVersionUID = 1;
+  
   /**
    * Create a new error message, setting the message field
    * to the address@hidden MESSAGE_ERROR} and the version number to
@@ -74,14 +81,22 @@
   /**
    * Send the error message to the given IOR address.
    *
-   * @param to the IOR address (host and port, other fields
+   * @param ior the IOR address (host and port, other fields
    * are not used).
+   * 
+   * @param orb the ORB, sending the error message.
    */
-  public void send(IOR ior)
+  public void send(IOR ior, ORB orb)
   {
     try
       {
-        Socket socket = new Socket(ior.Internet.host, ior.Internet.port);
+        Socket socket;
+        
+        if (orb instanceof Functional_ORB)
+          socket = ((Functional_ORB) orb).socketFactory.createClientSocket(
+            ior.Internet.host, ior.Internet.port);
+        else
+          socket = new Socket(ior.Internet.host, ior.Internet.port);
 
         OutputStream socketOutput = socket.getOutputStream();
         write(socketOutput);
Index: org/omg/CORBA/ORB.java
===================================================================
RCS file: /cvsroot/classpath/classpath/org/omg/CORBA/ORB.java,v
retrieving revision 1.19
diff -u -r1.19 ORB.java
--- org/omg/CORBA/ORB.java      16 Oct 2005 22:39:05 -0000      1.19
+++ org/omg/CORBA/ORB.java      21 Oct 2005 11:22:28 -0000
@@ -106,6 +106,16 @@
  * should not overlap with the values, specified for other ORB's.
  * </td>
  * </tr>
+ * <tr>
+ * <td>gnu.Corba.SocketFactory</td>
+ * <td>Sets the user-defined server and client socket factory for the ORB being
+ * currently instantiated. Serves as a replacement of the proprietary
+ * property com.sun.CORBA.connection.ORBSocketFactoryClass. To have multiple
+ * types of sockets, instantiate several ORB's with this property each time
+ * set to the different value. 
+ * The factory must implement gnu.CORBA.interfaces.gnuSocketFactory.
+ * </td>
+ * </tr>
  * </table> 
  * <p>The command line accepts the same properties as a keys. When
  * specifying in the command line, the prefix org.omg.CORBA can be omitted, for
/* DefaultSocketFactory.java --
 Copyright (C) 2005 Free Software Foundation, Inc.

 This file is part of GNU Classpath.

 GNU Classpath is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2, or (at your option)
 any later version.

 GNU Classpath 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
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with GNU Classpath; see the file COPYING.  If not, write to the
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 02110-1301 USA.

 Linking this library statically or dynamically with other modules is
 making a combined work based on this library.  Thus, the terms and
 conditions of the GNU General Public License cover the whole
 combination.

 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */


package gnu.CORBA;

import gnu.CORBA.interfaces.gnuSocketFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * The default socket factory that forges "plain" server and client sockets. The
 * class can be replaced by setting the gnu.CORBA.SocketFactory property.
 * 
 * @author Audrius Meskauskas, Lithuania (address@hidden)
 */
public class DefaultSocketFactory
  implements gnuSocketFactory
{
  /**
   * It is enough to have one instance of this class for all ORBs.
   */
  public static final DefaultSocketFactory Singleton = new 
DefaultSocketFactory();

  /**
   * Create a client socket.
   */
  public Socket createClientSocket(String host, int port)
    throws IOException
  {
    return new Socket(host, port);
  }

  /**
   * Create a server socket.
   */
  public ServerSocket createServerSocket(int port)
    throws IOException
  {
    return new ServerSocket(port);
  }

}

This package contains Classpath specific interfaces that the user program may be forced to use in cases when no other, better solution of the problem is available. The existing classes and methods in this package should not be removed without the real need

/* gnuSocketFactory.java --
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.CORBA.interfaces;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * This class produces sockets for serving and submitting CORBA requests. The
 * socket factory can be set using address@hidden gnuOrb.setSocketFactory()} for
 * producting all sockets for that ORB. This is needed for using secure sockets,
 * for implementing the desired timeout policies, for HTTP tunnels and in some
 * other similar cases. While such functionality is provided by near all
 * existing CORBA implementations, no standard mechanism is defined.
 * 
 * The socket factory may need to put additional information to the IORs of the
 * objects, released by the ORB. Because of this reason, this interface extends
 * IORInterceptorOperations.
 * 
 * @author Audrius Meskauskas, Lithuania (address@hidden)
 */
public interface gnuSocketFactory
{
  /**
   * The name of the ORB property that forces the ORB to use the socket
   * factory class, the name of that (String) is the value of this property.
   */
  final String PROPERTY = "gnu.CORBA.SocketFactory";
  
  /**
   * Create a server socket that should serve remote invocations on the given
   * port. The ORB may use this socket to serve either one or more objects.
   * 
   * @param port the port, on that the socket should be listening for requests.
   * The port policy can be controlled by address@hidden gnuPortManager}.
   * 
   * @throws IOException if the socket cannot be created on the given port due
   * any reasons. The ORB may try to open the socket on another port, calling
   * this method with the different parameter.
   */
  ServerSocket createServerSocket(int port)
    throws IOException;
  
  /**
   * Create a client socket that should send a request to the remote side. When
   * returned, the socket should be opened and ready to communicate.
   * 
   * @param port the port, on that the socket should be openend. The port is
   * usually part of the internet profile.
   * 
   * @throws IOException if the socket cannot be created on the given port due
   * any reasons. The ORB may try to open the socket on another port, calling
   * this method with the different parameter.
   */
  Socket createClientSocket(String host, int port)
    throws IOException;

}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]