Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/ftp/FtpClientProvider.java
38831 views
/*1* Copyright (c) 2009, 2011, 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*/24package sun.net.ftp;2526import java.security.AccessController;27import java.security.PrivilegedAction;28import java.util.ServiceConfigurationError;29//import java.util.ServiceLoader;3031/**32* Service provider class for FtpClient.33* Sub-classes of FtpClientProvider provide an implementation of {@link FtpClient}34* and associated classes. Applications do not normally use this class directly.35* See {@link #provider() } for how providers are found and loaded.36*37* @since 1.738*/39public abstract class FtpClientProvider {4041/**42* Creates a FtpClient from this provider.43*44* @return The created {@link FtpClient}.45*/46public abstract FtpClient createFtpClient();47private static final Object lock = new Object();48private static FtpClientProvider provider = null;4950/**51* Initializes a new instance of this class.52*53* @throws SecurityException if a security manager is installed and it denies54* {@link RuntimePermission}<tt>("ftpClientProvider")</tt>55*/56protected FtpClientProvider() {57SecurityManager sm = System.getSecurityManager();58if (sm != null) {59sm.checkPermission(new RuntimePermission("ftpClientProvider"));60}61}6263private static boolean loadProviderFromProperty() {64String cm = System.getProperty("sun.net.ftpClientProvider");65if (cm == null) {66return false;67}68try {69Class<?> c = Class.forName(cm, true, null);70provider = (FtpClientProvider) c.newInstance();71return true;72} catch (ClassNotFoundException |73IllegalAccessException |74InstantiationException |75SecurityException x) {76throw new ServiceConfigurationError(x.toString());77}78}7980private static boolean loadProviderAsService() {81// Iterator<FtpClientProvider> i =82// ServiceLoader.load(FtpClientProvider.class,83// ClassLoader.getSystemClassLoader()).iterator();84//85// while (i.hasNext()) {86// try {87// provider = i.next();88// return true;89// } catch (ServiceConfigurationError sce) {90// if (sce.getCause() instanceof SecurityException) {91// // Ignore, try next provider, if any92// continue;93// }94// throw sce;95// }96// }97return false;98}99100/**101* Returns the system wide default FtpClientProvider for this invocation of102* the Java virtual machine.103*104* <p> The first invocation of this method locates the default provider105* object as follows: </p>106*107* <ol>108*109* <li><p> If the system property110* <tt>java.net.FtpClientProvider</tt> is defined then it is111* taken to be the fully-qualified name of a concrete provider class.112* The class is loaded and instantiated; if this process fails then an113* unspecified unchecked error or exception is thrown. </p></li>114*115* <li><p> If a provider class has been installed in a jar file that is116* visible to the system class loader, and that jar file contains a117* provider-configuration file named118* <tt>java.net.FtpClientProvider</tt> in the resource119* directory <tt>META-INF/services</tt>, then the first class name120* specified in that file is taken. The class is loaded and121* instantiated; if this process fails then an unspecified unchecked error or exception is122* thrown. </p></li>123*124* <li><p> Finally, if no provider has been specified by any of the above125* means then the system-default provider class is instantiated and the126* result is returned. </p></li>127*128* </ol>129*130* <p> Subsequent invocations of this method return the provider that was131* returned by the first invocation. </p>132*133* @return The system-wide default FtpClientProvider134*/135public static FtpClientProvider provider() {136synchronized (lock) {137if (provider != null) {138return provider;139}140return (FtpClientProvider) AccessController.doPrivileged(141new PrivilegedAction<Object>() {142143public Object run() {144if (loadProviderFromProperty()) {145return provider;146}147if (loadProviderAsService()) {148return provider;149}150provider = new sun.net.ftp.impl.DefaultFtpClientProvider();151return provider;152}153});154}155}156}157158159