Skip to content

Commit

Permalink
do not assume MX classes will work + safe-guard system property get
Browse files Browse the repository at this point in the history
... RuntimeMXBean#getName is assumed to have PID - added a fallback 
when by any reason (e.g. on Android) java.lang.management throws

also warn when tmpdir not accessible and fallback to current work dir
  • Loading branch information
kares committed Sep 8, 2016
1 parent dc44e7d commit 46d289a
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions core/src/main/java/org/jruby/util/JRubyClassLoader.java
Expand Up @@ -60,25 +60,12 @@ public class JRubyClassLoader extends ClassDefiningJRubyClassLoader {

private Runnable unloader;

private File tempdir;
private File tempDir;

public JRubyClassLoader(ClassLoader parent) {
super(parent);
}

private File getTempDir() {
if (tempdir == null) {
String processName = ManagementFactory.getRuntimeMXBean().getName();
long pid = Long.parseLong(processName.split("@")[0]);
File dir = new File(System.getProperty("java.io.tmpdir"), "jruby-" + pid);
if (dir.mkdirs()) {
dir.deleteOnExit();
}
tempdir = dir;
}
return tempdir;
}

// Change visibility so others can see it
@Override
public void addURL(URL url) {
Expand Down Expand Up @@ -123,6 +110,43 @@ public void addURL(URL url) {
super.addURL( url );
}

private File getTempDir() {
File tempDir = this.tempDir;
if (tempDir != null) return tempDir;

tempDir = new File(systemTmpDir(), tempDirName());
if (tempDir.mkdirs()) {
tempDir.deleteOnExit();
}
return this.tempDir = tempDir;
}

private static final String TEMP_DIR_PREFIX = "jruby-";
private static String tempDirName;

private static String tempDirName() {
String dirName = tempDirName;
if (dirName != null) return dirName;
try {
String processName = ManagementFactory.getRuntimeMXBean().getName();
return tempDirName = TEMP_DIR_PREFIX + processName.split("@")[0]; // jruby-PID
}
catch (Throwable ex) {
LOG.debug(ex); // e.g. java.lang.management classes not available (on Android)
return tempDirName = TEMP_DIR_PREFIX + Integer.toHexString(System.identityHashCode(JRubyClassLoader.class));
}
}

private static String systemTmpDir() {
try {
return System.getProperty("java.io.tmpdir");
}
catch (SecurityException ex) {
LOG.warn("could not access 'java.io.tmpdir' will use working directory", ex);
}
return "";
}

/**
* @deprecated use {@link #close()} instead
*/
Expand Down

0 comments on commit 46d289a

Please sign in to comment.