1package vtk; 2 3import java.io.File; 4 5/** 6 * Enum used to load native library more easily. If you don't want to set the 7 * specific environment variable you can provide the path of the directory that 8 * contains the VTK library through a Java Property. Like in the following 9 * command line: 10 * 11 * > java -cp vtk.jar -Dvtk.lib.dir=.../vtk-libs vtk.sample.SimpleVTK 12 * 13 * The directory .../vtk-libs must contain the so/dll/dylib + the jnilib files 14 * 15 * @author sebastien jourdain - sebastien.jourdain@kitware.com 16 */ 17public enum vtkNativeLibrary { 18 19@VTK_JAVA_NATIVE_LIBRARIES@ 20 21 /** 22 * Try to load all library 23 * 24 * @return true if all library have been successfully loaded 25 */ 26 public static boolean LoadAllNativeLibraries() { 27 boolean isEveryThingLoaded = true; 28 for (vtkNativeLibrary lib : values()) { 29 try { 30 if(lib.IsBuilt()) { 31 lib.LoadLibrary(); 32 } 33 } catch (UnsatisfiedLinkError e) { 34 isEveryThingLoaded = false; 35 e.printStackTrace(); 36 } 37 } 38 39 return isEveryThingLoaded; 40 } 41 42 /** 43 * Load the set of given library and trows runtime exception if any given 44 * library failed in the loading process 45 * 46 * @param nativeLibraries 47 */ 48 public static void LoadNativeLibraries(vtkNativeLibrary... nativeLibraries) { 49 for (vtkNativeLibrary lib : nativeLibraries) { 50 lib.LoadLibrary(); 51 } 52 } 53 54 /** 55 * Disable the pop-in vtkErrorWindow by writing the error to a log file. 56 * If the provided logFile is null the default "vtkError.txt" file will be 57 * used. 58 * 59 * @param logFile 60 */ 61 public static void DisableOutputWindow(File logFile) { 62 if(logFile == null) { 63 logFile = new File("vtkError.txt"); 64 } 65 vtkFileOutputWindow outputError = new vtkFileOutputWindow(); 66 outputError.SetFileName(logFile.getAbsolutePath()); 67 outputError.SetInstance(outputError); 68 } 69 70 private vtkNativeLibrary(String nativeLibraryName, boolean built) { 71 this.nativeLibraryName = nativeLibraryName; 72 this.loaded = false; 73 this.built = built; 74 } 75 76 /** 77 * Load the library and throws runtime exception if the library failed in 78 * the loading process 79 */ 80 public void LoadLibrary() throws UnsatisfiedLinkError { 81 if (!loaded) { 82 if (System.getProperty("vtk.lib.dir") != null) { 83 File dir = new File(System.getProperty("vtk.lib.dir")); 84 patchJavaLibraryPath(dir.getAbsolutePath()); 85 File libPath = new File(dir, System.mapLibraryName(nativeLibraryName)); 86 if (libPath.exists()) { 87 try { 88 Runtime.getRuntime().load(libPath.getAbsolutePath()); 89 loaded = true; 90 return; 91 } catch (UnsatisfiedLinkError e) { 92 e.printStackTrace(); 93 } 94 } 95 } 96 System.loadLibrary(nativeLibraryName); 97 } 98 loaded = true; 99 } 100 101 /** 102 * @return true if the library has already been successfully loaded 103 */ 104 public boolean IsLoaded() { 105 return loaded; 106 } 107 108 /** 109 * @return true if the module was enabled and therefore build 110 */ 111 public boolean IsBuilt() { 112 return built; 113 } 114 115 /** 116 * @return the library name 117 */ 118 public String GetLibraryName() { 119 return nativeLibraryName; 120 } 121 122 private static void patchJavaLibraryPath(String vtkLibDir) { 123 if (vtkLibDir != null) { 124 String path_separator = System.getProperty("path.separator"); 125 String s = System.getProperty("java.library.path"); 126 if (!s.contains(vtkLibDir)) { 127 s = s + path_separator + vtkLibDir; 128 System.setProperty("java.library.path", s); 129 } 130 } 131 } 132 133 private String nativeLibraryName; 134 private boolean loaded; 135 private boolean built; 136} 137