Skip to content

Commit e20ee7b

Browse files
committed
Extract library-dropping code to edu.rpi.aris.util.SharedObjectLoader.
1 parent a0bca52 commit e20ee7b

File tree

5 files changed

+74
-78
lines changed

5 files changed

+74
-78
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ This repository contains several modules for the Aris platform which are as foll
9393
* **packaging** - Contains files for building distribution packages
9494
* **proof-client** - Contains the client side module of the Aris Proof system
9595
* **proof-server** - Contains the server side module of the Aris Proof system
96-
* **service** - Contains the ServiceLoader interfaces required to add new programs to the Aris platform
96+
* **service** - Contains the ServiceLoader interfaces required to add new programs to the Aris platform, and utilities common to all Aris programs
9797
9898
## Authors
9999

assign-server/src/main/java/edu/rpi/aris/assign/server/auth/PAMLoginAuth.java

+2-36
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
public class PAMLoginAuth extends LoginAuth {
1717

1818
private static final String LIB_NAME = "libassign_pam";
19-
private static final String LIB_FILE = LIB_NAME + ".so";
2019
private static final Logger log = LogManager.getLogger();
2120
private static final PAMLoginAuth instance = new PAMLoginAuth();
22-
private static boolean loaded = false;
2321

2422
static {
25-
loadLib();
23+
edu.rpi.aris.util.SharedObjectLoader.loadLib(LIB_NAME);
2624
}
2725

2826
private PAMLoginAuth() {
@@ -32,38 +30,6 @@ static void register() {
3230
LoginAuth.registerLoginAuth(instance);
3331
}
3432

35-
private static void loadLib() {
36-
if (!SystemUtils.IS_OS_LINUX) {
37-
log.info("PAM authentication is only available on linux and has been disabled");
38-
return;
39-
}
40-
log.info("Loading libassign_pam.so native library");
41-
42-
File tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_FILE);
43-
int i = 0;
44-
while (tmpFile.exists() && !tmpFile.delete())
45-
tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_NAME + (i++) + ".so");
46-
boolean copied = false;
47-
try (InputStream in = ClassLoader.getSystemResourceAsStream(LIB_FILE);
48-
FileOutputStream out = new FileOutputStream(tmpFile)) {
49-
if (in != null) {
50-
IOUtils.copy(in, out);
51-
copied = true;
52-
}
53-
} catch (IOException e) {
54-
copied = false;
55-
log.error("Failed to extract " + LIB_NAME + " to temp directory", e);
56-
}
57-
if (copied) {
58-
try {
59-
System.load(tmpFile.getCanonicalPath());
60-
loaded = true;
61-
} catch (Exception e) {
62-
log.error("Failed to load native " + LIB_NAME + " library", e);
63-
}
64-
}
65-
}
66-
6733
static PAMLoginAuth getInstance() {
6834
return instance;
6935
}
@@ -82,7 +48,7 @@ public String checkAuth(String user, String pass, String salt, String savedHash)
8248

8349
@Override
8450
public boolean isSupported() {
85-
return loaded;
51+
return edu.rpi.aris.util.SharedObjectLoader.isLoaded(LIB_NAME);
8652
}
8753

8854
@Override

libaris/src/main/java/edu/rpi/aris/ast/ASTConstructor.java

+1-40
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,13 @@
11
package edu.rpi.aris.ast;
22

33
import edu.rpi.aris.ast.Expression.*;
4-
import java.io.*;
54
import java.util.*;
65
import org.antlr.v4.runtime.*;
76
import org.antlr.v4.runtime.misc.ParseCancellationException;
87
import org.antlr.v4.runtime.tree.*;
9-
import org.apache.commons.io.IOUtils;
10-
import org.apache.commons.lang3.SystemUtils;
11-
import org.apache.logging.log4j.*;
128

139
public class ASTConstructor extends ParseExpressionBaseVisitor<Expression> implements ParseExpressionVisitor<Expression> {
14-
// TODO: deduplicate with edu.rpi.aris.assign.server.auth.PAMLoginAuth and put into a common util class
15-
private static final Logger log = LogManager.getLogger();
16-
private static final Set<String> loaded = new HashSet();
17-
static { loadLib("liblibaris_rs"); }
18-
private static void loadLib(String LIB_NAME) {
19-
if (!SystemUtils.IS_OS_LINUX) {
20-
// TODO: drop a dll on windows, a .so on mac
21-
return;
22-
}
23-
String LIB_FILE = LIB_NAME + ".so";
24-
log.info("Loading " + LIB_FILE + " native library");
25-
26-
File tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_FILE);
27-
int i = 0;
28-
while (tmpFile.exists() && !tmpFile.delete())
29-
tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_NAME + (i++) + ".so");
30-
boolean copied = false;
31-
try (InputStream in = ClassLoader.getSystemResourceAsStream(LIB_FILE);
32-
FileOutputStream out = new FileOutputStream(tmpFile)) {
33-
if (in != null) {
34-
IOUtils.copy(in, out);
35-
copied = true;
36-
}
37-
} catch (IOException e) {
38-
copied = false;
39-
log.error("Failed to extract " + LIB_NAME + " to temp directory", e);
40-
}
41-
if (copied) {
42-
try {
43-
System.load(tmpFile.getCanonicalPath());
44-
loaded.add(LIB_NAME);
45-
} catch (Exception e) {
46-
log.error("Failed to load native " + LIB_NAME + " library", e);
47-
}
48-
}
49-
}
10+
static { edu.rpi.aris.util.SharedObjectLoader.loadLib("liblibaris_rs"); }
5011

5112
public static native Expression parseViaRust(String s);
5213
public static Expression parse(String s) {

service/build.gradle

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
dependencies {
2+
compile 'org.apache.commons:commons-lang3:3.7'
3+
compile 'org.apache.logging.log4j:log4j-api:2.10.0'
4+
compile 'org.apache.logging.log4j:log4j-core:2.10.0'
5+
compile group: 'commons-io', name: 'commons-io', version: '2.6'
6+
27
testCompile group: 'junit', name: 'junit', version: '4.12'
3-
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package edu.rpi.aris.util;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
import org.apache.commons.io.IOUtils;
6+
import org.apache.commons.lang3.SystemUtils;
7+
import org.apache.logging.log4j.*;
8+
9+
public class SharedObjectLoader {
10+
private static final Logger log = LogManager.getLogger();
11+
private static final Set<String> loaded = new HashSet();
12+
13+
public static boolean isLoaded(String LIB_NAME) {
14+
return loaded.contains(LIB_NAME);
15+
}
16+
public static void loadLib(String LIB_NAME) {
17+
if(!loaded.contains(LIB_NAME)) {
18+
__loadLib(LIB_NAME);
19+
}
20+
}
21+
22+
private static String getPlatformLibrarySuffix() {
23+
if (SystemUtils.IS_OS_LINUX) {
24+
return ".so";
25+
} else {
26+
// TODO: drop a dll on windows, a .so on mac
27+
return null;
28+
}
29+
}
30+
31+
private static void __loadLib(String LIB_NAME) {
32+
String suffix = getPlatformLibrarySuffix();
33+
if(suffix == null) {
34+
log.info("Loading libraries is not yet supported on the current platform");
35+
return;
36+
}
37+
String LIB_FILE = LIB_NAME + suffix;
38+
log.info("Loading " + LIB_FILE + " native library");
39+
40+
File tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_FILE);
41+
int i = 0;
42+
while (tmpFile.exists() && !tmpFile.delete())
43+
tmpFile = new File(System.getProperty("java.io.tmpdir"), LIB_NAME + (i++) + ".so");
44+
boolean copied = false;
45+
try (InputStream in = ClassLoader.getSystemResourceAsStream(LIB_FILE);
46+
FileOutputStream out = new FileOutputStream(tmpFile)) {
47+
if (in != null) {
48+
IOUtils.copy(in, out);
49+
copied = true;
50+
}
51+
} catch (IOException e) {
52+
copied = false;
53+
log.error("Failed to extract " + LIB_NAME + " to temp directory", e);
54+
}
55+
if (copied) {
56+
try {
57+
System.load(tmpFile.getCanonicalPath());
58+
loaded.add(LIB_NAME);
59+
} catch (Exception e) {
60+
log.error("Failed to load native " + LIB_NAME + " library", e);
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)