Skip to content

Commit d1176c1

Browse files
committed
Changed so that dylib is automatically extracted from jar at runtime so you no longer need to bundle the libjcocoa.dylib.
1 parent 6ffdb54 commit d1176c1

File tree

16 files changed

+1937
-78
lines changed

16 files changed

+1937
-78
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/java/nbproject/private/
2+
/java/build/
3+
/java/dist/
4+
/samples/BringToFrontSample/nbproject/private/
5+
/samples/BringToFrontSample/build/
6+
.DS_Store
7+
private

java/nbproject/build-impl.xml

Lines changed: 83 additions & 74 deletions
Large diffs are not rendered by default.

java/nbproject/genfiles.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ [email protected]
44
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
55
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
66
nbproject/build-impl.xml.data.CRC32=bdde2110
7-
nbproject/build-impl.xml.script.CRC32=33aeb395
8-
nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46
7+
nbproject/build-impl.xml.script.CRC32=fae8a97c
8+
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48

java/nbproject/private/private.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ do.depend=false
33
do.jar=true
44
javac.debug=true
55
javadoc.preview=true
6-
user.properties.file=/Users/shannah/Library/Application Support/NetBeans/7.3/build.properties
6+
user.properties.file=/Users/shannah/cn1_files/.netbeans-jclient/build.properties
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ca.weblite.objc;
7+
8+
/**
9+
*
10+
* @author shannah
11+
*/
12+
public interface Constants {
13+
public static final String VERSION="1.0";
14+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package ca.weblite.objc;
7+
8+
import java.io.File;
9+
import java.io.FileOutputStream;
10+
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.OutputStream;
13+
14+
/**
15+
*
16+
* @author shannah
17+
*/
18+
public class NativeLibLoader implements Constants {
19+
private boolean loaded;
20+
21+
private static String getLibName(boolean withPrefixAndExtension) {
22+
if (withPrefixAndExtension) {
23+
return "libjcocoa-"+VERSION+".dylib";
24+
} else {
25+
return "jcocoa-"+VERSION;
26+
}
27+
}
28+
29+
30+
31+
public void load() throws UnsatisfiedLinkError {
32+
if (loaded) {
33+
return;
34+
}
35+
// Try for versioned name first
36+
try {
37+
System.loadLibrary(getLibName(false));
38+
loaded = true;
39+
} catch (UnsatisfiedLinkError err) {
40+
41+
}
42+
if (loaded) {
43+
return;
44+
}
45+
// Try for unversioned name
46+
// For backward compatibility of existing deployments, this needs to
47+
// occur before we try to extract the lib from the jar file.
48+
try {
49+
System.loadLibrary("libjcocoa");
50+
loaded = true;
51+
} catch (UnsatisfiedLinkError err) {
52+
53+
}
54+
55+
if (loaded) {
56+
return;
57+
}
58+
59+
try {
60+
extract();
61+
System.load(getExtractLocation().getAbsolutePath());
62+
} catch (IOException ex) {
63+
throw new UnsatisfiedLinkError("libjcocoa.dylib could not be found");
64+
}
65+
66+
}
67+
68+
private File getExtractLocation() {
69+
return new File(new File(System.getProperty("java.io.tmpdir")), getLibName(true));
70+
}
71+
72+
private File getJarFile() {
73+
return new File(NativeLibLoader.class.getProtectionDomain().getCodeSource().getLocation().getPath());
74+
}
75+
76+
private long getJarMTime() {
77+
File jarFile = getJarFile();
78+
if (!jarFile.exists()) {
79+
return System.currentTimeMillis();
80+
}
81+
return jarFile.lastModified();
82+
}
83+
84+
public void extract() throws IOException {
85+
File extractLocation = getExtractLocation();
86+
if (!extractLocation.exists() || extractLocation.lastModified() < getJarMTime()) {
87+
copyResourceToFile(NativeLibLoader.class, "libjcocoa.dylib", extractLocation);
88+
}
89+
}
90+
91+
92+
private static void copyResourceToFile(Class context, String resource, File dest) throws IOException {
93+
InputStream is=null;
94+
try {
95+
is = context.getResourceAsStream(resource);
96+
OutputStream os=null;
97+
try {
98+
os = new FileOutputStream(dest);
99+
copy(is, os);
100+
} finally {
101+
if (os != null) {
102+
try {
103+
os.close();
104+
} catch (Throwable t){}
105+
}
106+
}
107+
} finally {
108+
if (is != null) {
109+
try {
110+
is.close();
111+
} catch (Throwable t){}
112+
}
113+
}
114+
}
115+
116+
private static final int BUFFER_SIZE = 8192;
117+
118+
/**
119+
* Reads all bytes from an input stream and writes them to an output stream.
120+
*/
121+
private static long copy(InputStream source, OutputStream sink)
122+
throws IOException
123+
{
124+
long nread = 0L;
125+
byte[] buf = new byte[BUFFER_SIZE];
126+
int n;
127+
while ((n = source.read(buf)) > 0) {
128+
sink.write(buf, 0, n);
129+
nread += n;
130+
}
131+
return nread;
132+
}
133+
}

java/src/ca/weblite/objc/RuntimeUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class RuntimeUtils {
5656
public static boolean loaded = false;
5757
static {
5858
try {
59-
System.loadLibrary("jcocoa");
59+
new NativeLibLoader().load();
6060
loaded = true;
6161
} catch (UnsatisfiedLinkError err){
6262
err.printStackTrace(System.err);
21.5 KB
Binary file not shown.

native/.DS_Store

0 Bytes
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- You may freely edit this file. See commented blocks below for -->
3+
<!-- some examples of how to customize the build. -->
4+
<!-- (If you delete it and reopen the project it will be recreated.) -->
5+
<!-- By default, only the Clean and Build commands use this build script. -->
6+
<!-- Commands such as Run, Debug, and Test only use this build script if -->
7+
<!-- the Compile on Save feature is turned off for the project. -->
8+
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
9+
<!-- in the project's Project Properties dialog box.-->
10+
<project name="BringToFrontSample" default="default" basedir=".">
11+
<description>Builds, tests, and runs the project BringToFrontSample.</description>
12+
<import file="nbproject/build-impl.xml"/>
13+
<!--
14+
15+
There exist several targets which are by default empty and which can be
16+
used for execution of your tasks. These targets are usually executed
17+
before and after some main targets. They are:
18+
19+
-pre-init: called before initialization of project properties
20+
-post-init: called after initialization of project properties
21+
-pre-compile: called before javac compilation
22+
-post-compile: called after javac compilation
23+
-pre-compile-single: called before javac compilation of single file
24+
-post-compile-single: called after javac compilation of single file
25+
-pre-compile-test: called before javac compilation of JUnit tests
26+
-post-compile-test: called after javac compilation of JUnit tests
27+
-pre-compile-test-single: called before javac compilation of single JUnit test
28+
-post-compile-test-single: called after javac compilation of single JUunit test
29+
-pre-jar: called before JAR building
30+
-post-jar: called after JAR building
31+
-post-clean: called after cleaning build products
32+
33+
(Targets beginning with '-' are not intended to be called on their own.)
34+
35+
Example of inserting an obfuscator after compilation could look like this:
36+
37+
<target name="-post-compile">
38+
<obfuscate>
39+
<fileset dir="${build.classes.dir}"/>
40+
</obfuscate>
41+
</target>
42+
43+
For list of available properties check the imported
44+
nbproject/build-impl.xml file.
45+
46+
47+
Another way to customize the build is by overriding existing main targets.
48+
The targets of interest are:
49+
50+
-init-macrodef-javac: defines macro for javac compilation
51+
-init-macrodef-junit: defines macro for junit execution
52+
-init-macrodef-debug: defines macro for class debugging
53+
-init-macrodef-java: defines macro for class execution
54+
-do-jar: JAR building
55+
run: execution of project
56+
-javadoc-build: Javadoc generation
57+
test-report: JUnit report generation
58+
59+
An example of overriding the target for project execution could look like this:
60+
61+
<target name="run" depends="BringToFrontSample-impl.jar">
62+
<exec dir="bin" executable="launcher.exe">
63+
<arg file="${dist.jar}"/>
64+
</exec>
65+
</target>
66+
67+
Notice that the overridden target depends on the jar target and not only on
68+
the compile target as the regular run target does. Again, for a list of available
69+
properties which you can use, check the target you are overriding in the
70+
nbproject/build-impl.xml file.
71+
72+
-->
73+
</project>

0 commit comments

Comments
 (0)