Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / net / ssl / TrustManagerFactory.java
blobc1f2019f24b33251b83cfccfbe3fd0eaeaebd0a2
1 /* TrustManagerFactory.java -- factory for trust managers.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.net.ssl;
41 import gnu.java.security.Engine;
43 import java.lang.reflect.InvocationTargetException;
44 import java.security.AccessController;
45 import java.security.InvalidAlgorithmParameterException;
46 import java.security.KeyStore;
47 import java.security.KeyStoreException;
48 import java.security.NoSuchAlgorithmException;
49 import java.security.NoSuchProviderException;
50 import java.security.PrivilegedAction;
51 import java.security.Provider;
52 import java.security.Security;
54 /**
55 * A factory for creating trust manager objects.
57 public class TrustManagerFactory
60 // Constants and fields.
61 // -------------------------------------------------------------------------
63 /** The service name for trust manager factories. */
64 private static final String TRUST_MANAGER_FACTORY = "TrustManagerFactory";
66 /** The system default trust manager algorithm. */
67 private static final String DEFAULT_ALGORITHM = "JessieX509";
69 /** The underlying engine class. */
70 private final TrustManagerFactorySpi tmfSpi;
72 /** The provider of the engine class. */
73 private final Provider provider;
75 /** The name of this trust manager algorithm. */
76 private final String algorithm;
78 // Constructor.
79 // -------------------------------------------------------------------------
81 /**
82 * Creates a new trust manager factory.
84 * @param tmfSpi The underlying engine class.
85 * @param provider The provider of the engine class.
86 * @param algorithm The trust manager algorithm name.
88 protected TrustManagerFactory(TrustManagerFactorySpi tmfSpi,
89 Provider provider, String algorithm)
91 this.tmfSpi = tmfSpi;
92 this.provider = provider;
93 this.algorithm = algorithm;
96 // Class methods.
97 // -------------------------------------------------------------------------
99 /**
100 * Returns an instance of a trust manager factory for the given algorithm
101 * from the first provider that implements it.
103 * @param algorithm The name of the algorithm to get.
104 * @return The instance of the trust manager factory.
105 * @throws NoSuchAlgorithmException If no provider implements the given
106 * algorithm.
108 public static final TrustManagerFactory getInstance(String algorithm)
109 throws NoSuchAlgorithmException
111 Provider[] provs = Security.getProviders();
112 for (int i = 0; i < provs.length; i++)
116 return getInstance(algorithm, provs[i]);
118 catch (NoSuchAlgorithmException ignore)
122 throw new NoSuchAlgorithmException(algorithm);
126 * Returns an instance of a trust manager factory for the given algorithm
127 * from the named provider.
129 * @param algorithm The name of the algorithm to get.
130 * @param provider The name of the provider to get the instance from.
131 * @return The instance of the trust manager factory.
132 * @throws NoSuchAlgorithmException If the provider does not implement the
133 * given algorithm.
134 * @throws NoSuchProviderException If there is no such named provider.
135 * @throws IllegalArgumentException If the provider argument is null.
137 public static final TrustManagerFactory getInstance(String algorithm,
138 String provider)
139 throws NoSuchAlgorithmException, NoSuchProviderException
141 if (provider == null)
143 throw new IllegalArgumentException();
145 Provider p = Security.getProvider(provider);
146 if (p == null)
148 throw new NoSuchProviderException(provider);
150 return getInstance(algorithm, p);
154 * Returns an instance of a trust manager factory for the given algorithm
155 * from the specified provider.
157 * @param algorithm The name of the algorithm to get.
158 * @param provider The provider to get the instance from.
159 * @return The instance of the trust manager factory.
160 * @throws NoSuchAlgorithmException If the provider does not implement the
161 * given algorithm.
162 * @throws IllegalArgumentException If the provider argument is null.
164 public static final TrustManagerFactory getInstance(String algorithm,
165 Provider provider)
166 throws NoSuchAlgorithmException
168 if (provider == null)
170 throw new IllegalArgumentException();
174 return new TrustManagerFactory((TrustManagerFactorySpi)
175 Engine.getInstance(TRUST_MANAGER_FACTORY, algorithm, provider),
176 provider, algorithm);
178 catch (InvocationTargetException ite)
180 throw new NoSuchAlgorithmException(algorithm);
182 catch (ClassCastException cce)
184 throw new NoSuchAlgorithmException(algorithm);
189 * Returns the default algorithm for trust manager factories. The value
190 * returned is either the value of the security property
191 * "ssl.TrustManagerFactory.algorithm" if it is set, or the value "JessieX509"
192 * if not.
194 * @return The default algorithm name.
195 * @see Security.getProperty(java.lang.String)
197 public static final String getDefaultAlgorithm()
199 String alg = null;
202 alg = (String) AccessController.doPrivileged(
203 new PrivilegedAction()
205 public Object run()
207 return Security.getProperty("ssl.TrustManagerFactory.algorithm");
212 catch (SecurityException se)
215 if (alg == null)
216 alg = DEFAULT_ALGORITHM;
217 return alg;
220 // Instance methods.
221 // -------------------------------------------------------------------------
224 * Returns the name of this trust manager algorithm.
226 * @return The algorithm name.
228 public final String getAlgorithm()
230 return algorithm;
234 * Returns the provider of the underlying implementation.
236 * @return The provider.
238 public final Provider getProvider()
240 return provider;
244 * Returns the trust managers created by this factory.
246 * @return The trust managers.
248 public final TrustManager[] getTrustManagers()
250 return tmfSpi.engineGetTrustManagers();
254 * Initialize this instance with some algorithm-specific parameters.
256 * @param params The parameters.
257 * @throws InvalidAlgorithmParameterException If the supplied parameters
258 * are inappropriate for this instance.
260 public final void init(ManagerFactoryParameters params)
261 throws InvalidAlgorithmParameterException
263 tmfSpi.engineInit(params);
267 * Initialize this instance with a key store. The key store may be null,
268 * in which case a default will be used.
270 * @param store The key store.
271 * @throws KeyStoreException If there is a problem reading from the
272 * key store.
274 public final void init(KeyStore store) throws KeyStoreException
276 tmfSpi.engineInit(store);