2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package java.nio.channels.spi;
20 import java.io.IOException;
21 import java.nio.channels.Channel;
22 import java.nio.channels.DatagramChannel;
23 import java.nio.channels.Pipe;
24 import java.nio.channels.ServerSocketChannel;
25 import java.nio.channels.SocketChannel;
26 import java.security.AccessController;
27 import java.security.PrivilegedAction;
28 import java.util.ServiceLoader;
29 import org.apache.harmony.nio.internal.SelectorProviderImpl;
32 * {@code SelectorProvider} is an abstract base class that declares methods for
33 * providing instances of {@link DatagramChannel}, {@link Pipe},
34 * {@link java.nio.channels.Selector} , {@link ServerSocketChannel}, and
35 * {@link SocketChannel}. All the methods of this class are thread-safe.
37 * <p>A provider instance can be retrieved through a system property or the
38 * configuration file in a jar file; if no provider is available that way then
39 * the system default provider is returned.
41 public abstract class SelectorProvider {
42 private static SelectorProvider provider = null;
45 * Constructs a new {@code SelectorProvider}.
47 * @throws SecurityException
48 * if there is a security manager installed that does not permit
49 * the runtime permission labeled "selectorProvider".
51 protected SelectorProvider() {
53 if (null != System.getSecurityManager()) {
54 System.getSecurityManager().checkPermission(
55 new RuntimePermission("selectorProvider"));
60 * Gets a provider instance by executing the following steps when called for
63 * <li> if the system property "java.nio.channels.spi.SelectorProvider" is
64 * set, the value of this property is the class name of the provider
66 * <li>if there is a provider-configuration file named
67 * "java.nio.channels.spi.SelectorProvider" in META-INF/services of a jar
68 * file valid in the system class loader, the first class name is the
69 * provider's class name; </li>
70 * <li> otherwise, a system default provider will be returned.</li>
73 * @return the provider.
75 synchronized public static SelectorProvider provider() {
76 if (provider == null) {
77 provider = ServiceLoader.loadFromSystemProperty(SelectorProvider.class);
78 if (provider == null) {
79 provider = loadProviderByJar();
81 if (provider == null) {
82 provider = AccessController.doPrivileged(new PrivilegedAction<SelectorProvider>() {
83 public SelectorProvider run() {
84 return new SelectorProviderImpl();
92 private static SelectorProvider loadProviderByJar() {
93 for (SelectorProvider provider : ServiceLoader.load(SelectorProvider.class, null)) {
100 * Creates a new open {@code DatagramChannel}.
102 * @return the new channel.
103 * @throws IOException
104 * if an I/O error occurs.
106 public abstract DatagramChannel openDatagramChannel() throws IOException;
109 * Creates a new {@code Pipe}.
111 * @return the new pipe.
112 * @throws IOException
113 * if an I/O error occurs.
115 public abstract Pipe openPipe() throws IOException;
118 * Creates a new selector.
120 * @return the new selector.
121 * @throws IOException
122 * if an I/O error occurs.
124 public abstract AbstractSelector openSelector() throws IOException;
127 * Creates a new open {@code ServerSocketChannel}.
129 * @return the new channel.
130 * @throws IOException
131 * if an I/O error occurs.
133 public abstract ServerSocketChannel openServerSocketChannel()
137 * Create a new open {@code SocketChannel}.
139 * @return the new channel.
140 * @throws IOException
141 * if an I/O error occurs.
143 public abstract SocketChannel openSocketChannel() throws IOException;
146 * Returns the channel inherited from the instance that created this
149 * @return the channel.
150 * @throws IOException
151 * if an I/O error occurs.
152 * @throws SecurityException
153 * if there is a security manager installed that does not permit
154 * the runtime permission labeled "selectorProvider".
156 public Channel inheritedChannel() throws IOException {
157 SecurityManager sm = System.getSecurityManager();
159 sm.checkPermission(new RuntimePermission("inheritedChannel"));
161 // Android never has stdin/stdout connected to a socket.