Sleuth Kit Java Bindings (JNI)  4.2
Java bindings for using The Sleuth Kit
LibraryUtils.java
Go to the documentation of this file.
1 /*
2  * Sleuth Kit Data Model
3  *
4  * Copyright 2013 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.datamodel;
20 
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.URL;
26 
33 public class LibraryUtils {
34 
35  public static final String[] EXTS = new String[]{".so", ".dylib", ".dll", ".jnilib"}; //NON-NLS
36 
40  public enum Lib {
41 
42  MSVCP("msvcp100", ""), //NON-NLS
43  MSVCR("msvcr100", ""), //NON-NLS
44  ZLIB("zlib", "z"), //NON-NLS
45  LIBEWF("libewf", "ewf"), //NON-NLS
46  TSK_JNI("libtsk_jni", "tsk_jni"); //NON-NLS
47 
48  private final String name;
49  private final String unixName;
50 
51  Lib(String name, String unixName) {
52  this.name = name;
53  this.unixName = unixName;
54  }
55 
56  public String getLibName() {
57  return this.name;
58  }
59 
60  public String getUnixName() {
61  return this.unixName;
62  }
63  }
64 
70  public static boolean loadSleuthkitJNI() {
72  if (!loaded) {
73  System.out.println("SleuthkitJNI: failed to load " + Lib.TSK_JNI.getLibName()); //NON-NLS
74  } else {
75  System.out.println("SleuthkitJNI: loaded " + Lib.TSK_JNI.getLibName()); //NON-NLS
76  }
77  return loaded;
78  }
79 
85  private static String getPlatform() {
86  String os = System.getProperty("os.name").toLowerCase();
87  if (LibraryUtils.isWindows()) {
88  os = "win"; //NON-NLS
89  } else if (LibraryUtils.isMac()) {
90  os = "mac"; //NON-NLS
91  } else if (LibraryUtils.isLinux()) {
92  os = "linux"; //NON-NLS
93  }
94  // os.arch represents the architecture of the JVM, not the os
95  String arch = System.getProperty("os.arch");
96  return arch.toLowerCase() + "/" + os.toLowerCase();
97  }
98 
104  private static boolean isWindows() {
105  return System.getProperty("os.name").toLowerCase().contains("windows"); //NON-NLS
106  }
107 
113  private static boolean isMac() {
114  return System.getProperty("os.name").toLowerCase().contains("mac"); //NON-NLS
115  }
116 
122  private static boolean isLinux() {
123  return System.getProperty("os.name").equals("Linux"); //NON-NLS
124  }
125 
132  private static boolean loadNativeLibFromTskJar(Lib library) {
133  String libName = library.getLibName();
134 
135  // find the library in the jar file
136  StringBuilder pathInJarBase = new StringBuilder();
137  pathInJarBase.append("/NATIVELIBS/"); //NON-NLS
138  pathInJarBase.append(getPlatform());
139  pathInJarBase.append("/");
140  pathInJarBase.append(libName);
141 
142  URL urlInJar = null;
143  String libExt = null;
144  for (String ext : EXTS) {
145  urlInJar = SleuthkitJNI.class.getResource(pathInJarBase.toString() + ext);
146  if (urlInJar != null) {
147  libExt = ext;
148  break;
149  }
150  }
151 
152  if (urlInJar == null) {
153  System.out.println("Library not found in jar (" + libName + ")"); //NON-NLS
154  return false;
155  }
156 
157  // copy library to temp folder and load it
158  try {
159  java.io.File tempLibFile = new java.io.File(System.getProperty("java.io.tmpdir") + java.io.File.separator + libName + libExt); //NON-NLS
160  System.out.println("Temp Folder for Libraries: " + tempLibFile.getParent()); //NON-NLS
161 
162  // cycle through the libraries and delete them.
163  // we used to copy dlls into here.
164  // delete any than may still exist from previous installations.
165  // Dec 2013
166  for (Lib l : Lib.values()) {
167  String ext = getExtByPlatform();
168  // try the windows version
169  java.io.File f = new java.io.File(l.getLibName() + ext);
170  //System.out.println(f.getName());
171  if (f.exists()) {
172  f.delete();
173  } else {
174  // try the unix version
175  java.io.File fUnix = new java.io.File(l.getUnixName() + ext);
176  //System.out.println(fUnix.getName());
177  if (fUnix.exists()) {
178  fUnix.delete();
179  }
180  }
181  }
182 
183  // Delete old file
184  if (tempLibFile.exists()) {
185  if (tempLibFile.delete() == false) {
186  System.out.println("Error deleting old native library. Is the app already running? (" + tempLibFile.toString() + ")"); //NON-NLS
187  return false;
188  }
189  }
190 
191  // copy it
192  InputStream in = urlInJar.openStream();
193  OutputStream out = new FileOutputStream(tempLibFile);
194 
195  byte[] buffer = new byte[1024];
196  int length;
197  while ((length = in.read(buffer)) > 0) {
198  out.write(buffer, 0, length);
199  }
200  in.close();
201  out.close();
202 
203  // load it
204  System.load(tempLibFile.getAbsolutePath());
205  } catch (IOException e) {
206  // Loading failed.
207  System.out.println("Error loading library: " + e.getMessage()); //NON-NLS
208  return false;
209  }
210  return true;
211  }
212 
213  private static String getExtByPlatform() {
214  if (isWindows()) {
215  return ".dll"; //NON-NLS
216  } else if (isMac()) {
217  return ".dylib"; //NON-NLS
218  } else {
219  return ".so"; //NON-NLS
220  }
221  }
222 }
Lib(String name, String unixName)
static boolean loadNativeLibFromTskJar(Lib library)

Copyright © 2011-2015 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.