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 public static class Helper { 017 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 018 019 public static boolean getExtractOnStaticLoad() { 020 return extractOnStaticLoad.get(); 021 } 022 023 public static void setExtractOnStaticLoad(boolean load) { 024 extractOnStaticLoad.set(load); 025 } 026 } 027 028 static { 029 if (Helper.getExtractOnStaticLoad()) { 030 try { 031 loader = 032 new RuntimeLoader<>( 033 "wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); 034 loader.loadLibrary(); 035 } catch (IOException ex) { 036 ex.printStackTrace(); 037 System.exit(1); 038 } 039 libraryLoaded = true; 040 } 041 } 042 043 /** 044 * Force load the library. 045 * 046 * @throws IOException if the library load failed 047 */ 048 public static synchronized void forceLoad() throws IOException { 049 if (libraryLoaded) { 050 return; 051 } 052 loader = 053 new RuntimeLoader<>( 054 "wpiHaljni", RuntimeLoader.getDefaultExtractionRoot(), JNIWrapper.class); 055 loader.loadLibrary(); 056 libraryLoaded = true; 057 } 058 059 public static void suppressUnused(Object object) {} 060}