smengcl commented on code in PR #4315:
URL: https://github.com/apache/ozone/pull/4315#discussion_r1129945436


##########
hadoop-hdds/rocks-native/src/main/java/org/apache/hadoop/hdds/utils/NativeLibraryLoader.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Class to load Native Libraries.
+ */
+public class NativeLibraryLoader {
+  private static final String OS = System.getProperty("os.name").toLowerCase();
+  private Map<String, Boolean> librariesLoaded;
+  private static volatile NativeLibraryLoader instance;
+
+  public NativeLibraryLoader(final Map<String, Boolean> librariesLoaded) {
+    this.librariesLoaded = librariesLoaded;
+  }
+
+  private static synchronized void initNewInstance() {
+    if (instance == null) {
+      instance = new NativeLibraryLoader(new ConcurrentHashMap<>());
+    }
+  }
+
+  public static NativeLibraryLoader getInstance() {
+    if (instance == null) {
+      initNewInstance();
+    }
+    return instance;
+  }
+
+  public static String getJniLibraryFileName(String libraryName) {
+    return appendLibOsSuffix("lib" + libraryName);
+  }
+
+  public static boolean isMac() {
+    return OS.contains("mac");
+  }
+
+  public static boolean isWindows() {
+    return OS.contains("win");
+  }
+
+  private static String getLibOsSuffix() {
+    if (isMac()) {
+      return ".dylib";
+    } else if (isWindows()) {
+      return ".dll";
+    }
+    return ".so";
+  }
+  private static String appendLibOsSuffix(String libraryFileName) {
+    return libraryFileName + getLibOsSuffix();
+  }
+
+  public boolean isLibraryLoaded(final String libraryName) {
+    return librariesLoaded.getOrDefault(libraryName, false);
+  }
+
+  public synchronized boolean loadLibrary(final String libraryName) {
+    if (isLibraryLoaded(libraryName)) {
+      return true;
+    }
+    boolean loaded = false;
+    try {
+      System.loadLibrary(libraryName);
+      loaded = true;
+    } catch (final UnsatisfiedLinkError ule) {
+
+    }
+    if (!loaded) {
+      try {
+        Optional<File> file = copyResourceFromJarToTemp(libraryName);
+        if (file.isPresent()) {
+          System.load(file.get().getAbsolutePath());
+          loaded = true;
+        }
+
+      } catch (IOException e) {
+
+      }
+
+    }
+    this.librariesLoaded.put(libraryName, loaded);
+    return isLibraryLoaded(libraryName);
+  }
+  private Optional<File> copyResourceFromJarToTemp(final String libraryName)
+          throws IOException {
+    final String libraryFileName = getJniLibraryFileName(libraryName);
+    InputStream is = null;
+    try {
+      is = getClass().getClassLoader().getResourceAsStream(libraryFileName);
+      if (is == null) {
+        return Optional.empty();
+      }
+
+      // create a temporary file to copy the library to
+      final File temp = File.createTempFile(libraryName, getLibOsSuffix());
+      if (!temp.exists()) {
+        return Optional.empty();
+      } else {
+        temp.deleteOnExit();
+      }
+
+      Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
+      return Optional.ofNullable(temp);
+
+    } finally {
+      if (is != null) {
+        is.close();
+      }
+    }
+  }
+}

Review Comment:
   Have we added this newline?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to