Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/spi/AsynchronousChannelProvider.java
38918 views
/*1* Copyright (c) 2007, 2013, 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() {54SecurityManager sm = System.getSecurityManager();55if (sm != null)56sm.checkPermission(new RuntimePermission("asynchronousChannelProvider"));57return null;58}59private AsynchronousChannelProvider(Void ignore) { }6061/**62* Initializes a new instance of this class.63*64* @throws SecurityException65* If a security manager has been installed and it denies66* {@link RuntimePermission}<tt>("asynchronousChannelProvider")</tt>67*/68protected AsynchronousChannelProvider() {69this(checkPermission());70}7172// lazy initialization of default provider73private static class ProviderHolder {74static final AsynchronousChannelProvider provider = load();7576private static AsynchronousChannelProvider load() {77return AccessController78.doPrivileged(new PrivilegedAction<AsynchronousChannelProvider>() {79public AsynchronousChannelProvider run() {80AsynchronousChannelProvider p;81p = loadProviderFromProperty();82if (p != null)83return p;84p = loadProviderAsService();85if (p != null)86return p;87return sun.nio.ch.DefaultAsynchronousChannelProvider.create();88}});89}9091private static AsynchronousChannelProvider loadProviderFromProperty() {92String cn = System.getProperty("java.nio.channels.spi.AsynchronousChannelProvider");93if (cn == null)94return null;95try {96Class<?> c = Class.forName(cn, true,97ClassLoader.getSystemClassLoader());98return (AsynchronousChannelProvider)c.newInstance();99} catch (ClassNotFoundException x) {100throw new ServiceConfigurationError(null, x);101} catch (IllegalAccessException x) {102throw new ServiceConfigurationError(null, x);103} catch (InstantiationException x) {104throw new ServiceConfigurationError(null, x);105} catch (SecurityException x) {106throw new ServiceConfigurationError(null, x);107}108}109110private static AsynchronousChannelProvider loadProviderAsService() {111ServiceLoader<AsynchronousChannelProvider> sl =112ServiceLoader.load(AsynchronousChannelProvider.class,113ClassLoader.getSystemClassLoader());114Iterator<AsynchronousChannelProvider> i = sl.iterator();115for (;;) {116try {117return (i.hasNext()) ? i.next() : null;118} catch (ServiceConfigurationError sce) {119if (sce.getCause() instanceof SecurityException) {120// Ignore the security exception, try the next provider121continue;122}123throw sce;124}125}126}127}128129/**130* Returns the system-wide default asynchronous channel provider for this131* invocation of the Java virtual machine.132*133* <p> The first invocation of this method locates the default provider134* object as follows: </p>135*136* <ol>137*138* <li><p> If the system property139* <tt>java.nio.channels.spi.AsynchronousChannelProvider</tt> is defined140* then it is taken to be the fully-qualified name of a concrete provider class.141* The class is loaded and instantiated; if this process fails then an142* unspecified error is thrown. </p></li>143*144* <li><p> If a provider class has been installed in a jar file that is145* visible to the system class loader, and that jar file contains a146* provider-configuration file named147* <tt>java.nio.channels.spi.AsynchronousChannelProvider</tt> in the resource148* directory <tt>META-INF/services</tt>, then the first class name149* specified in that file is taken. The class is loaded and150* instantiated; if this process fails then an unspecified error is151* thrown. </p></li>152*153* <li><p> Finally, if no provider has been specified by any of the above154* means then the system-default provider class is instantiated and the155* result is returned. </p></li>156*157* </ol>158*159* <p> Subsequent invocations of this method return the provider that was160* returned by the first invocation. </p>161*162* @return The system-wide default AsynchronousChannel provider163*/164public static AsynchronousChannelProvider provider() {165return ProviderHolder.provider;166}167168/**169* Constructs a new asynchronous channel group with a fixed thread pool.170*171* @param nThreads172* The number of threads in the pool173* @param threadFactory174* The factory to use when creating new threads175*176* @return A new asynchronous channel group177*178* @throws IllegalArgumentException179* If {@code nThreads <= 0}180* @throws IOException181* If an I/O error occurs182*183* @see AsynchronousChannelGroup#withFixedThreadPool184*/185public abstract AsynchronousChannelGroup186openAsynchronousChannelGroup(int nThreads, ThreadFactory threadFactory) throws IOException;187188/**189* Constructs a new asynchronous channel group with the given thread pool.190*191* @param executor192* The thread pool193* @param initialSize194* A value {@code >=0} or a negative value for implementation195* specific default196*197* @return A new asynchronous channel group198*199* @throws IOException200* If an I/O error occurs201*202* @see AsynchronousChannelGroup#withCachedThreadPool203*/204public abstract AsynchronousChannelGroup205openAsynchronousChannelGroup(ExecutorService executor, int initialSize) throws IOException;206207/**208* Opens an asynchronous server-socket channel.209*210* @param group211* The group to which the channel is bound, or {@code null} to212* bind to the default group213*214* @return The new channel215*216* @throws IllegalChannelGroupException217* If the provider that created the group differs from this provider218* @throws ShutdownChannelGroupException219* The group is shutdown220* @throws IOException221* If an I/O error occurs222*/223public abstract AsynchronousServerSocketChannel openAsynchronousServerSocketChannel224(AsynchronousChannelGroup group) throws IOException;225226/**227* Opens an asynchronous socket channel.228*229* @param group230* The group to which the channel is bound, or {@code null} to231* bind to the default group232*233* @return The new channel234*235* @throws IllegalChannelGroupException236* If the provider that created the group differs from this provider237* @throws ShutdownChannelGroupException238* The group is shutdown239* @throws IOException240* If an I/O error occurs241*/242public abstract AsynchronousSocketChannel openAsynchronousSocketChannel243(AsynchronousChannelGroup group) throws IOException;244}245246247