Skip to content

Commit 2184faf

Browse files
authored
Merge pull request shannah#35 from Marcono1234/marcono1234/fixes
Various fixes and improvements
2 parents 9708eaa + 6da9170 commit 2184faf

25 files changed

+449
-834
lines changed

pom.xml

Lines changed: 24 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040

4141
<properties>
4242
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
43-
<maven.compiler.target>11</maven.compiler.target>
44-
<maven.compiler.source>11</maven.compiler.source>
43+
<maven.compiler.release>11</maven.compiler.release>
4544
<xcodeScheme>Debug</xcodeScheme>
4645
</properties>
4746

@@ -65,7 +64,7 @@
6564
<licenses>
6665
<license>
6766
<name>Apache License, Version 2.0</name>
68-
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
67+
<url>https://www.apache.org/licenses/LICENSE-2.0.txt</url>
6968
<distribution>repo</distribution>
7069
</license>
7170
</licenses>
@@ -74,7 +73,7 @@
7473

7574
<organization>
7675
<name>Web Lite Solutions Corp.</name>
77-
<url>http://solutions.weblite.ca</url>
76+
<url>https://solutions.weblite.ca/</url>
7877
</organization>
7978

8079
<issueManagement>
@@ -91,29 +90,6 @@
9190
<url>https://github.com/shannah/Java-Objective-C-Bridge</url>
9291
</scm>
9392

94-
<repositories>
95-
<repository>
96-
<id>mvnrepository</id>
97-
<name>mvnrepository</name>
98-
<url>https://www.mvnrepository.com</url>
99-
<snapshots>
100-
<enabled>false</enabled>
101-
</snapshots>
102-
</repository>
103-
</repositories>
104-
105-
<pluginRepositories>
106-
<!-- eclipse lifecycle plugin is only available here -->
107-
<pluginRepository>
108-
<id>mvnrepository</id>
109-
<name>mvnrepository</name>
110-
<url>https://www.mvnrepository.com</url>
111-
<snapshots>
112-
<enabled>false</enabled>
113-
</snapshots>
114-
</pluginRepository>
115-
</pluginRepositories>
116-
11793
<distributionManagement>
11894
<snapshotRepository>
11995
<id>ossrh</id>
@@ -136,16 +112,16 @@
136112
<extension>
137113
<groupId>org.apache.maven.wagon</groupId>
138114
<artifactId>wagon-ssh</artifactId>
139-
<version>2.4</version>
115+
<version>3.4.2</version>
140116
</extension>
141117
</extensions>
142-
<plugins>
143118

119+
<plugins>
144120
<!-- native build -->
145121
<plugin>
146122
<groupId>org.codehaus.mojo</groupId>
147123
<artifactId>exec-maven-plugin</artifactId>
148-
<version>1.6.0</version>
124+
<version>3.0.0</version>
149125
<executions>
150126
<execution>
151127
<id>xcodebuild</id>
@@ -162,41 +138,17 @@
162138
<argument>libjcocoa</argument>
163139
<argument>-configuration</argument>
164140
<argument>${xcodeScheme}</argument>
165-
<argument>CONFIGURATION_BUILD_DIR=${basedir}/target/native/</argument>
141+
<argument>CONFIGURATION_BUILD_DIR=${project.build.outputDirectory}</argument>
166142
<argument>build</argument>
167143
</arguments>
168144
</configuration>
169145
</plugin>
170146

171-
<!-- copy native libs -->
172-
<plugin>
173-
<artifactId>maven-resources-plugin</artifactId>
174-
<version>3.1.0</version>
175-
<executions>
176-
<execution>
177-
<id>copy-native-libs</id>
178-
<phase>process-resources</phase>
179-
<goals>
180-
<goal>copy-resources</goal>
181-
</goals>
182-
<configuration>
183-
<resources>
184-
<resource>
185-
<directory>${basedir}/target/native/</directory>
186-
<filtering>false</filtering>
187-
</resource>
188-
</resources>
189-
<outputDirectory>${basedir}/target/classes/ca/weblite/objc</outputDirectory>
190-
</configuration>
191-
</execution>
192-
</executions>
193-
</plugin>
194-
195147
<!-- add java 11 module name to manifest -->
196148
<plugin>
197149
<groupId>org.apache.maven.plugins</groupId>
198150
<artifactId>maven-jar-plugin</artifactId>
199-
<version>3.1.2</version>
151+
<version>3.2.0</version>
200152
<configuration>
201153
<archive>
202154
<manifestEntries>
@@ -210,19 +162,12 @@
210162
<plugin>
211163
<groupId>com.github.spotbugs</groupId>
212164
<artifactId>spotbugs-maven-plugin</artifactId>
213-
<version>3.1.12.2</version>
165+
<version>4.2.0</version>
214166
<configuration>
215167
<effort>more</effort>
216168
<jvmArgs>--add-opens java.base/java.lang=ALL-UNNAMED
217169
--illegal-access=deny</jvmArgs>
218170
</configuration>
219-
<dependencies>
220-
<dependency>
221-
<groupId>com.github.spotbugs</groupId>
222-
<artifactId>spotbugs</artifactId>
223-
<version>4.0.0-beta4</version>
224-
</dependency>
225-
</dependencies>
226171
<executions>
227172
<!-- Ensures that spotbugs inspects source code when project is deployed. -->
228173
<!-- <execution> -->
@@ -251,10 +196,21 @@
251196

252197
<pluginManagement>
253198
<plugins>
199+
<plugin>
200+
<groupId>org.apache.maven.plugins</groupId>
201+
<artifactId>maven-compiler-plugin</artifactId>
202+
<version>3.8.1</version>
203+
</plugin>
204+
<plugin>
205+
<groupId>org.apache.maven.plugins</groupId>
206+
<artifactId>maven-surefire-plugin</artifactId>
207+
<version>3.0.0-M5</version>
208+
</plugin>
209+
254210
<plugin>
255211
<groupId>org.apache.maven.plugins</groupId>
256212
<artifactId>maven-source-plugin</artifactId>
257-
<version>3.1.0</version>
213+
<version>3.2.1</version>
258214
<executions>
259215
<execution>
260216
<id>attach-sources</id>
@@ -268,7 +224,7 @@
268224
<plugin>
269225
<groupId>org.apache.maven.plugins</groupId>
270226
<artifactId>maven-javadoc-plugin</artifactId>
271-
<version>3.1.1</version>
227+
<version>3.2.0</version>
272228
<configuration>
273229
<failOnError>false</failOnError>
274230
<!-- <additionalparam>-Xdoclint:none</additionalparam> -->
@@ -333,16 +289,9 @@
333289

334290
<dependency>
335291
<groupId>org.junit.jupiter</groupId>
336-
<artifactId>junit-jupiter-api</artifactId>
337-
<version>5.5.1</version>
338-
<scope>test</scope>
339-
</dependency>
340-
<dependency>
341-
<groupId>org.junit.vintage</groupId>
342-
<artifactId>junit-vintage-engine</artifactId>
343-
<version>5.5.1</version>
292+
<artifactId>junit-jupiter</artifactId>
293+
<version>5.7.0</version>
344294
<scope>test</scope>
345295
</dependency>
346-
347296
</dependencies>
348297
</project>
Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package ca.weblite.nativeutils;
22

3-
import java.io.File;
43
import java.io.FileNotFoundException;
5-
import java.io.FileOutputStream;
64
import java.io.IOException;
75
import java.io.InputStream;
86
import java.io.OutputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.StandardOpenOption;
910

1011
/**
1112
* Simple library class for working with JNI (Java Native Interface)
@@ -24,96 +25,81 @@ private NativeUtils() {
2425
}
2526

2627
/**
27-
* <p>loadLibraryFromJar.</p>
28+
* Loads a library from current JAR archive, using the class loader of the {@code NativeUtils}
29+
* class to find the resource in the JAR.
2830
*
2931
* @param path a {@link java.lang.String} object.
3032
* @throws java.io.IOException if any.
33+
* @throws UnsatisfiedLinkError if loading the native library fails.
3134
*/
3235
public static void loadLibraryFromJar(String path) throws IOException {
3336
loadLibraryFromJar(path, NativeUtils.class);
3437
}
3538

3639
/**
37-
* Loads library from current JAR archive
40+
* Loads a library from current JAR archive.
3841
*
3942
* The file from JAR is copied into system temporary directory and then loaded. The temporary file is deleted after exiting.
4043
* Method uses String as filename because the pathname is "abstract", not system-dependent.
4144
*
4245
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
43-
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
44-
* @throws java.lang.IllegalArgumentException If the path is not absolute or if the filename is shorter than three characters (restriction of @see File#createTempFile(java.lang.String, java.lang.String)).
4546
* @param path a {@link java.lang.String} object.
46-
* @param source a {@link java.lang.Class} object.
47+
* @param source {@code Class} whose class loader should be used to look up the resource in the JAR file
4748
* @throws java.io.IOException if any.
49+
* @throws UnsatisfiedLinkError if loading the native library fails.
4850
*/
49-
public static void loadLibraryFromJar(String path, Class source) throws IOException {
50-
51-
51+
public static void loadLibraryFromJar(String path, Class<?> source) throws IOException, UnsatisfiedLinkError {
5252
// Finally, load the library
53-
System.load(loadFileFromJar(path, source).getAbsolutePath());
53+
System.load(extractFromJar(path, source).toAbsolutePath().toString());
5454
}
5555

5656
/**
57-
* <p>loadFileFromJar.</p>
57+
* Extracts a resource from the JAR and stores it as temporary file
58+
* in the file system.
5859
*
59-
* @param path a {@link java.lang.String} object.
60-
* @param source a {@link java.lang.Class} object.
61-
* @return a {@link java.io.File} object.
60+
* @param path path of the resource, must begin with {@code '/'}, see {@link Class#getResourceAsStream(String)}
61+
* @param source {@code Class} whose class loader should be used to look up the resource in the JAR file
62+
* @return file path of the temporary file extracted from this JAR
6263
* @throws java.io.IOException if any.
6364
*/
64-
public static File loadFileFromJar(String path, Class source) throws IOException {
65+
public static Path extractFromJar(String path, Class<?> source) throws IOException {
6566
if (!path.startsWith("/")) {
6667
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
6768
}
6869

69-
// Obtain filename from path
70-
String[] parts = path.split("/");
71-
String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
70+
String filename = path.substring(path.lastIndexOf('/') + 1);
7271

73-
// Split filename to prexif and suffix (extension)
74-
String prefix = "";
75-
String suffix = null;
76-
if (filename != null) {
77-
parts = filename.split("\\.", 2);
78-
prefix = parts[0];
79-
suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-)
72+
// Split filename to prefix and suffix (extension)
73+
String prefix;
74+
String suffix;
75+
int lastDot = filename.lastIndexOf('.');
76+
if (lastDot == -1) {
77+
// No file extension; use complete filename as prefix
78+
prefix = filename;
79+
suffix = null;
80+
} else {
81+
prefix = filename.substring(0, lastDot);
82+
suffix = filename.substring(lastDot);
8083
}
8184

8285
// Check if the filename is okay
83-
if (filename == null || prefix.length() < 3) {
86+
if (prefix.length() < 3) {
8487
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
8588
}
8689

8790
// Prepare temporary file
88-
File temp = File.createTempFile(prefix, suffix);
89-
temp.deleteOnExit();
90-
91-
if (!temp.exists()) {
92-
throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist.");
93-
}
94-
95-
// Prepare buffer for data copying
96-
byte[] buffer = new byte[1024];
97-
int readBytes;
91+
Path temp = Files.createTempFile(prefix, suffix);
92+
temp.toFile().deleteOnExit();
9893

9994
// Open and check input stream
10095
InputStream is = source.getResourceAsStream(path);
10196
if (is == null) {
10297
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
10398
}
10499

105-
// Open output stream and copy data between source file in JAR and the temporary file
106-
OutputStream os = new FileOutputStream(temp);
107-
try {
108-
while ((readBytes = is.read(buffer)) != -1) {
109-
os.write(buffer, 0, readBytes);
110-
}
111-
} finally {
112-
// If read/write fails, close streams safely before throwing an exception
113-
os.close();
114-
is.close();
100+
try (is; OutputStream out = Files.newOutputStream(temp, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
101+
is.transferTo(out);
115102
}
116103
return temp;
117-
118104
}
119105
}

0 commit comments

Comments
 (0)