author | chegar |
Wed, 27 Apr 2016 20:36:02 +0100 | |
changeset 37676 | 24ef455da1b0 |
parent 34894 | jdk/src/java.base/share/classes/jdk/net/ExtendedSocketOptions.java@3248b89d1921 |
permissions | -rw-r--r-- |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
1 |
/* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
2 |
* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
4 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
10 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
16 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
20 |
* |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
23 |
* questions. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
24 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
25 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
26 |
package jdk.net; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
27 |
|
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
28 |
import java.io.FileDescriptor; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
29 |
import java.net.SocketException; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
30 |
import java.net.SocketOption; |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
31 |
import java.security.AccessController; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
32 |
import java.security.PrivilegedAction; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
33 |
import java.util.Collections; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
34 |
import java.util.Set; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
35 |
import jdk.internal.misc.JavaIOFileDescriptorAccess; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
36 |
import jdk.internal.misc.SharedSecrets; |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
37 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
38 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
39 |
* Defines extended socket options, beyond those defined in |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
40 |
* {@link java.net.StandardSocketOptions}. These options may be platform |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
41 |
* specific. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
42 |
* |
24713
5451c8499988
8044766: New jdk.net classes have @since 1.9 tags in 8u20
michaelm
parents:
23879
diff
changeset
|
43 |
* @since 1.8 |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
44 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
45 |
public final class ExtendedSocketOptions { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
46 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
47 |
private static class ExtSocketOption<T> implements SocketOption<T> { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
48 |
private final String name; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
49 |
private final Class<T> type; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
50 |
ExtSocketOption(String name, Class<T> type) { |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
51 |
this.name = name; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
52 |
this.type = type; |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
53 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
54 |
@Override public String name() { return name; } |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
55 |
@Override public Class<T> type() { return type; } |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
56 |
@Override public String toString() { return name; } |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
57 |
} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
58 |
|
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
59 |
private ExtendedSocketOptions() { } |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
60 |
|
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
61 |
/** |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
62 |
* Service level properties. When a security manager is installed, |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
63 |
* setting or getting this option requires a {@link NetworkPermission} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
64 |
* {@code ("setOption.SO_FLOW_SLA")} or {@code "getOption.SO_FLOW_SLA"} |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
65 |
* respectively. |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
66 |
*/ |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
67 |
public static final SocketOption<SocketFlow> SO_FLOW_SLA = new |
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
68 |
ExtSocketOption<SocketFlow>("SO_FLOW_SLA", SocketFlow.class); |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
69 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
70 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
71 |
private static final PlatformSocketOptions platformSocketOptions = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
72 |
PlatformSocketOptions.get(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
73 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
74 |
private static final boolean flowSupported = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
75 |
platformSocketOptions.flowSupported(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
76 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
77 |
private static final Set<SocketOption<?>> extendedOptions = options(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
78 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
79 |
static Set<SocketOption<?>> options() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
80 |
if (flowSupported) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
81 |
return Set.of(SO_FLOW_SLA); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
82 |
else |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
83 |
return Collections.<SocketOption<?>>emptySet(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
84 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
85 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
86 |
static { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
87 |
// Registers the extended socket options with the base module. |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
88 |
sun.net.ext.ExtendedSocketOptions.register( |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
89 |
new sun.net.ext.ExtendedSocketOptions(extendedOptions) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
90 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
91 |
@Override |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
92 |
public void setOption(FileDescriptor fd, |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
93 |
SocketOption<?> option, |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
94 |
Object value) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
95 |
throws SocketException |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
96 |
{ |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
97 |
SecurityManager sm = System.getSecurityManager(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
98 |
if (sm != null) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
99 |
sm.checkPermission(new NetworkPermission("setOption." + option.name())); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
100 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
101 |
if (fd == null || !fd.valid()) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
102 |
throw new SocketException("socket closed"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
103 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
104 |
if (option == SO_FLOW_SLA) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
105 |
assert flowSupported; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
106 |
SocketFlow flow = checkValueType(value, option.type()); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
107 |
setFlowOption(fd, flow); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
108 |
} else { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
109 |
throw new InternalError("Unexpected option " + option); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
110 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
111 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
112 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
113 |
@Override |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
114 |
public Object getOption(FileDescriptor fd, |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
115 |
SocketOption<?> option) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
116 |
throws SocketException |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
117 |
{ |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
118 |
SecurityManager sm = System.getSecurityManager(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
119 |
if (sm != null) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
120 |
sm.checkPermission(new NetworkPermission("getOption." + option.name())); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
121 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
122 |
if (fd == null || !fd.valid()) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
123 |
throw new SocketException("socket closed"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
124 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
125 |
if (option == SO_FLOW_SLA) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
126 |
assert flowSupported; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
127 |
SocketFlow flow = SocketFlow.create(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
128 |
getFlowOption(fd, flow); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
129 |
return flow; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
130 |
} else { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
131 |
throw new InternalError("Unexpected option " + option); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
132 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
133 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
134 |
}); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
135 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
136 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
137 |
@SuppressWarnings("unchecked") |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
138 |
private static <T> T checkValueType(Object value, Class<?> type) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
139 |
if (!type.isAssignableFrom(value.getClass())) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
140 |
String s = "Found: " + value.getClass() + ", Expected: " + type; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
141 |
throw new IllegalArgumentException(s); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
142 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
143 |
return (T) value; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
144 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
145 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
146 |
private static final JavaIOFileDescriptorAccess fdAccess = |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
147 |
SharedSecrets.getJavaIOFileDescriptorAccess(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
148 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
149 |
private static void setFlowOption(FileDescriptor fd, SocketFlow f) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
150 |
throws SocketException |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
151 |
{ |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
152 |
int status = platformSocketOptions.setFlowOption(fdAccess.get(fd), |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
153 |
f.priority(), |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
154 |
f.bandwidth()); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
155 |
f.status(status); // augment the given flow with the status |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
156 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
157 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
158 |
private static void getFlowOption(FileDescriptor fd, SocketFlow f) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
159 |
throws SocketException |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
160 |
{ |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
161 |
int status = platformSocketOptions.getFlowOption(fdAccess.get(fd), f); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
162 |
f.status(status); // augment the given flow with the status |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
163 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
164 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
165 |
static class PlatformSocketOptions { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
166 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
167 |
protected PlatformSocketOptions() {} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
168 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
169 |
@SuppressWarnings("unchecked") |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
170 |
private static PlatformSocketOptions newInstance(String cn) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
171 |
Class<PlatformSocketOptions> c; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
172 |
try { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
173 |
c = (Class<PlatformSocketOptions>)Class.forName(cn); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
174 |
return c.getConstructor(new Class<?>[] { }).newInstance(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
175 |
} catch (ReflectiveOperationException x) { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
176 |
throw new AssertionError(x); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
177 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
178 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
179 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
180 |
private static PlatformSocketOptions create() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
181 |
String osname = AccessController.doPrivileged( |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
182 |
new PrivilegedAction<String>() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
183 |
public String run() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
184 |
return System.getProperty("os.name"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
185 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
186 |
}); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
187 |
if ("SunOS".equals(osname)) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
188 |
return newInstance("jdk.net.SolarisSocketOptions"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
189 |
return new PlatformSocketOptions(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
190 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
191 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
192 |
private static final PlatformSocketOptions instance = create(); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
193 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
194 |
static PlatformSocketOptions get() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
195 |
return instance; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
196 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
197 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
198 |
int setFlowOption(int fd, int priority, long bandwidth) |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
199 |
throws SocketException |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
200 |
{ |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
201 |
throw new UnsupportedOperationException("unsupported socket option"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
202 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
203 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
204 |
int getFlowOption(int fd, SocketFlow f) throws SocketException { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
205 |
throw new UnsupportedOperationException("unsupported socket option"); |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
206 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
207 |
|
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
208 |
boolean flowSupported() { |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
209 |
return false; |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
210 |
} |
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
34894
diff
changeset
|
211 |
} |
23879
284802a2d355
8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff
changeset
|
212 |
} |