View Javadoc
1   /*
2    * Copyright 2016 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.util.internal;
17  
18  /**
19   * A Utility to Call the {@link System#load(String)} or {@link System#loadLibrary(String)}.
20   * Because the {@link System#load(String)} and {@link System#loadLibrary(String)} are both
21   * CallerSensitive, it will load the native library into its caller's {@link ClassLoader}.
22   * In OSGi environment, we need this helper to delegate the calling to {@link System#load(String)}
23   * and it should be as simple as possible. It will be injected into the native library's
24   * ClassLoader when it is undefined. And therefore, when the defined new helper is invoked,
25   * the native library would be loaded into the native library's ClassLoader, not the
26   * caller's ClassLoader.
27   */
28  final class NativeLibraryUtil {
29      /**
30       * Delegate the calling to {@link System#load(String)} or {@link System#loadLibrary(String)}.
31       * @param libName - The native library path or name
32       * @param absolute - Whether the native library will be loaded by path or by name
33       */
34      public static void loadLibrary(String libName, boolean absolute) {
35          if (absolute) {
36              System.load(libName);
37          } else {
38              System.loadLibrary(libName);
39          }
40      }
41  
42      private NativeLibraryUtil() {
43          // Utility
44      }
45  }