jdk/src/java.base/windows/classes/java/net/DefaultDatagramSocketImplFactory.java
author redestad
Thu, 21 Apr 2016 13:39:53 +0200
changeset 37593 824750ada3d6
parent 37354 2cc8c7bd70ee
child 37781 71ed5645f17c
permissions -rw-r--r--
8154231: Simplify access to System properties from JDK code Reviewed-by: rriggs, chegar, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37354
diff changeset
    27
import java.util.Properties;
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    28
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class defines a factory for creating DatagramSocketImpls. It defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * to creating plain DatagramSocketImpls, but may create other DatagramSocketImpls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * by setting the impl.prefix system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * For Windows versions lower than Windows Vista a TwoStacksPlainDatagramSocketImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * is always created. This impl supports IPv6 on these platform where available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * On Windows platforms greater than Vista that support a dual layer TCP/IP stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * a DualStackPlainDatagramSocketImpl is created for DatagramSockets. For MulticastSockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a TwoStacksPlainDatagramSocketImpl is always created. This is to overcome the lack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * of behavior defined for multicasting over a dual layer socket by the RFC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Chris Hegarty
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class DefaultDatagramSocketImplFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32503
diff changeset
    48
    private static final Class<?> prefixImplClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* java.net.preferIPv4Stack */
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    51
    private static final boolean preferIPv4Stack;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    52
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    53
    /* True if exclusive binding is on for Windows */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32503
diff changeset
    54
    private static final boolean exclusiveBind;
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    static {
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
    57
        Class<?> prefixImplClassLocal = null;
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    58
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37354
diff changeset
    59
        Properties props = GetPropertyAction.getProperties();
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    60
        preferIPv4Stack = Boolean.parseBoolean(
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37354
diff changeset
    61
                props.getProperty("java.net.preferIPv4Stack"));
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
    62
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37354
diff changeset
    63
        String exclBindProp = props.getProperty("sun.net.useExclusiveBind", "");
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    64
        exclusiveBind = (exclBindProp.isEmpty())
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    65
                ? true
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    66
                : Boolean.parseBoolean(exclBindProp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        // impl.prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        String prefix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        try {
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 37354
diff changeset
    71
            prefix = props.getProperty("impl.prefix");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            if (prefix != null)
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
    73
                prefixImplClassLocal = Class.forName("java.net."+prefix+"DatagramSocketImpl");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            System.err.println("Can't find class: java.net." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                                "DatagramSocketImpl: check impl.prefix property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
    79
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
    80
        prefixImplClass = prefixImplClassLocal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a new <code>DatagramSocketImpl</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param   isMulticast true if this impl is to be used for a MutlicastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (prefixImplClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                return (DatagramSocketImpl) prefixImplClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                throw new SocketException("can't instantiate DatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
37354
2cc8c7bd70ee 8154185: Drop code to support Windows XP in DefaultDatagramSocketImplFactory
redestad
parents: 32649
diff changeset
    98
            if (!preferIPv4Stack && !isMulticast)
18192
fa6bd0992104 7170730: Improve Windows network stack support.
khazra
parents: 10596
diff changeset
    99
                return new DualStackPlainDatagramSocketImpl(exclusiveBind);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            else
32503
1478c9130534 8072466: Deadlock when initializing MulticastSocket and DatagramSocket
igerasim
parents: 25859
diff changeset
   101
                return new TwoStacksPlainDatagramSocketImpl(exclusiveBind && !isMulticast);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}