1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
package net.sourceforge.jnlp.cache;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import net.sourceforge.jnlp.runtime.JNLPRuntime;
import net.sourceforge.jnlp.util.FileUtils;
import net.sourceforge.jnlp.util.logging.OutputController;
/**
* Handles loading and access of native code loading through a JNLP application or applet.
* Stores native code in a temporary folder.
* Be sure to call cleanupTemporayFolder when finished with the object.
*/
public class NativeLibraryStorage {
private ResourceTracker tracker;
private List<File> nativeSearchDirectories = new ArrayList<File>();
/* Temporary directory to store native jar entries, added to our search path */
private File jarEntryDirectory = null;
public NativeLibraryStorage(ResourceTracker tracker) {
this.tracker = tracker;
}
/**
* Clean up our temporary folder if we created one.
*/
public void cleanupTemporaryFolder() {
if (jarEntryDirectory != null) {
OutputController.getLogger().log("Cleaning up native directory" + jarEntryDirectory.getAbsolutePath());
try {
FileUtils.recursiveDelete(jarEntryDirectory,
new File(System.getProperty("java.io.tmpdir")));
jarEntryDirectory = null;
} catch (IOException e) {
/*
* failed to delete a file in tmpdir, no big deal (as well the VM
* might be shutting down at this point so no much we can do)
*/
}
}
}
/**
* Adds the {@link File} to the search path of this {@link NativeLibraryStorage}
* when trying to find a native library
*/
public void addSearchDirectory(File directory) {
nativeSearchDirectories.add(directory);
}
public List<File> getSearchDirectories() {
return nativeSearchDirectories;
}
/**
* Looks in the search directories for 'fileName',
* returning a path to the found file if it exists.
* Returns null otherwise.
*/
public File findLibrary(String fileName) {
for (File dir : getSearchDirectories()) {
File target = new File(dir, fileName);
if (target.exists())
return target;
}
return null;
}
public static final String[] NATIVE_LIBRARY_EXTENSIONS = { ".so", ".dylib", ".jnilib", ".framework", ".dll" };
/**
* Search for and enable any native code contained in a JAR by copying the
* native files into the filesystem. Called in the security context of the
* classloader.
*/
public void addSearchJar(URL jarLocation) {
OutputController.getLogger().log("Activate native: " + jarLocation);
File localFile = tracker.getCacheFile(jarLocation);
if (localFile == null)
return;
try {
JarFile jarFile = new JarFile(localFile, false);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
if (e.isDirectory()) {
continue;
}
String name = new File(e.getName()).getName();
boolean isLibrary = false;
for (String suffix : NATIVE_LIBRARY_EXTENSIONS) {
if (name.endsWith(suffix)) {
isLibrary = true;
break;
}
}
if (!isLibrary) {
continue;
}
ensureNativeStoreDirectory();
File outFile = new File(jarEntryDirectory, name);
if (!outFile.isFile()) {
FileUtils.createRestrictedFile(outFile, true);
}
CacheUtil.streamCopy(jarFile.getInputStream(e),
new FileOutputStream(outFile));
}
jarFile.close();
} catch (IOException ex) {
OutputController.getLogger().log(ex);
}
}
void ensureNativeStoreDirectory() {
if (jarEntryDirectory == null) {
jarEntryDirectory = createNativeStoreDirectory();
addSearchDirectory(jarEntryDirectory);
}
}
/**
* Create a random base directory to store native code files in.
*/
private static File createNativeStoreDirectory() {
final int rand = (int)((Math.random()*2 - 1) * Integer.MAX_VALUE);
File nativeDir = new File(System.getProperty("java.io.tmpdir")
+ File.separator + "netx-native-"
+ (rand & 0xFFFF));
File parent = nativeDir.getParentFile();
if (!parent.isDirectory() && !parent.mkdirs()) {
return null;
}
try {
FileUtils.createRestrictedDirectory(nativeDir);
return nativeDir;
} catch (IOException e) {
return null;
}
}
}
|