Hi,

I'm trying to port the network code to the JamaicaVM. It appears that
the PlainSocketImpl (or its subclass SocksSocketImpl) that is used by
default in java.net.Socket is not really feasible to use as SocketImpl,
and I would rather be able to choose different implementations for some
platforms. However, the choice of SocksSocketImpl is hardwired in the
Socket class. I propose to do something similar as in DatagramSocket and
InetAddress, and choose the implementation based on the property
impl.prefix, using a factory class.

The exact name of the property might be different though, otherwise we
might introduce a small incompatibility, when somebody uses the
impl.prefix property (or is it used internally? that would be even
worse) then he would now also have to provide a SocketImpl for that
prefix. Maybe we should choose 'socketimpl.prefix' then?

What do you think, is such a change feasible? It would certainly make my
life a lot easier.

Cheers, Roman

-- 
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com   * Tel: +49-721-663 968-0
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt
diff -r 1bfbe2752e32 src/share/classes/java/net/DefaultSocketImplFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/share/classes/java/net/DefaultSocketImplFactory.java	Mon Mar 10 20:17:24 2008 +0100
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package java.net;
+
+import java.security.AccessController;
+
+/**
+ * A factory for choosing the default SocketImpl in Socket.
+ */
+class DefaultSocketImplFactory
+{
+
+  /**
+   * The cached implementation class.
+   */
+  static Class prefixImplClass = null;
+
+  /**
+   * Fetch the impl.prefix property and try to load the corresponding
+   * impl class.
+   */
+  static {
+      String prefix = null;
+      try {
+          prefix = AccessController.doPrivileged(
+              new sun.security.action.GetPropertyAction("impl.prefix", null));
+          if (prefix != null)
+              prefixImplClass = Class.forName("java.net."+prefix+"SocketImpl");
+      } catch (Exception e) {
+          System.err.println("Can't find class: java.net." +
+                              prefix +
+                              "SocketImpl: check impl.prefix property");
+          //prefixImplClass = null;
+      }
+  }
+
+  /**
+   * Creates a new <code>SocketImpl</code> instance.
+   *
+   * @return  a new instance of a <code>SocketImpl</code>.
+   */
+  static SocketImpl createSocketImpl() {
+      if (prefixImplClass != null) {
+          try {
+              return (SocketImpl) prefixImplClass.newInstance();
+          } catch (Exception e) {
+              throw new InternalError("can't instantiate SocketImpl");
+          }
+      } else {
+          return new SocksSocketImpl();
+      }
+  }
+}
diff -r 1bfbe2752e32 src/share/classes/java/net/Socket.java
--- a/src/share/classes/java/net/Socket.java	Thu Feb 28 10:14:08 2008 +0100
+++ b/src/share/classes/java/net/Socket.java	Mon Mar 10 20:17:24 2008 +0100
@@ -478,9 +478,10 @@ class Socket implements java.io.Closeabl
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = new SocksSocketImpl();
+            impl = DefaultSocketImplFactory.createSocketImpl();
+            if (! (impl instanceof SocksSocketImpl)) {
+              checkOldImpl();
+            }
         }
         if (impl != null)
             impl.setSocket(this);

Reply via email to