001// Copyright (c) FIRST and other WPILib contributors. 002// Open Source Software; you can modify and/or share it under the terms of 003// the WPILib BSD license file in the root directory of this project. 004 005package edu.wpi.first.hal; 006 007import edu.wpi.first.util.RuntimeLoader; 008import java.io.IOException; 009import java.util.concurrent.atomic.AtomicBoolean; 010 011/** Base class for all JNI wrappers. */ 012public class JNIWrapper { 013 static boolean libraryLoaded = false; 014 static RuntimeLoader<JNIWrapper> loader = null; 015 016 /** Sets whether JNI should be loaded in the static block. */ 017 public static class Helper { 018 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 019 020 /** 021 * Returns true if the JNI should be loaded in the static block. 022 * 023 * @return True if the JNI should be loaded in the static block. 024 */ 025 public static boolean getExtractOnStaticLoad() { 026 return extractOnStaticLoad.get(); 027 } 028 029 /** 030 * Sets whether the JNI should be loaded in the static block. 031 * 032 * @param load Whether the JNI should be loaded in the static block. 033 */ 034 public static void setExtractOnStaticLoad(boolean load) { 035 extractOnStaticLoad.set(load); 036 } 037 038 /** Utility class. */ 039 private Helper() {} 040 } 041 042 static { 043 if (Helper.getExtractOnStaticLoad()) { 044 try { 045 loader = 046 new RuntimeLoader<>( 047 "wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); 048 loader.loadLibrary(); 049 } catch (IOException ex) { 050 ex.printStackTrace(); 051 System.exit(1); 052 } 053 libraryLoaded = true; 054 } 055 } 056 057 /** 058 * Force load the library. 059 * 060 * @throws IOException if the library load failed 061 */ 062 public static synchronized void forceLoad() throws IOException { 063 if (libraryLoaded) { 064 return; 065 } 066 loader = 067 new RuntimeLoader<>( 068 "wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); 069 loader.loadLibrary(); 070 libraryLoaded = true; 071 } 072 073 public static void suppressUnused(Object object) {} 074 075 /** Utility class. */ 076 public JNIWrapper() {} 077}