author | chegar |
Fri, 16 Sep 2011 12:09:04 -0700 | |
changeset 10596 | 39b3a979e600 |
parent 5506 | 202f599c92aa |
child 11124 | 5fdabdc21259 |
permissions | -rw-r--r-- |
4155 | 1 |
/* |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
4155 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
4155 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4155 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
4155 | 24 |
*/ |
25 |
package sun.net.ftp; |
|
26 |
||
27 |
import java.security.AccessController; |
|
28 |
import java.security.PrivilegedAction; |
|
29 |
import java.util.ServiceConfigurationError; |
|
30 |
//import sun.misc.Service; |
|
31 |
||
32 |
/** |
|
33 |
* Service provider class for FtpClient. |
|
34 |
* Sub-classes of FtpClientProvider provide an implementation of {@link FtpClient} |
|
35 |
* and associated classes. Applications do not normally use this class directly. |
|
36 |
* See {@link #provider() } for how providers are found and loaded. |
|
37 |
* |
|
38 |
* @since 1.7 |
|
39 |
*/ |
|
40 |
public abstract class FtpClientProvider { |
|
41 |
||
42 |
/** |
|
43 |
* Creates a FtpClient from this provider. |
|
44 |
* |
|
45 |
* @return The created {@link FtpClient}. |
|
46 |
*/ |
|
47 |
public abstract FtpClient createFtpClient(); |
|
48 |
private static final Object lock = new Object(); |
|
49 |
private static FtpClientProvider provider = null; |
|
50 |
||
51 |
/** |
|
52 |
* Initializes a new instance of this class. |
|
53 |
* |
|
54 |
* @throws SecurityException if a security manager is installed and it denies |
|
55 |
* {@link RuntimePermission}<tt>("ftpClientProvider")</tt> |
|
56 |
*/ |
|
57 |
protected FtpClientProvider() { |
|
58 |
SecurityManager sm = System.getSecurityManager(); |
|
59 |
if (sm != null) { |
|
60 |
sm.checkPermission(new RuntimePermission("ftpClientProvider")); |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
private static boolean loadProviderFromProperty() { |
|
65 |
String cm = System.getProperty("sun.net.ftpClientProvider"); |
|
66 |
if (cm == null) { |
|
67 |
return false; |
|
68 |
} |
|
69 |
try { |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
70 |
Class<?> c = Class.forName(cm, true, null); |
4155 | 71 |
provider = (FtpClientProvider) c.newInstance(); |
72 |
return true; |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
73 |
} catch (ClassNotFoundException | |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
74 |
IllegalAccessException | |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
75 |
InstantiationException | |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
5506
diff
changeset
|
76 |
SecurityException x) { |
4155 | 77 |
throw new ServiceConfigurationError(x.toString()); |
78 |
} |
|
79 |
} |
|
80 |
||
81 |
private static boolean loadProviderAsService() { |
|
82 |
// Iterator i = Service.providers(FtpClientProvider.class, |
|
83 |
// ClassLoader.getSystemClassLoader()); |
|
84 |
// while (i.hasNext()) { |
|
85 |
// try { |
|
86 |
// provider = (FtpClientProvider) i.next(); |
|
87 |
// return true; |
|
88 |
// } catch (ServiceConfigurationError sce) { |
|
89 |
// if (sce.getCause() instanceof SecurityException) { |
|
90 |
// // Ignore, try next provider, if any |
|
91 |
// continue; |
|
92 |
// } |
|
93 |
// throw sce; |
|
94 |
// } |
|
95 |
// } |
|
96 |
return false; |
|
97 |
} |
|
98 |
||
99 |
/** |
|
100 |
* Returns the system wide default FtpClientProvider for this invocation of |
|
101 |
* the Java virtual machine. |
|
102 |
* |
|
103 |
* <p> The first invocation of this method locates the default provider |
|
104 |
* object as follows: </p> |
|
105 |
* |
|
106 |
* <ol> |
|
107 |
* |
|
108 |
* <li><p> If the system property |
|
109 |
* <tt>java.net.FtpClientProvider</tt> is defined then it is |
|
110 |
* taken to be the fully-qualified name of a concrete provider class. |
|
111 |
* The class is loaded and instantiated; if this process fails then an |
|
112 |
* unspecified unchecked error or exception is thrown. </p></li> |
|
113 |
* |
|
114 |
* <li><p> If a provider class has been installed in a jar file that is |
|
115 |
* visible to the system class loader, and that jar file contains a |
|
116 |
* provider-configuration file named |
|
117 |
* <tt>java.net.FtpClientProvider</tt> in the resource |
|
118 |
* directory <tt>META-INF/services</tt>, then the first class name |
|
119 |
* specified in that file is taken. The class is loaded and |
|
120 |
* instantiated; if this process fails then an unspecified unchecked error or exception is |
|
121 |
* thrown. </p></li> |
|
122 |
* |
|
123 |
* <li><p> Finally, if no provider has been specified by any of the above |
|
124 |
* means then the system-default provider class is instantiated and the |
|
125 |
* result is returned. </p></li> |
|
126 |
* |
|
127 |
* </ol> |
|
128 |
* |
|
129 |
* <p> Subsequent invocations of this method return the provider that was |
|
130 |
* returned by the first invocation. </p> |
|
131 |
* |
|
132 |
* @return The system-wide default FtpClientProvider |
|
133 |
*/ |
|
134 |
public static FtpClientProvider provider() { |
|
135 |
synchronized (lock) { |
|
136 |
if (provider != null) { |
|
137 |
return provider; |
|
138 |
} |
|
139 |
return (FtpClientProvider) AccessController.doPrivileged( |
|
140 |
new PrivilegedAction<Object>() { |
|
141 |
||
142 |
public Object run() { |
|
143 |
if (loadProviderFromProperty()) { |
|
144 |
return provider; |
|
145 |
} |
|
146 |
if (loadProviderAsService()) { |
|
147 |
return provider; |
|
148 |
} |
|
149 |
provider = new sun.net.ftp.impl.DefaultFtpClientProvider(); |
|
150 |
return provider; |
|
151 |
} |
|
152 |
}); |
|
153 |
} |
|
154 |
} |
|
155 |
} |