Skip to content

Commit

Permalink
Version 2.4, fixed library files not cleaned from temp. directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed May 15, 2016
1 parent 8d1ab72 commit 674ab29
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 110 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ You can include `system-hook` from this GitHub repository by adding this depende
<dependency>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
<version>2.3</version>
<version>2.4</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2.3.{build}
version: 2.4.{build}

branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>lc.kra.system</groupId>
<artifactId>system-hook</artifactId>
<version>2.3</version>
<version>2.4</version>
<description>Global Keyboard / Mouse Hook for Java applications.</description>
<url>https://github.com/kristian/system-hook</url>

Expand Down
224 changes: 117 additions & 107 deletions src/main/java/lc/kra/system/LibraryLoader.java
Original file line number Diff line number Diff line change
@@ -1,108 +1,118 @@
/**
* Copyright (c) 2016 Kristian Kraljic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package lc.kra.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

public class LibraryLoader {
/**
* Tries to laod the library with the given name
*
* @param name The name of the library to load
* @throws UnsatisfiedLinkError Thrown in case loading the library fails
*/
public static void loadLibrary(String name) throws UnsatisfiedLinkError {
String libName = System.getProperty(name+".lib.name",System.mapLibraryName(name).replaceAll("\\.jnilib$","\\.dylib")),
libPath = System.getProperty(name+".lib.path");

try {
if(libPath==null)
System.loadLibrary(libName);
else System.load(new File(libPath,libName).getAbsolutePath());
return;
} catch(UnsatisfiedLinkError e) { /* do nothing, try next */ }

libName = System.mapLibraryName(name+'-'+getOperatingSystemName()+'-'+getOperatingSystemArchitecture())
.replaceAll("\\.jnilib$","\\.dylib"); // for JDK < 1.7
String libResourcePath = LibraryLoader.class.getPackage().getName().replace('.','/')+"/lib/"+libName;

File tempFile = null; InputStream inputStream = null; OutputStream outputStream = null;
try {
if((inputStream=LibraryLoader.class.getClassLoader().getResourceAsStream(libResourcePath))==null)
throw new FileNotFoundException("lib: "+libName+" not found in lib directory");
tempFile = File.createTempFile(name+"-",libName.substring(libName.lastIndexOf('.')));

outputStream = new FileOutputStream(tempFile);
int read; byte[] buffer = new byte[1024];
while((read=inputStream.read(buffer))!=-1)
outputStream.write(buffer,0,read);
outputStream.close();

System.load(tempFile.getAbsolutePath());
} catch(IOException e) {
throw new UnsatisfiedLinkError(e.getMessage());
} finally {
if(inputStream!=null) {
try { inputStream.close(); }
catch (IOException e) { /* nothing to do here */ }
}
if(outputStream!=null) {
try { outputStream.close(); }
catch (IOException e) { /* nothing to do here */ }
}
if(tempFile!=null) tempFile.delete();
}
}

private static String getOperatingSystemName() {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if(osName.startsWith("windows"))
return "windows";
else if(osName.startsWith("linux"))
return "linux";
else if(osName.startsWith("mac os"))
return "darwin";
else if(osName.startsWith("sunos")||osName.startsWith("solaris"))
return "solaris";
else return osName;
}
private static String getOperatingSystemArchitecture() {
String osArch = System.getProperty("os.arch").toLowerCase(Locale.ROOT);
if((osArch.startsWith("i")||osArch.startsWith("x"))&&osArch.endsWith("86"))
return "x86";
else if((osArch.equals("i86")||osArch.startsWith("amd"))&&osArch.endsWith("64"))
return "amd64";
else if(osArch.startsWith("arm"))
return "arm";
else if(osArch.startsWith("sparc"))
return !osArch.endsWith("64")?"sparc":"sparc64";
else if(osArch.startsWith("ppc"))
return !osArch.endsWith("64")?"ppc":"ppc64";
else return osArch;
}
/**
* Copyright (c) 2016 Kristian Kraljic
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package lc.kra.system;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class LibraryLoader {
/**
* Tries to laod the library with the given name
*
* @param name The name of the library to load
* @throws UnsatisfiedLinkError Thrown in case loading the library fails
*/
public static void loadLibrary(String name) throws UnsatisfiedLinkError {
String libName = System.getProperty(name+".lib.name", System.mapLibraryName(name).replaceAll("\\.jnilib$", "\\.dylib")),
libPath = System.getProperty(name+".lib.path");

try {
if(libPath==null)
System.loadLibrary(libName);
else System.load(new File(libPath, libName).getAbsolutePath());
return;
} catch(UnsatisfiedLinkError e) { /* do nothing, try next */ }

libName = System.mapLibraryName(name+'-'+getOperatingSystemName()+'-'+getOperatingSystemArchitecture())
.replaceAll("\\.jnilib$", "\\.dylib"); // for JDK < 1.7
String libNameExtension = libName.substring(libName.lastIndexOf('.')),
libResourcePath = LibraryLoader.class.getPackage().getName().replace('.', '/')+"/lib/"+libName;

InputStream inputStream = null; OutputStream outputStream = null;
try {
if((inputStream=LibraryLoader.class.getClassLoader().getResourceAsStream(libResourcePath))==null)
throw new FileNotFoundException("lib: "+libName+" not found in lib directory");
File tempFile = File.createTempFile(name+"-", libNameExtension);

Checksum checksum = new CRC32();
outputStream = new FileOutputStream(tempFile);
int read; byte[] buffer = new byte[1024];
while((read=inputStream.read(buffer))!=-1) {
outputStream.write(buffer, 0, read);
checksum.update(buffer, 0, read);
}
outputStream.close();

File libFile = new File(tempFile.getParentFile(), name+"+"+checksum.getValue()+libNameExtension);
if(!libFile.exists())
tempFile.renameTo(libFile);
else tempFile.delete();

System.load(libFile.getAbsolutePath());
} catch(IOException e) {
throw new UnsatisfiedLinkError(e.getMessage());
} finally {
if(inputStream!=null) {
try { inputStream.close(); }
catch (IOException e) { /* nothing to do here */ }
}
if(outputStream!=null) {
try { outputStream.close(); }
catch (IOException e) { /* nothing to do here */ }
}
}
}

private static String getOperatingSystemName() {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if(osName.startsWith("windows"))
return "windows";
else if(osName.startsWith("linux"))
return "linux";
else if(osName.startsWith("mac os"))
return "darwin";
else if(osName.startsWith("sunos")||osName.startsWith("solaris"))
return "solaris";
else return osName;
}
private static String getOperatingSystemArchitecture() {
String osArch = System.getProperty("os.arch").toLowerCase(Locale.ROOT);
if((osArch.startsWith("i")||osArch.startsWith("x"))&&osArch.endsWith("86"))
return "x86";
else if((osArch.equals("i86")||osArch.startsWith("amd"))&&osArch.endsWith("64"))
return "amd64";
else if(osArch.startsWith("arm"))
return "arm";
else if(osArch.startsWith("sparc"))
return !osArch.endsWith("64")?"sparc":"sparc64";
else if(osArch.startsWith("ppc"))
return !osArch.endsWith("64")?"ppc":"ppc64";
else return osArch;
}
}

0 comments on commit 674ab29

Please sign in to comment.