View Javadoc
1   /*
2    * Copyright 2021 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import io.netty.util.AbstractConstant;
19  import io.netty.util.ConstantPool;
20  import io.netty.util.internal.ObjectUtil;
21  
22  
23  /**
24   * A {@link SslContextOption} allows to configure a {@link SslContext} in a type-safe
25   * way. Which {@link SslContextOption} is supported depends on the actual implementation
26   * of {@link SslContext} and may depend on the nature of the SSL implementation it belongs
27   * to.
28   *
29   * @param <T>   the type of the value which is valid for the {@link SslContextOption}
30   */
31  public class SslContextOption<T> extends AbstractConstant<SslContextOption<T>> {
32  
33      private static final ConstantPool<SslContextOption<Object>> pool = new ConstantPool<SslContextOption<Object>>() {
34          @Override
35          protected SslContextOption<Object> newConstant(int id, String name) {
36              return new SslContextOption<Object>(id, name);
37          }
38      };
39  
40      /**
41       * Returns the {@link SslContextOption} of the specified name.
42       */
43      @SuppressWarnings("unchecked")
44      public static <T> SslContextOption<T> valueOf(String name) {
45          return (SslContextOption<T>) pool.valueOf(name);
46      }
47  
48      /**
49       * Shortcut of {@link #valueOf(String) valueOf(firstNameComponent.getName() + "#" + secondNameComponent)}.
50       */
51      @SuppressWarnings("unchecked")
52      public static <T> SslContextOption<T> valueOf(Class<?> firstNameComponent, String secondNameComponent) {
53          return (SslContextOption<T>) pool.valueOf(firstNameComponent, secondNameComponent);
54      }
55  
56      /**
57       * Returns {@code true} if a {@link SslContextOption} exists for the given {@code name}.
58       */
59      public static boolean exists(String name) {
60          return pool.exists(name);
61      }
62  
63      /**
64       * Creates a new {@link SslContextOption} with the specified unique {@code name}.
65       */
66      private SslContextOption(int id, String name) {
67          super(id, name);
68      }
69  
70      /**
71       * Should be used by sub-classes.
72       *
73       * @param name the name of the option
74       */
75      protected SslContextOption(String name) {
76          this(pool.nextId(), name);
77      }
78  
79      /**
80       * Validate the value which is set for the {@link SslContextOption}. Sub-classes
81       * may override this for special checks.
82       */
83      public void validate(T value) {
84          ObjectUtil.checkNotNull(value, "value");
85      }
86  }