Skip to content

Commit

Permalink
Showing 12 changed files with 375 additions and 377 deletions.
18 changes: 9 additions & 9 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -130,10 +130,10 @@
import org.jruby.truffle.translator.TranslatorDriver;
import org.jruby.util.ByteList;
import org.jruby.util.DefinedMessage;
import org.jruby.util.DynamicJRubyClassLoader;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.IOInputStream;
import org.jruby.util.IOOutputStream;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.jruby.util.KCode;
import org.jruby.util.SafePropertyAccessor;
import org.jruby.util.cli.Options;
@@ -727,7 +727,7 @@ public IRubyObject runNormally(Node scriptNode) {
// IR JIT does not handle all scripts yet, so let those that fail run in interpreter instead
// FIXME: restore error once JIT should handle everything
try {
scriptAndCode = tryCompile(scriptNode, new JRubyClassLoader(getJRubyClassLoader()));
scriptAndCode = tryCompile(scriptNode, new ClassDefininngJRubyClassLoader(getJRubyClassLoader()));
if (scriptAndCode != null && Options.JIT_LOGGING.load()) {
LOG.info("done compiling target script: " + scriptNode.getPosition().getFile());
}
@@ -767,7 +767,7 @@ public IRubyObject runNormally(Node scriptNode) {
* @return an instance of the successfully-compiled Script, or null.
*/
public Script tryCompile(Node node) {
return tryCompile(node, new JRubyClassLoader(getJRubyClassLoader())).script();
return tryCompile(node, new ClassDefininngJRubyClassLoader(getJRubyClassLoader())).script();
}

private void failForcedCompile(Node scriptNode) throws RaiseException {
@@ -783,7 +783,7 @@ private void handeCompileError(Node node, Throwable t) {
}
}

private ScriptAndCode tryCompile(Node node, JRubyClassLoader classLoader) {
private ScriptAndCode tryCompile(Node node, ClassDefininngJRubyClassLoader classLoader) {
try {
return Compiler.getInstance().execute(this, node, classLoader);
} catch (NotCompilableException e) {
@@ -2569,10 +2569,10 @@ public static ClassLoader getClassLoader() {
*
* @return
*/
public synchronized DynamicJRubyClassLoader getJRubyClassLoader() {
public synchronized JRubyClassLoader getJRubyClassLoader() {
// FIXME: Get rid of laziness and handle restricted access elsewhere
if (!Ruby.isSecurityRestricted() && jrubyClassLoader == null) {
jrubyClassLoader = new DynamicJRubyClassLoader(config.getLoader());
jrubyClassLoader = new JRubyClassLoader(config.getLoader());

// if jit code cache is used, we need to add the cache directory to the classpath
// so the previously generated class files can be reused.
@@ -2913,7 +2913,7 @@ public void compileAndLoadFile(String filename, InputStream in, boolean wrap) {
// script was not found in cache above, so proceed to compile
Node scriptNode = parseFile(readStream, filename, null);
if (script == null) {
scriptAndCode = tryCompile(scriptNode, new JRubyClassLoader(jrubyClassLoader));
scriptAndCode = tryCompile(scriptNode, new ClassDefininngJRubyClassLoader(jrubyClassLoader));
if (scriptAndCode != null) script = scriptAndCode.script();
}

@@ -4831,7 +4831,7 @@ public FilenoUtil getFilenoUtil() {

// Java support
private JavaSupport javaSupport;
private DynamicJRubyClassLoader jrubyClassLoader;
private JRubyClassLoader jrubyClassLoader;

// Management/monitoring
private BeanManager beanManager;
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/anno/InvokerGenerator.java
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@

import org.jruby.RubyModule.MethodClumper;
import org.jruby.internal.runtime.methods.DumpingInvocationMethodFactory;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

@@ -68,7 +68,7 @@ public static void main(String[] args) throws Exception {
br.close();
}

DumpingInvocationMethodFactory dumper = new DumpingInvocationMethodFactory(args[1], new JRubyClassLoader(ClassLoader.getSystemClassLoader()));
DumpingInvocationMethodFactory dumper = new DumpingInvocationMethodFactory(args[1], new ClassDefininngJRubyClassLoader(ClassLoader.getSystemClassLoader()));

for (String name : classNames) {
MethodClumper clumper = new MethodClumper();
8 changes: 4 additions & 4 deletions core/src/main/java/org/jruby/compiler/util/HandleFactory.java
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@
import java.lang.reflect.Modifier;
import org.jruby.compiler.JITCompiler;
import org.jruby.compiler.impl.SkinnyMethodAdapter;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import static org.jruby.util.CodegenUtils.*;
@@ -53,7 +53,7 @@ public static class Handle {
public Object invoke(Object receiver, Object... args) { throw fail(); }
}

public static Handle createHandle(JRubyClassLoader classLoader, Method method) {
public static Handle createHandle(ClassDefininngJRubyClassLoader classLoader, Method method) {
String name = createHandleName(method);

Class handleClass;
@@ -72,9 +72,9 @@ public static Handle createHandle(JRubyClassLoader classLoader, Method method) {
}
}

public static Class createHandleClass(JRubyClassLoader classLoader, Method method, String name) {
public static Class createHandleClass(ClassDefininngJRubyClassLoader classLoader, Method method, String name) {
byte[] bytes = createHandleBytes(method, name);
return (classLoader != null ? classLoader : new JRubyClassLoader(JRubyClassLoader.class.getClassLoader())).defineClass(name, bytes);
return (classLoader != null ? classLoader : new ClassDefininngJRubyClassLoader(ClassDefininngJRubyClassLoader.class.getClassLoader())).defineClass(name, bytes);
}

public static byte[] createHandleBytes(Method method, String name) {
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@
import org.jruby.util.CodegenUtils;
import static org.jruby.util.CodegenUtils.*;
import static java.lang.System.*;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Label;
import org.objectweb.asm.util.CheckClassAdapter;
@@ -174,7 +174,7 @@ public class InvocationMethodFactory extends MethodFactory implements Opcodes {
public static final int BLOCK_INDEX = 6;

/** The classloader to use for code loading */
protected final JRubyClassLoader classLoader;
protected final ClassDefininngJRubyClassLoader classLoader;

/** An object to sync against when loading classes, to avoid dups */
protected final Object syncObject;
@@ -204,10 +204,10 @@ public InvocationMethodFactory(ClassLoader classLoader) {
// use the given classloader as our sync, regardless of whether we wrap it
this.syncObject = classLoader;

if (classLoader instanceof JRubyClassLoader) {
this.classLoader = (JRubyClassLoader)classLoader;
if (classLoader instanceof ClassDefininngJRubyClassLoader) {
this.classLoader = (ClassDefininngJRubyClassLoader)classLoader;
} else {
this.classLoader = new JRubyClassLoader(classLoader);
this.classLoader = new ClassDefininngJRubyClassLoader(classLoader);
}
}

6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/ir/Compiler.java
Original file line number Diff line number Diff line change
@@ -21,14 +21,14 @@
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Compiler extends IRTranslator<ScriptAndCode, JRubyClassLoader> {
public class Compiler extends IRTranslator<ScriptAndCode, ClassDefininngJRubyClassLoader> {

// Compiler is singleton
private Compiler() {}
@@ -43,7 +43,7 @@ public static Compiler getInstance() {
}

@Override
protected ScriptAndCode execute(final Ruby runtime, final IRScriptBody scope, JRubyClassLoader classLoader) {
protected ScriptAndCode execute(final Ruby runtime, final IRScriptBody scope, ClassDefininngJRubyClassLoader classLoader) {
JVMVisitor visitor;
byte[] bytecode;
Class compiled;
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callsite.CacheEntry;
import org.jruby.util.ClassDefiningClassLoader;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import static org.jruby.util.CodegenUtils.*;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Type;
@@ -456,11 +456,11 @@ public static Class defineRealImplClass(Ruby ruby, String name, Class superClass
// create the class
byte[] bytes = cw.toByteArray();
Class newClass;
JRubyClassLoader loader;
if (superClass.getClassLoader() instanceof JRubyClassLoader) {
loader = new JRubyClassLoader(superClass.getClassLoader());
ClassDefininngJRubyClassLoader loader;
if (superClass.getClassLoader() instanceof ClassDefininngJRubyClassLoader) {
loader = new ClassDefininngJRubyClassLoader(superClass.getClassLoader());
} else {
loader = new JRubyClassLoader(ruby.getJRubyClassLoader());
loader = new ClassDefininngJRubyClassLoader(ruby.getJRubyClassLoader());
}
try {
newClass = loader.loadClass(name);
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/runtime/MethodFactory.java
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@
import org.jruby.lexer.yacc.ISourcePosition;
import org.jruby.parser.StaticScope;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.cli.Options;
import org.jruby.util.log.Logger;
@@ -81,7 +81,7 @@ public abstract class MethodFactory {
baos.write(buf, 0, bytesRead);
}

JRubyClassLoader oscl = new JRubyClassLoader(Ruby.getClassLoader());
ClassDefininngJRubyClassLoader oscl = new ClassDefininngJRubyClassLoader(Ruby.getClassLoader());
Class<?> unloaderClass = oscl.defineClass("org.jruby.util.JDBCDriverUnloader", baos.toByteArray());
unloaderClass.newInstance();
can = true;
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
import org.jruby.Ruby;
import org.jruby.ast.executable.Script;
import org.jruby.util.OneShotClassLoader;
import org.jruby.util.JRubyClassLoader;
import org.jruby.util.ClassDefininngJRubyClassLoader;
import org.objectweb.asm.ClassReader;

/**
@@ -31,7 +31,7 @@ public static Script loadScriptFromFile(Ruby runtime, InputStream inStream, Stri
baos.write(buf, 0, read);
}
buf = baos.toByteArray();
JRubyClassLoader jcl = runtime.getJRubyClassLoader();
ClassDefininngJRubyClassLoader jcl = runtime.getJRubyClassLoader();
OneShotClassLoader oscl = new OneShotClassLoader(jcl);

ClassReader cr = new ClassReader(buf);
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
**** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/

package org.jruby.util;

import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;

import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

public class ClassDefininngJRubyClassLoader extends URLClassLoader implements ClassDefiningClassLoader {

final static ProtectionDomain DEFAULT_DOMAIN;

static {
ProtectionDomain defaultDomain = null;
try {
defaultDomain = JRubyClassLoader.class.getProtectionDomain();
} catch (SecurityException se) {
// just use null since we can't acquire protection domain
}
DEFAULT_DOMAIN = defaultDomain;
}

public ClassDefininngJRubyClassLoader(ClassLoader parent) {
super(new URL[0], parent);
}

public Class<?> defineClass(String name, byte[] bytes) {
return super.defineClass(name, bytes, 0, bytes.length, DEFAULT_DOMAIN);
}

public Class<?> defineClass(String name, byte[] bytes, ProtectionDomain domain) {
return super.defineClass(name, bytes, 0, bytes.length, domain);
}
}
325 changes: 0 additions & 325 deletions core/src/main/java/org/jruby/util/DynamicJRubyClassLoader.java

This file was deleted.

295 changes: 279 additions & 16 deletions core/src/main/java/org/jruby/util/JRubyClassLoader.java
Original file line number Diff line number Diff line change
@@ -27,36 +27,299 @@

package org.jruby.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

public class JRubyClassLoader extends URLClassLoader implements ClassDefiningClassLoader {
public class JRubyClassLoader extends ClassDefininngJRubyClassLoader {

final static ProtectionDomain DEFAULT_DOMAIN;
private static final Logger LOG = LoggerFactory.getLogger("JRubyClassLoader");

static {
ProtectionDomain defaultDomain = null;
private final Map<URL,Set<String>> jarIndexes = new LinkedHashMap<URL,Set<String>>();

private Runnable unloader;

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

public void addURLNoIndex(URL url) {
// if we have such embedded jar within a jar, we copy it to temp file and use the
// the temp file with the super URLClassLoader
if ( url.toString().contains( "!/" )) {
InputStream in = null;
OutputStream out = null;
try
{
File f = File.createTempFile( "jruby", ".jar");
f.deleteOnExit();
out = new BufferedOutputStream( new FileOutputStream( f ) );
in = new BufferedInputStream( url.openStream() );
int i = in.read();
while( i != -1 ) {
out.write( i );
i = in.read();
}
out.close();
in.close();
url = f.toURI().toURL();
}
catch (IOException e)
{
throw new RuntimeException("BUG: we can not copy embedded jar to temp directory", e);
}
finally {
// make sure we close everything
if ( out != null ) {
try
{
out.close();
}
catch (IOException e)
{
}
}
if ( in != null ) {
try
{
in.close();
}
catch (IOException e)
{
}
}
}
}
super.addURL( url );
}

// Change visibility so others can see it
@Override
public void addURL(URL url) {
super.addURL(url);
indexJarContents(url);
}

@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> c = findLoadedClass(name);
if (c == null) {
try {
c = findClass(name);
} catch (ClassNotFoundException e) {
return super.loadClass(name, resolve);
}
}
return c;
}
}

/**
* Called when the parent runtime is torn down.
*/
public void tearDown(boolean debug) {
try {
defaultDomain = JRubyClassLoader.class.getProtectionDomain();
} catch (SecurityException se) {
// just use null since we can't acquire protection domain
// A hack to allow unloading all JDBC Drivers loaded by this classloader.
// See http://bugs.jruby.org/4226
getJDBCDriverUnloader().run();
} catch (Exception e) {
if (debug) {
LOG.debug(e);
}
}
DEFAULT_DOMAIN = defaultDomain;
}

public JRubyClassLoader(ClassLoader parent) {
super(new URL[0], parent);
public synchronized Runnable getJDBCDriverUnloader() {
if (unloader == null) {
try {
InputStream unloaderStream = getClass().getResourceAsStream("/org/jruby/util/JDBCDriverUnloader.class");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = unloaderStream.read(buf)) != -1) {
baos.write(buf, 0, bytesRead);
}

Class<?> unloaderClass = defineClass("org.jruby.util.JDBCDriverUnloader", baos.toByteArray());
unloader = (Runnable) unloaderClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return unloader;
}

public Class<?> defineClass(String name, byte[] bytes) {
return super.defineClass(name, bytes, 0, bytes.length, DEFAULT_DOMAIN);
@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
try {
return super.findClass(className);
} catch (ClassNotFoundException ex) {
String resourceName = className.replace('.', '/').concat(".class");

URL classUrl = null;
synchronized (jarIndexes) {
for (URL jarUrl : jarIndexes.keySet()) {
if (jarIndexes.get(jarUrl).contains(resourceName)) {
try {
classUrl = CompoundJarURLStreamHandler.createUrl(jarUrl, resourceName);
break;
} catch (IOException e) {
// keep going to next URL
}
}
}
}

if (classUrl != null) {
try {
InputStream input = classUrl.openStream();
try {
byte[] buffer = new byte[4096];
ByteArrayOutputStream output = new ByteArrayOutputStream();

for (int count = input.read(buffer); count > 0; count = input.read(buffer)) {
output.write(buffer, 0, count);
}

byte[] data = output.toByteArray();
return defineClass(className, data, 0, data.length);
} finally {
close(input);
}
} catch (IOException e) {
// just fall-through to the re-throw below
}
}

throw ex;
}
}

@Override
public URL findResource(String resourceName) {
URL result = super.findResource(resourceName);

if (result == null) {
synchronized (jarIndexes) {
for (URL jarUrl : jarIndexes.keySet()) {
if (jarIndexes.get(jarUrl).contains(resourceName)) {
try {
return CompoundJarURLStreamHandler.createUrl(jarUrl, resourceName);
} catch (IOException e) {
// keep going
}
}
}
}
}

return result;
}

public Class<?> defineClass(String name, byte[] bytes, ProtectionDomain domain) {
return super.defineClass(name, bytes, 0, bytes.length, domain);
@Override
public Enumeration<URL> findResources(String resourceName) throws IOException {
final List<URL> embeddedUrls = new ArrayList<URL>();

synchronized (jarIndexes) {
for (URL jarUrl : jarIndexes.keySet()) {
if (jarIndexes.get(jarUrl).contains(resourceName)) {
try {
embeddedUrls.add(CompoundJarURLStreamHandler.createUrl(jarUrl, resourceName));
} catch (IOException e) {
// keep going
}
}
}
}

if (embeddedUrls.isEmpty()) {
return super.findResources(resourceName);
} else {
final Enumeration<URL> originalResult = super.findResources(resourceName);

return new Enumeration<URL>() {
private Iterator<URL> extendedResult;

public URL nextElement() {
if (extendedResult == null) {
return originalResult.nextElement();
} else {
return extendedResult.next();
}
}

public boolean hasMoreElements() {
if (extendedResult == null) {
boolean result = originalResult.hasMoreElements();

if (!result) {
// original result is consumed, switching to result
// from embedded jars processing.
extendedResult = embeddedUrls.iterator();
result = extendedResult.hasNext();
}
return result;
} else {
return extendedResult.hasNext();
}
}
};
}
}

private void indexJarContents(URL jarUrl) {
String proto = jarUrl.getProtocol();
// we only need to index jar: and compoundjar: URLs
// 1st-level jar files with file: URLs are handled by the JDK
if (proto.equals("jar") || proto.equals(CompoundJarURLStreamHandler.PROTOCOL)) {
synchronized (jarIndexes) {
Set<String> entries = new HashSet<String>();
jarIndexes.put(jarUrl, entries);

try {
InputStream baseInputStream = jarUrl.openStream();
try {
JarInputStream baseJar = new JarInputStream(baseInputStream);
for (JarEntry entry = baseJar.getNextJarEntry(); entry != null; entry = baseJar.getNextJarEntry()) {
entries.add(entry.getName());
}
} finally {
close(baseInputStream);
}
} catch (IOException ex) {
// can't read the stream, keep going
}
}
}
}

private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException ignore) {
}
}
}
}
6 changes: 2 additions & 4 deletions core/src/main/java/org/jruby/util/OneShotClassLoader.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package org.jruby.util;

import java.security.ProtectionDomain;

/**
* Represents a class loader designed to load exactly one class.
*/
public class OneShotClassLoader extends ClassLoader implements ClassDefiningClassLoader {

public OneShotClassLoader(JRubyClassLoader parent) {
public OneShotClassLoader(ClassDefininngJRubyClassLoader parent) {
super(parent);
}

public Class<?> defineClass(String name, byte[] bytes) {
return super.defineClass(name, bytes, 0, bytes.length, JRubyClassLoader.DEFAULT_DOMAIN);
return super.defineClass(name, bytes, 0, bytes.length, ClassDefininngJRubyClassLoader.DEFAULT_DOMAIN);
}
}

1 comment on commit b45a7f0

@kares
Copy link
Member

@kares kares commented on b45a7f0 Dec 1, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot Kristian ... very appreciated ... shall avoid any painfulness during the transition!

Please sign in to comment.