jdk/src/windows/classes/java/net/DefaultDatagramSocketImplFactory.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10596 39b3a979e600
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.PrivilegedAction;
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
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static Class prefixImplClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /* the windows version. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static float version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /* java.net.preferIPv4Stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static boolean preferIPv4Stack = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* If the version supports a dual stack TCP implementation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static boolean useDualStackImpl = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        // Determine Windows Version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    version = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        version = Float.parseFloat(System.getProperties().getProperty("os.version"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        preferIPv4Stack = Boolean.parseBoolean(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                                          System.getProperties().getProperty("java.net.preferIPv4Stack"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                    } catch (NumberFormatException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                        assert false : e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    return null; // nothing to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                } });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // (version >= 6.0) implies Vista or greater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (version >= 6.0 && !preferIPv4Stack) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            useDualStackImpl = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // impl.prefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        String prefix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            prefix = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                new sun.security.action.GetPropertyAction("impl.prefix", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (prefix != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                prefixImplClass = Class.forName("java.net."+prefix+"DatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            System.err.println("Can't find class: java.net." +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                prefix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                "DatagramSocketImpl: check impl.prefix property");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Creates a new <code>DatagramSocketImpl</code> instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param   isMulticast true if this impl is to be used for a MutlicastSocket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (prefixImplClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                return (DatagramSocketImpl) prefixImplClass.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new SocketException("can't instantiate DatagramSocketImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (useDualStackImpl && !isMulticast)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                return new DualStackPlainDatagramSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                return new TwoStacksPlainDatagramSocketImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}