Path: blob/master/src/java.base/share/classes/java/nio/channels/spi/AsynchronousChannelProvider.java
41161 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels.spi;2627import java.nio.channels.*;28import java.io.IOException;29import java.util.Iterator;30import java.util.ServiceLoader;31import java.util.ServiceConfigurationError;32import java.util.concurrent.*;33import java.security.AccessController;34import java.security.PrivilegedAction;3536/**37* Service-provider class for asynchronous channels.38*39* <p> An asynchronous channel provider is a concrete subclass of this class that40* has a zero-argument constructor and implements the abstract methods specified41* below. A given invocation of the Java virtual machine maintains a single42* system-wide default provider instance, which is returned by the {@link43* #provider() provider} method. The first invocation of that method will locate44* the default provider as specified below.45*46* <p> All of the methods in this class are safe for use by multiple concurrent47* threads. </p>48*49* @since 1.750*/5152public abstract class AsynchronousChannelProvider {53private static Void checkPermission() {54@SuppressWarnings("removal")55SecurityManager sm = System.getSecurityManager();56if (sm != null)57sm.checkPermission(new RuntimePermission("asynchronousChannelProvider"));58return null;59}60private AsynchronousChannelProvider(Void ignore) { }6162/**63* Initializes a new instance of this class.64*65* @throws SecurityException66* If a security manager has been installed and it denies67* {@link RuntimePermission}{@code ("asynchronousChannelProvider")}68*/69protected AsynchronousChannelProvider() {70this(checkPermission());71}7273// lazy initialization of default provider74private static class ProviderHolder {75static final AsynchronousChannelProvider provider = load();7677@SuppressWarnings("removal")78private static AsynchronousChannelProvider load() {79return AccessController80.doPrivileged(new PrivilegedAction<>() {81public AsynchronousChannelProvider run() {82AsynchronousChannelProvider p;83p = loadProviderFromProperty();84if (p != null)85return p;86p = loadProviderAsService();87if (p != null)88return p;89return sun.nio.ch.DefaultAsynchronousChannelProvider.create();90}});91}9293private static AsynchronousChannelProvider loadProviderFromProperty() {94String cn = System.getProperty("java.nio.channels.spi.AsynchronousChannelProvider");95if (cn == null)96return null;97try {98@SuppressWarnings("deprecation")99Object tmp = Class.forName(cn, true,100ClassLoader.getSystemClassLoader()).newInstance();101return (AsynchronousChannelProvider)tmp;102} catch (ClassNotFoundException x) {103throw new ServiceConfigurationError(null, x);104} catch (IllegalAccessException x) {105throw new ServiceConfigurationError(null, x);106} catch (InstantiationException x) {107throw new ServiceConfigurationError(null, x);108} catch (SecurityException x) {109throw new ServiceConfigurationError(null, x);110}111}112113private static AsynchronousChannelProvider loadProviderAsService() {114ServiceLoader<AsynchronousChannelProvider> sl =115ServiceLoader.load(AsynchronousChannelProvider.class,116ClassLoader.getSystemClassLoader());117Iterator<AsynchronousChannelProvider> i = sl.iterator();118for (;;) {119try {120return (i.hasNext()) ? i.next() : null;121} catch (ServiceConfigurationError sce) {122if (sce.getCause() instanceof SecurityException) {123// Ignore the security exception, try the next provider124continue;125}126throw sce;127}128}129}130}131132/**133* Returns the system-wide default asynchronous channel provider for this134* invocation of the Java virtual machine.135*136* <p> The first invocation of this method locates the default provider137* object as follows: </p>138*139* <ol>140*141* <li><p> If the system property142* {@systemProperty java.nio.channels.spi.AsynchronousChannelProvider} is143* defined then it is taken to be the fully-qualified name of a concrete144* provider class. The class is loaded and instantiated; if this process145* fails then an unspecified error is thrown. </p></li>146*147* <li><p> If a provider class has been installed in a jar file that is148* visible to the system class loader, and that jar file contains a149* provider-configuration file named150* {@code java.nio.channels.spi.AsynchronousChannelProvider} in the resource151* directory {@code META-INF/services}, then the first class name152* specified in that file is taken. The class is loaded and153* instantiated; if this process fails then an unspecified error is154* thrown. </p></li>155*156* <li><p> Finally, if no provider has been specified by any of the above157* means then the system-default provider class is instantiated and the158* result is returned. </p></li>159*160* </ol>161*162* <p> Subsequent invocations of this method return the provider that was163* returned by the first invocation. </p>164*165* @return The system-wide default AsynchronousChannel provider166*/167public static AsynchronousChannelProvider provider() {168return ProviderHolder.provider;169}170171/**172* Constructs a new asynchronous channel group with a fixed thread pool.173*174* @param nThreads175* The number of threads in the pool176* @param threadFactory177* The factory to use when creating new threads178*179* @return A new asynchronous channel group180*181* @throws IllegalArgumentException182* If {@code nThreads <= 0}183* @throws IOException184* If an I/O error occurs185*186* @see AsynchronousChannelGroup#withFixedThreadPool187*/188public abstract AsynchronousChannelGroup189openAsynchronousChannelGroup(int nThreads, ThreadFactory threadFactory) throws IOException;190191/**192* Constructs a new asynchronous channel group with the given thread pool.193*194* @param executor195* The thread pool196* @param initialSize197* A value {@code >=0} or a negative value for implementation198* specific default199*200* @return A new asynchronous channel group201*202* @throws IOException203* If an I/O error occurs204*205* @see AsynchronousChannelGroup#withCachedThreadPool206*/207public abstract AsynchronousChannelGroup208openAsynchronousChannelGroup(ExecutorService executor, int initialSize) throws IOException;209210/**211* Opens an asynchronous server-socket channel.212*213* @param group214* The group to which the channel is bound, or {@code null} to215* bind to the default group216*217* @return The new channel218*219* @throws IllegalChannelGroupException220* If the provider that created the group differs from this provider221* @throws ShutdownChannelGroupException222* The group is shutdown223* @throws IOException224* If an I/O error occurs225*/226public abstract AsynchronousServerSocketChannel openAsynchronousServerSocketChannel227(AsynchronousChannelGroup group) throws IOException;228229/**230* Opens an asynchronous socket channel.231*232* @param group233* The group to which the channel is bound, or {@code null} to234* bind to the default group235*236* @return The new channel237*238* @throws IllegalChannelGroupException239* If the provider that created the group differs from this provider240* @throws ShutdownChannelGroupException241* The group is shutdown242* @throws IOException243* If an I/O error occurs244*/245public abstract AsynchronousSocketChannel openAsynchronousSocketChannel246(AsynchronousChannelGroup group) throws IOException;247}248249250