]> gerrit.simantics Code Review - simantics/3d.git/blob - vtk/src/vtk/vtkNativeLibrary.java
5fd59cfe5ea09992f519d2f8494436007a96331e
[simantics/3d.git] / vtk / src / vtk / vtkNativeLibrary.java
1 package vtk;
2
3 import 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  */
17 public enum vtkNativeLibrary {
18
19     COMMON("vtkCommonJava"), //
20     FILTERING("vtkFilteringJava"), //
21     GEOVIS("vtkGeovisJava"), //
22     GRAPHICS("vtkGraphicsJava"), //
23     HYBRID("vtkHybridJava"), //
24     IMAGING("vtkImagingJava"), //
25     INFOVIS("vtkInfovisJava"), //
26     IO("vtkIOJava"), //
27     RENDERING("vtkRenderingJava"), //
28     VIEWS("vtkViewsJava"), //
29     VOLUME_RENDERING("vtkVolumeRenderingJava"), //
30     WIDGETS("vtkWidgetsJava"), //
31     CHARTS("vtkChartsJava");
32
33     /**
34      * Try to load all library
35      *
36      * @return true if all library have been successfully loaded
37      */
38     public static boolean LoadAllNativeLibraries() {
39         boolean isEveryThingLoaded = true;
40         for (vtkNativeLibrary lib : values()) {
41             try {
42                 lib.LoadLibrary();
43             } catch (UnsatisfiedLinkError e) {
44                 isEveryThingLoaded = false;
45             }
46         }
47
48         return isEveryThingLoaded;
49     }
50
51     /**
52      * Load the set of given library and trows runtime exception if any given
53      * library failed in the loading process
54      *
55      * @param nativeLibraries
56      */
57     public static void LoadNativeLibraries(vtkNativeLibrary... nativeLibraries) {
58         for (vtkNativeLibrary lib : nativeLibraries) {
59             lib.LoadLibrary();
60         }
61     }
62
63     /**
64      * Disable the pop-in vtkErrorWindow by writing the error to a log file.
65      * If the provided logFile is null the default "vtkError.txt" file will be
66      * used.
67      *
68      * @param logFile
69      */
70     public static void DisableOutputWindow(File logFile) {
71         if(logFile == null) {
72            logFile = new File("vtkError.txt");
73         }
74         vtkFileOutputWindow outputError = new vtkFileOutputWindow();
75         outputError.SetFileName(logFile.getAbsolutePath());
76         outputError.SetInstance(outputError);
77     }
78
79     private vtkNativeLibrary(String nativeLibraryName) {
80         this.nativeLibraryName = nativeLibraryName;
81         this.loaded = false;
82     }
83
84     /**
85      * Load the library and throws runtime exception if the library failed in
86      * the loading process
87      */
88     public void LoadLibrary() throws UnsatisfiedLinkError {
89         if (!loaded) {
90             if (System.getProperty("vtk.lib.dir") != null) {
91                 File dir = new File(System.getProperty("vtk.lib.dir"));
92                 patchJavaLibraryPath(dir.getAbsolutePath());
93                 File libPath = new File(dir, System.mapLibraryName(nativeLibraryName));
94                 if (libPath.exists()) {
95                     try {
96                         Runtime.getRuntime().load(libPath.getAbsolutePath());
97                         loaded = true;
98                         return;
99                     } catch (UnsatisfiedLinkError e) {
100                     }
101                 }
102             }
103             System.loadLibrary(nativeLibraryName);
104         }
105         loaded = true;
106     }
107
108     /**
109      * @return true if the library has already been succefuly loaded
110      */
111     public boolean IsLoaded() {
112         return loaded;
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 }