1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ovanttasks.ovnative.win32;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.Properties;
25
26
27
28
29
30
31
32
33
34
35
36 class OvNativeLibLoader {
37
38 static boolean loaded = false;
39 static String libName;
40
41 public static String getLibName(boolean appendVersion) {
42 if (libName == null) {
43 Properties props = new Properties();
44 try {
45 props.load(OvNativeLibLoader.class.getResourceAsStream("ov-native.properties"));
46 libName = "ov-native-win32";
47 if (appendVersion) {
48 libName += "-" + props.getProperty("net.sf.ovanttasks.ovnative.libversion");
49 }
50 } catch (IOException e) {
51 throw new Win32Exception(
52 "Unable to get libname fom properties", e);
53 }
54 }
55 return libName;
56
57 }
58
59
60
61
62 static synchronized void loadLibWithoutVersion() {
63 if (loaded) {
64 return;
65 }
66 libName = null;
67 System.loadLibrary(getLibName(false));
68 loaded = true;
69 }
70
71 public static synchronized void loadLib() {
72 if (loaded) {
73 return;
74 }
75 libName = null;
76 try {
77 System.loadLibrary(getLibName(true));
78 loaded = true;
79 } catch (UnsatisfiedLinkError e) {
80 File file = new File(System.getProperty("user.home") + File.separator + ".ov-native" + File.separator + getLibName(true) + ".dll");
81 if (!file.exists()) {
82 try {
83 if (!file.getParentFile().exists()) {
84 file.getParentFile().mkdir();
85 }
86 System.out.println("Install native library " + file.getAbsolutePath());
87 String resName = "/" + getLibName(true) + ".dll";
88 System.out.println("Dll in Jar Name:\"" + resName + "\"");
89 InputStream in = OvNativeLibLoader.class.getResourceAsStream(resName);
90 if (in == null) {
91 throw new Win32Exception("Cant find dll in resource");
92
93 }
94 OutputStream out = new FileOutputStream(file);
95 byte buffer[] = new byte[8192];
96 int read;
97 int written = 0;
98 while ((read = in.read(buffer)) > -1) {
99 written += read;
100 out.write(buffer, 0, read);
101 }
102 out.close();
103 in.close();
104 if (written == 0) {
105 file.delete();
106 loaded = true;
107 } else {
108 loaded = true;
109 }
110 } catch (Exception ex) {
111 file.delete();
112 ex.printStackTrace();
113 throw new Win32Exception("Unable to copy " + file.getName() + " to user.home" + File.separator + ".ov-native.",
114 ex);
115 }
116 }
117 System.load(file.getAbsolutePath());
118 }
119
120 }
121 }