< prev index next >

src/java.base/windows/classes/java/net/DefaultDatagramSocketImplFactory.java

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.net;
  26 
  27 import java.security.AccessController;
  28 import java.security.PrivilegedAction;
  29 import sun.security.action.GetPropertyAction;
  30 
  31 /**
  32  * This class defines a factory for creating DatagramSocketImpls. It defaults
  33  * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls
  34  * by setting the impl.prefix system property.
  35  *
  36  * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl
  37  * is always created. This impl supports IPv6 on these platform where available.
  38  *
  39  * On Windows platforms greater than Vista that support a dual layer TCP/IP stack
  40  * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets
  41  * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack
  42  * of behavior defined for multicasting over a dual layer socket by the RFC.
  43  *
  44  * @author Chris Hegarty
  45  */
  46 
  47 class DefaultDatagramSocketImplFactory
  48 {
  49     private static final Class<?> prefixImplClass;
  50 
  51     /* java.net.preferIPv4Stack */
  52     private static final boolean preferIPv4Stack;
  53 
  54     /* True if exclusive binding is on for Windows */
  55     private static final boolean exclusiveBind;
  56 
  57     static {
  58         Class<?> prefixImplClassLocal = null;
  59 

  60         preferIPv4Stack = Boolean.parseBoolean(
  61                 AccessController.doPrivileged(
  62                         new GetPropertyAction("java.net.preferIPv4Stack")));
  63 
  64         String exclBindProp = AccessController.doPrivileged(
  65                 new GetPropertyAction("sun.net.useExclusiveBind", ""));
  66         exclusiveBind = (exclBindProp.isEmpty())
  67                 ? true
  68                 : Boolean.parseBoolean(exclBindProp);
  69 
  70         // impl.prefix
  71         String prefix = null;
  72         try {
  73             prefix = AccessController.doPrivileged(
  74                 new GetPropertyAction("impl.prefix", null));
  75             if (prefix != null)
  76                 prefixImplClassLocal = Class.forName("java.net."+prefix+"DatagramSocketImpl");
  77         } catch (Exception e) {
  78             System.err.println("Can't find class: java.net." +
  79                                 prefix +
  80                                 "DatagramSocketImpl: check impl.prefix property");
  81         }
  82 
  83         prefixImplClass = prefixImplClassLocal;
  84     }
  85 
  86     /**
  87      * Creates a new <code>DatagramSocketImpl</code> instance.
  88      *
  89      * @param   isMulticast true if this impl is to be used for a MutlicastSocket
  90      * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
  91      */
  92     static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
  93         throws SocketException {
  94         if (prefixImplClass != null) {


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.net;
  26 
  27 import java.util.Properties;

  28 import sun.security.action.GetPropertyAction;
  29 
  30 /**
  31  * This class defines a factory for creating DatagramSocketImpls. It defaults
  32  * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls
  33  * by setting the impl.prefix system property.
  34  *
  35  * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl
  36  * is always created. This impl supports IPv6 on these platform where available.
  37  *
  38  * On Windows platforms greater than Vista that support a dual layer TCP/IP stack
  39  * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets
  40  * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack
  41  * of behavior defined for multicasting over a dual layer socket by the RFC.
  42  *
  43  * @author Chris Hegarty
  44  */
  45 
  46 class DefaultDatagramSocketImplFactory
  47 {
  48     private static final Class<?> prefixImplClass;
  49 
  50     /* java.net.preferIPv4Stack */
  51     private static final boolean preferIPv4Stack;
  52 
  53     /* True if exclusive binding is on for Windows */
  54     private static final boolean exclusiveBind;
  55 
  56     static {
  57         Class<?> prefixImplClassLocal = null;
  58 
  59         Properties props = GetPropertyAction.getProperties();
  60         preferIPv4Stack = Boolean.parseBoolean(
  61                 props.getProperty("java.net.preferIPv4Stack"));

  62 
  63         String exclBindProp = props.getProperty("sun.net.useExclusiveBind", "");

  64         exclusiveBind = (exclBindProp.isEmpty())
  65                 ? true
  66                 : Boolean.parseBoolean(exclBindProp);
  67 
  68         // impl.prefix
  69         String prefix = null;
  70         try {
  71             prefix = props.getProperty("impl.prefix");

  72             if (prefix != null)
  73                 prefixImplClassLocal = Class.forName("java.net."+prefix+"DatagramSocketImpl");
  74         } catch (Exception e) {
  75             System.err.println("Can't find class: java.net." +
  76                                 prefix +
  77                                 "DatagramSocketImpl: check impl.prefix property");
  78         }
  79 
  80         prefixImplClass = prefixImplClassLocal;
  81     }
  82 
  83     /**
  84      * Creates a new <code>DatagramSocketImpl</code> instance.
  85      *
  86      * @param   isMulticast true if this impl is to be used for a MutlicastSocket
  87      * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
  88      */
  89     static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
  90         throws SocketException {
  91         if (prefixImplClass != null) {
< prev index next >