• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.io;
6 
7 import software.amazon.awssdk.crt.CrtResource;
8 import software.amazon.awssdk.crt.CrtRuntimeException;
9 
10 /**
11  * This class wraps the aws_tls_context from aws-c-io to provide
12  * access to TLS configuration contexts in the AWS Common Runtime.
13  */
14 public class TlsContext extends CrtResource {
15 
16     /**
17      * Creates a new Client TlsContext. There are significant native resources consumed to create a TlsContext, so most
18      * applications will only need to create one and re-use it for all connections.
19      * @param options A set of options for this context
20      * @throws CrtRuntimeException If the provided options are malformed or the system is unable
21      * to allocate space for a native tls context
22      */
TlsContext(TlsContextOptions options)23     public TlsContext(TlsContextOptions options) throws CrtRuntimeException {
24         acquireNativeHandle(tlsContextNew(options.getNativeHandle()));
25     }
26 
27     /**
28      * Creates a new Client TlsContext. There are significant native resources consumed to create a TlsContext, so most
29      * applications will only need to create one and re-use it for all connections.
30      */
TlsContext()31     public TlsContext() throws CrtRuntimeException  {
32         try (TlsContextOptions options = TlsContextOptions.createDefaultClient()) {
33             acquireNativeHandle(tlsContextNew(options.getNativeHandle()));
34         }
35     }
36 
37     /**
38      * Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
39      * Resources that wait are responsible for calling releaseReferences() manually.
40      */
41     @Override
canReleaseReferencesImmediately()42     protected boolean canReleaseReferencesImmediately() { return true; }
43 
44     /**
45      * Frees all native resources associated with the context. This object is unusable after close is called.
46      */
47     @Override
releaseNativeHandle()48     protected void releaseNativeHandle() {
49         if (!isNull()) {
50             tlsContextDestroy(getNativeHandle());
51         }
52     }
53 
tlsContextNew(long options)54     protected static native long tlsContextNew(long options) throws CrtRuntimeException;
55 
tlsContextDestroy(long elg)56     private static native void tlsContextDestroy(long elg);
57 };
58