Class SSLContext
- java.lang.Object
-
- javax.net.ssl.SSLContext
-
public class SSLContext extends Object
Instances of this class represent a secure socket protocol implementation which acts as a factory for secure socket factories orSSLEngines. This class is initialized with an optional set of key and trust managers and source of secure random bytes.Every implementation of the Java platform is required to support the following standard
SSLContextprotocol:- TLSv1
- Since:
- 1.4
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static SSLContextgetDefault()Returns the default SSL context.static SSLContextgetInstance(String protocol)Returns aSSLContextobject that implements the specified secure socket protocol.StringgetProtocol()Returns the protocol name of thisSSLContextobject.SSLServerSocketFactorygetServerSocketFactory()Returns aServerSocketFactoryobject for this context.SSLSocketFactorygetSocketFactory()Returns aSocketFactoryobject for this context.voidinit(KeyManager[] km, TrustManager[] tm, SecureRandom random)Initializes this context.
-
-
-
Method Detail
-
getDefault
public static SSLContext getDefault() throws NoSuchAlgorithmException
Returns the default SSL context.The first call of this method triggers the call
SSLContext.getInstance("Default"). If successful, that object is made the default SSL context and returned.The default context is immediately usable and does not require initialization.
- Returns:
- the default SSL context
- Throws:
NoSuchAlgorithmException- if theSSLContext.getInstance()call fails- Since:
- 1.6
-
getInstance
public static SSLContext getInstance(String protocol) throws NoSuchAlgorithmException
Returns aSSLContextobject that implements the specified secure socket protocol.This method traverses the list of registered security Providers, starting with the most preferred Provider. A new SSLContext object encapsulating the SSLContextSpi implementation from the first Provider that supports the specified protocol is returned.
- Parameters:
protocol- the standard name of the requested protocol. See the SSLContext section in the Java Cryptography Architecture Standard Algorithm Name Documentation for information about standard protocol names.- Returns:
- the new
SSLContextobject. - Throws:
NoSuchAlgorithmException- if no Provider supports a SSLContextSpi implementation for the specified protocol.NullPointerException- if protocol is null.
-
getProtocol
public final String getProtocol()
Returns the protocol name of thisSSLContextobject.This is the same name that was specified in one of the
getInstancecalls that created thisSSLContextobject.- Returns:
- the protocol name of this
SSLContextobject.
-
init
public final void init(@Nullable KeyManager[] km, @Nullable TrustManager[] tm, @Nullable SecureRandom random) throws KeyManagementException
Initializes this context. Either of the first two parameters may be null in which case the installed security providers will be searched for the highest priority implementation of the appropriate factory. Likewise, the secure random parameter may be null in which case the default implementation will be used.Only the first instance of a particular key and/or trust manager implementation type in the array is used. (For example, only the first javax.net.ssl.X509KeyManager in the array will be used.)
- Parameters:
km- the sources of authentication keys or nulltm- the sources of peer authentication trust decisions or nullrandom- the source of randomness for this generator or null- Throws:
KeyManagementException- if this operation fails
-
getSocketFactory
public final SSLSocketFactory getSocketFactory()
Returns aSocketFactoryobject for this context.- Returns:
- the
SocketFactoryobject - Throws:
IllegalStateException- if the SSLContextImpl requires initialization and theinit()has not been called
-
getServerSocketFactory
public final SSLServerSocketFactory getServerSocketFactory()
Returns aServerSocketFactoryobject for this context.- Returns:
- the
ServerSocketFactoryobject - Throws:
IllegalStateException- if the SSLContextImpl requires initialization and theinit()has not been called
-
-