1 /* |
1 /* |
2 * Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 * Copyright 2000-2009 Sun Microsystems, Inc. All Rights Reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Sun designates this |
7 * published by the Free Software Foundation. Sun designates this |
25 |
25 |
26 package sun.nio.ch; |
26 package sun.nio.ch; |
27 |
27 |
28 import java.io.FileDescriptor; |
28 import java.io.FileDescriptor; |
29 import java.io.IOException; |
29 import java.io.IOException; |
30 import java.lang.reflect.*; |
|
31 import java.net.*; |
30 import java.net.*; |
32 import java.nio.channels.*; |
31 import java.nio.channels.*; |
33 import java.nio.channels.spi.*; |
32 import java.nio.channels.spi.*; |
34 import java.security.AccessController; |
|
35 import java.security.PrivilegedAction; |
|
36 import java.util.*; |
33 import java.util.*; |
37 |
34 |
38 |
35 |
39 /** |
36 /** |
40 * An implementation of ServerSocketChannels |
37 * An implementation of ServerSocketChannels |
109 |
106 |
110 @Override |
107 @Override |
111 public SocketAddress getLocalAddress() throws IOException { |
108 public SocketAddress getLocalAddress() throws IOException { |
112 synchronized (stateLock) { |
109 synchronized (stateLock) { |
113 if (!isOpen()) |
110 if (!isOpen()) |
114 return null; |
111 throw new ClosedChannelException(); |
115 return localAddress; |
112 return localAddress; |
116 } |
113 } |
117 } |
114 } |
118 |
115 |
119 @Override |
116 @Override |
120 public ServerSocketChannel setOption(SocketOption name, Object value) |
117 public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
121 throws IOException |
118 throws IOException |
122 { |
119 { |
123 if (name == null) |
120 if (name == null) |
124 throw new NullPointerException(); |
121 throw new NullPointerException(); |
125 if (!options().contains(name)) |
122 if (!supportedOptions().contains(name)) |
126 throw new IllegalArgumentException("invalid option name"); |
123 throw new UnsupportedOperationException("'" + name + "' not supported"); |
127 |
124 |
128 synchronized (stateLock) { |
125 synchronized (stateLock) { |
129 if (!isOpen()) |
126 if (!isOpen()) |
130 throw new ClosedChannelException(); |
127 throw new ClosedChannelException(); |
131 |
128 |
140 public <T> T getOption(SocketOption<T> name) |
137 public <T> T getOption(SocketOption<T> name) |
141 throws IOException |
138 throws IOException |
142 { |
139 { |
143 if (name == null) |
140 if (name == null) |
144 throw new NullPointerException(); |
141 throw new NullPointerException(); |
145 if (!options().contains(name)) |
142 if (!supportedOptions().contains(name)) |
146 throw new IllegalArgumentException("invalid option name"); |
143 throw new UnsupportedOperationException("'" + name + "' not supported"); |
147 |
144 |
148 synchronized (stateLock) { |
145 synchronized (stateLock) { |
149 if (!isOpen()) |
146 if (!isOpen()) |
150 throw new ClosedChannelException(); |
147 throw new ClosedChannelException(); |
151 |
148 |
152 // no options that require special handling |
149 // no options that require special handling |
153 return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
150 return (T) Net.getSocketOption(fd, Net.UNSPEC, name); |
154 } |
151 } |
155 } |
152 } |
156 |
153 |
157 private static class LazyInitialization { |
154 private static class DefaultOptionsHolder { |
158 static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
155 static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
159 |
156 |
160 private static Set<SocketOption<?>> defaultOptions() { |
157 private static Set<SocketOption<?>> defaultOptions() { |
161 HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(2); |
158 HashSet<SocketOption<?>> set = new HashSet<SocketOption<?>>(2); |
162 set.add(StandardSocketOption.SO_RCVBUF); |
159 set.add(StandardSocketOption.SO_RCVBUF); |
164 return Collections.unmodifiableSet(set); |
161 return Collections.unmodifiableSet(set); |
165 } |
162 } |
166 } |
163 } |
167 |
164 |
168 @Override |
165 @Override |
169 public final Set<SocketOption<?>> options() { |
166 public final Set<SocketOption<?>> supportedOptions() { |
170 return LazyInitialization.defaultOptions; |
167 return DefaultOptionsHolder.defaultOptions; |
171 } |
168 } |
172 |
169 |
173 public boolean isBound() { |
170 public boolean isBound() { |
174 synchronized (stateLock) { |
171 synchronized (stateLock) { |
175 return localAddress != null; |
172 return localAddress != null; |