查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2014 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    *   http://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  
17  package org.jboss.netty.handler.ssl;
18  
19  import org.apache.tomcat.jni.Library;
20  import org.apache.tomcat.jni.SSL;
21  import org.jboss.netty.logging.InternalLogger;
22  import org.jboss.netty.logging.InternalLoggerFactory;
23  import org.jboss.netty.util.internal.NativeLibraryLoader;
24  
25  /**
26   * Tells if <a href="http://netty.io/wiki/forked-tomcat-native.html">{@code netty-tcnative}</a> and its OpenSSL support
27   * are available.
28   */
29  public final class OpenSsl {
30  
31      private static final InternalLogger logger = InternalLoggerFactory.getInstance(OpenSsl.class);
32      private static final Throwable UNAVAILABILITY_CAUSE;
33  
34      static final String IGNORABLE_ERROR_PREFIX = "error:00000000:";
35  
36      static {
37          Throwable cause = null;
38          try {
39              NativeLibraryLoader.load("netty-tcnative", SSL.class.getClassLoader());
40              Library.initialize("provided");
41              SSL.initialize(null);
42          } catch (Throwable t) {
43              cause = t;
44              logger.debug(
45                      "Failed to load netty-tcnative; " +
46                              OpenSslEngine.class.getSimpleName() + " will be unavailable.", t);
47          }
48          UNAVAILABILITY_CAUSE = cause;
49      }
50  
51      /**
52       * Returns {@code true} if and only if
53       * <a href="http://netty.io/wiki/forked-tomcat-native.html">{@code netty-tcnative}</a> and its OpenSSL support
54       * are available.
55       */
56      public static boolean isAvailable() {
57          return UNAVAILABILITY_CAUSE == null;
58      }
59  
60      /**
61       * Ensure that <a href="http://netty.io/wiki/forked-tomcat-native.html">{@code netty-tcnative}</a> and
62       * its OpenSSL support are available.
63       *
64       * @throws UnsatisfiedLinkError if unavailable
65       */
66      public static void ensureAvailability() {
67          if (UNAVAILABILITY_CAUSE != null) {
68              throw (Error) new UnsatisfiedLinkError(
69                      "failed to load the required native library").initCause(UNAVAILABILITY_CAUSE);
70          }
71      }
72  
73      /**
74       * Returns the cause of unavailability of
75       * <a href="http://netty.io/wiki/forked-tomcat-native.html">{@code netty-tcnative}</a> and its OpenSSL support.
76       *
77       * @return the cause if unavailable. {@code null} if available.
78       */
79      public static Throwable unavailabilityCause() {
80          return UNAVAILABILITY_CAUSE;
81      }
82  
83      private OpenSsl() { }
84  }