2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
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 |
// (version >= 6.0) implies Vista or greater.
|
|
75 |
if (version >= 6.0 && !preferIPv4Stack) {
|
|
76 |
useDualStackImpl = true;
|
|
77 |
}
|
|
78 |
|
|
79 |
// impl.prefix
|
|
80 |
String prefix = null;
|
|
81 |
try {
|
|
82 |
prefix = AccessController.doPrivileged(
|
|
83 |
new sun.security.action.GetPropertyAction("impl.prefix", null));
|
|
84 |
if (prefix != null)
|
|
85 |
prefixImplClass = Class.forName("java.net."+prefix+"DatagramSocketImpl");
|
|
86 |
} catch (Exception e) {
|
|
87 |
System.err.println("Can't find class: java.net." +
|
|
88 |
prefix +
|
|
89 |
"DatagramSocketImpl: check impl.prefix property");
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Creates a new <code>DatagramSocketImpl</code> instance.
|
|
95 |
*
|
|
96 |
* @param isMulticast true if this impl is to be used for a MutlicastSocket
|
|
97 |
* @return a new instance of <code>PlainDatagramSocketImpl</code>.
|
|
98 |
*/
|
|
99 |
static DatagramSocketImpl createDatagramSocketImpl(boolean isMulticast)
|
|
100 |
throws SocketException {
|
|
101 |
if (prefixImplClass != null) {
|
|
102 |
try {
|
|
103 |
return (DatagramSocketImpl) prefixImplClass.newInstance();
|
|
104 |
} catch (Exception e) {
|
|
105 |
throw new SocketException("can't instantiate DatagramSocketImpl");
|
|
106 |
}
|
|
107 |
} else {
|
|
108 |
if (useDualStackImpl && !isMulticast)
|
|
109 |
return new DualStackPlainDatagramSocketImpl();
|
|
110 |
else
|
|
111 |
return new TwoStacksPlainDatagramSocketImpl();
|
|
112 |
}
|
|
113 |
}
|
|
114 |
}
|