~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/openjdk/java/net/DefaultDatagramSocketImplFactory.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
 
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
4
 *
 
5
 * This code is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 2 only, as
 
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
 
 
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
    static Class prefixImplClass = null;
 
49
 
 
50
    /* the windows version. */
 
51
    private static float version;
 
52
 
 
53
    /* java.net.preferIPv4Stack */
 
54
    private static boolean preferIPv4Stack = false;
 
55
 
 
56
    /* If the version supports a dual stack TCP implementation */
 
57
    private static boolean useDualStackImpl = false;
 
58
 
 
59
    static {
 
60
        // Determine Windows Version.
 
61
        java.security.AccessController.doPrivileged( new PrivilegedAction<Object>() {
 
62
                public Object run() {
 
63
                    version = 0;
 
64
                    try {
 
65
                        version = Float.parseFloat(System.getProperties().getProperty("os.version"));
 
66
                        preferIPv4Stack = Boolean.parseBoolean(
 
67
                                          System.getProperties().getProperty("java.net.preferIPv4Stack"));
 
68
                    } catch (NumberFormatException e ) {
 
69
                        assert false : e;
 
70
                    }
 
71
                    return null; // nothing to return
 
72
                } });
 
73
 
 
74
        String ipv6 = ikvm.internal.Util.SafeGetEnvironmentVariable("IKVM_IPV6");
 
75
        if (ipv6 != null) {
 
76
            try {
 
77
                if ((Integer.parseInt(ipv6) & 4) == 0) {
 
78
                    preferIPv4Stack = true;
 
79
                } else {
 
80
                    useDualStackImpl = true;
 
81
                }
 
82
            } catch (NumberFormatException _) {
 
83
            }
 
84
        } else if (!InetAddressImplFactory.isIPv6Supported()) {
 
85
            preferIPv4Stack = true;
 
86
        }
 
87
 
 
88
        // (version >= 6.0) implies Vista or greater.
 
89
        if (version >= 6.0 && !preferIPv4Stack) {
 
90
            useDualStackImpl = true;
 
91
        }
 
92
 
 
93
        // impl.prefix
 
94
        String prefix = null;
 
95
        try {
 
96
            prefix = AccessController.doPrivileged(
 
97
                new sun.security.action.GetPropertyAction("impl.prefix", null));
 
98
            if (prefix != null)
 
99
                prefixImplClass = Class.forName("java.net."+prefix+"DatagramSocketImpl");
 
100
        } catch (Exception e) {
 
101
            System.err.println("Can't find class: java.net." +
 
102
                                prefix +
 
103
                                "DatagramSocketImpl: check impl.prefix property");
 
104
        }
 
105
    }
 
106
 
 
107
    /**
 
108
     * Creates a new <code>DatagramSocketImpl</code> instance.
 
109
     *
 
110
     * @param   isMulticast true if this impl is to be used for a MutlicastSocket
 
111
     * @return  a new instance of <code>PlainDatagramSocketImpl</code>.
 
112
     */
 
113
    static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
 
114
        throws SocketException {
 
115
        if (prefixImplClass != null) {
 
116
            try {
 
117
                return (DatagramSocketImpl) prefixImplClass.newInstance();
 
118
            } catch (Exception e) {
 
119
                throw new SocketException("can't instantiate DatagramSocketImpl");
 
120
            }
 
121
        } else {
 
122
            if (useDualStackImpl && !isMulticast)
 
123
                return new DualStackPlainDatagramSocketImpl();
 
124
            else
 
125
                return new TwoStacksPlainDatagramSocketImpl();
 
126
        }
 
127
    }
 
128
}