Mercurial > cgi-bin > hgweb.cgi > JpegWasher
annotate src/name/blackcap/exifwasher/exiv2/Initialize.kt @ 57:94cd2d486adc
Tabs to spaces (argh).
author | davidb |
---|---|
date | Thu, 07 May 2020 17:15:43 -0700 |
parents | cd2ca4727b7f |
children |
rev | line source |
---|---|
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
1 /* |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
2 * Logic common to initializing all external (JNI) classes goes here. |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
3 */ |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
4 package name.blackcap.exifwasher.exiv2 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
5 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
6 import java.io.File |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
7 import java.io.FileOutputStream |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
8 import java.io.IOException |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
9 import java.util.jar.JarEntry |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
10 import java.util.jar.JarFile |
12
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
11 import java.util.logging.Level |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
12 import java.util.logging.Logger |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
13 import javax.swing.* |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
14 import name.blackcap.exifwasher.Application |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
15 import name.blackcap.exifwasher.LF_DIR |
12
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
16 import name.blackcap.exifwasher.LOGGER |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
17 import name.blackcap.exifwasher.OS |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
18 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 object Initialize { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
20 private var initialized = false |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
21 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
22 public fun libraries() { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 /* no-op if already initialized */ |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
24 if (initialized) { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
25 return |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
26 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
27 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
28 /* use the appropriate binary for the system we're on */ |
22 | 29 val (subdir, pfx, ext) = when(OS.type) { |
30 OS.UNIX -> Triple("linux", "lib", ".so") | |
31 OS.MAC -> Triple("mac", "lib", ".dylib") | |
32 OS.WINDOWS -> Triple("windows", "", ".dll") | |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
33 OS.OTHER -> throw RuntimeException("unsupported OS!") |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
34 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
35 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
36 /* extract to scratch files, if needed, then load */ |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
37 val klass = Initialize::class.java |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 var myJar = JarFile(File(klass.getProtectionDomain().getCodeSource().getLocation().toURI())) |
22 | 39 for (base in arrayOf("exiv2", "jni")) { |
40 val eBase = "${pfx}${base}${ext}" | |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 val sPath = "name/blackcap/exifwasher/binaries/${subdir}/${eBase}" |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
42 val source = myJar.getJarEntry(sPath) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
43 if (source == null) { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
44 die("${sPath} not found in jar") |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
45 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 val target = File(LF_DIR, eBase) |
1
42277ce58ace
Improve exception handling; always use absolute paths on libs.
David Barts <n5jrn@me.com>
parents:
0
diff
changeset
|
47 val tPath = target.absolutePath |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
48 try { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 if (!target.exists() || target.lastModified() < source.getTime()) { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 myJar.getInputStream(source).use { sfp -> |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
51 FileOutputStream(target).use { tfp -> |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
52 sfp.copyTo(tfp) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
55 target.setExecutable(true) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
56 target.setLastModified(source.getTime()) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
57 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 } catch (e: IOException) { |
12
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
59 LOGGER.log(Level.SEVERE, "unable to create ${tPath}", e) |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
60 val message = e.message ?: "I/O error" |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 die("unable to create ${tPath}: ${message}") |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
63 try { |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
64 System.load(tPath) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
65 } catch (e: UnsatisfiedLinkError) { |
12
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
66 LOGGER.log(Level.SEVERE, "unable to link ${tPath}", e) |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
67 val message = e.message ?: "unsatisfied link" |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
68 die("unable to link ${tPath}: ${message}") |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
69 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
70 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
71 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
72 initialized = true |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
73 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
74 |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
75 private fun die(message: String) { |
12
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
76 JOptionPane.showMessageDialog(Application.mainFrame, |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
77 "A fatal error occurred in initialization:\n${message}", |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
78 "Error", JOptionPane.ERROR_MESSAGE) |
9ac6136c710c
Replace the temporary error handling in Initialize.kt.
David Barts <n5jrn@me.com>
parents:
2
diff
changeset
|
79 LOGGER.log(Level.SEVERE, "aborted due to fatal error") |
0
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
80 System.exit(1) |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
81 } |
db63d01a23c6
JNI calls and test case (finally!) seem to work.
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
82 } |