Path: blob/master/src/java.base/share/classes/sun/net/ftp/FtpClientProvider.java
41159 views
/*1* Copyright (c) 2009, 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*/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}{@code ("ftpClientProvider")}55*/56protected FtpClientProvider() {57@SuppressWarnings("removal")58SecurityManager sm = System.getSecurityManager();59if (sm != null) {60sm.checkPermission(new RuntimePermission("ftpClientProvider"));61}62}6364private static boolean loadProviderFromProperty() {65String cm = System.getProperty("sun.net.ftpClientProvider");66if (cm == null) {67return false;68}69try {70@SuppressWarnings("deprecation")71Object o = Class.forName(cm, true, null).newInstance();72provider = (FtpClientProvider)o;73return true;74} catch (ClassNotFoundException |75IllegalAccessException |76InstantiationException |77SecurityException x) {78throw new ServiceConfigurationError(x.toString());79}80}8182private static boolean loadProviderAsService() {83// Iterator<FtpClientProvider> i =84// ServiceLoader.load(FtpClientProvider.class,85// ClassLoader.getSystemClassLoader()).iterator();86//87// while (i.hasNext()) {88// try {89// provider = i.next();90// return true;91// } catch (ServiceConfigurationError sce) {92// if (sce.getCause() instanceof SecurityException) {93// // Ignore, try next provider, if any94// continue;95// }96// throw sce;97// }98// }99return false;100}101102/**103* Returns the system wide default FtpClientProvider for this invocation of104* the Java virtual machine.105*106* <p> The first invocation of this method locates the default provider107* object as follows: </p>108*109* <ol>110*111* <li><p> If the system property112* {@code java.net.FtpClientProvider} is defined then it is113* taken to be the fully-qualified name of a concrete provider class.114* The class is loaded and instantiated; if this process fails then an115* unspecified unchecked error or exception is thrown. </p></li>116*117* <li><p> If a provider class has been installed in a jar file that is118* visible to the system class loader, and that jar file contains a119* provider-configuration file named120* {@code java.net.FtpClientProvider} in the resource121* directory {@code META-INF/services}, then the first class name122* specified in that file is taken. The class is loaded and123* instantiated; if this process fails then an unspecified unchecked error or exception is124* thrown. </p></li>125*126* <li><p> Finally, if no provider has been specified by any of the above127* means then the system-default provider class is instantiated and the128* result is returned. </p></li>129*130* </ol>131*132* <p> Subsequent invocations of this method return the provider that was133* returned by the first invocation. </p>134*135* @return The system-wide default FtpClientProvider136*/137@SuppressWarnings("removal")138public static FtpClientProvider provider() {139synchronized (lock) {140if (provider != null) {141return provider;142}143return (FtpClientProvider) AccessController.doPrivileged(144new PrivilegedAction<Object>() {145146public Object run() {147if (loadProviderFromProperty()) {148return provider;149}150if (loadProviderAsService()) {151return provider;152}153provider = new sun.net.ftp.impl.DefaultFtpClientProvider();154return provider;155}156});157}158}159}160161162