Skip to content

Commit

Permalink
Showing 8 changed files with 216 additions and 62 deletions.
66 changes: 22 additions & 44 deletions core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -29,22 +29,25 @@
package org.jruby;

import jnr.posix.util.Platform;

import org.jruby.ast.executable.Script;
import org.jruby.compiler.ASTCompiler;
import org.jruby.compiler.ASTCompiler19;
import org.jruby.exceptions.MainExitException;
import org.jruby.embed.util.SystemPropertyCatcher;
import org.jruby.exceptions.MainExitException;
import org.jruby.runtime.Constants;
import org.jruby.runtime.backtrace.TraceType;
import org.jruby.runtime.load.LoadService;
import org.jruby.runtime.load.LoadService19;
import org.jruby.runtime.profile.builtin.ProfileOutput;
import org.jruby.util.ClassCache;
import org.jruby.util.FileResource;
import org.jruby.util.InputStreamMarkCursor;
import org.jruby.util.JRubyFile;
import org.jruby.util.KCode;
import org.jruby.util.NormalizedFile;
import org.jruby.util.SafePropertyAccessor;
import org.jruby.util.URLResource;
import org.jruby.util.cli.ArgumentProcessor;
import org.jruby.util.cli.Options;
import org.jruby.util.cli.OutputStrings;
@@ -55,6 +58,7 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
@@ -408,37 +412,32 @@ public InputStream getScriptSource() {
return getInput();
} else {
final String script = getScriptFileName();
final InputStream stream;
if (script.startsWith("file:") && script.indexOf(".jar!/") != -1) {
stream = new URL("jar:" + script).openStream();
} else if (script.startsWith("classpath:")) {
stream = Ruby.getClassLoader().getResourceAsStream(script.substring("classpath:".length()));
} else {
File file = JRubyFile.create(getCurrentDirectory(), getScriptFileName());
if (isXFlag()) {
// search for a shebang line and
// return the script between shebang and __END__ or CTRL-Z (0x1A)
return findScript(file);
FileResource resource =JRubyFile.createResource(null, getCurrentDirectory(), getScriptFileName());

This comment has been minimized.

Copy link
@kares

kares Jun 5, 2015

Member

unable to merge this part (to master) ... just so much divergence between these APIs at least if they were "somehow" compatible with each other but they're quite different ... under master createResource assumes a non null runtime :

JRubyFile.java:113:in `createResource': java.lang.NullPointerException
    from JRubyFile.java:75:in `createResource'
    from RubyInstanceConfig.java:392:in `getScriptSource'
    from Main.java:254:in `internalRun'
    from Main.java:225:in `run'
    from Main.java:197:in `main'

This comment has been minimized.

Copy link
@mkristian

mkristian Jun 5, 2015

Author Member

my sloppy way would return new RegularFileResource(runtime == null ? null : runtime.getPosix(), f);

probably better will be to two RegularFileResource one with uses posix and one which has limited features.

This comment has been minimized.

Copy link
@kares

kares Jun 5, 2015

Member

I also got (new) failures e.g. under test/jruby/test_dir.rb some were commented out (but still work on jruby-1_7) but there's also at least one regression - which I did not understand since I kept the pieces (such as these) unable to follow merging changes under jruby-1_7. there might be some on my end as well as this is a WiP at https://github.com/jruby/jruby/tree/test-merge-1_7 (was planning to //cc you later to look at those, just wanted to let know that I'm somehow merging as most conflicts are on my plate anyways)

if (resource != null && resource.exists()) {
if (resource.isFile() || resource.isSymLink()) {
if (isXFlag()) {
// search for a shebang line and
// return the script between shebang and __END__ or CTRL-Z (0x1A)
return findScript(resource.inputStream());
}
return new BufferedInputStream(resource.inputStream(), 8192);
}
else {
throw new FileNotFoundException(script + " (Not a file)");
}
stream = new FileInputStream(file);
}

return new BufferedInputStream(stream, 8192);
else {
throw new FileNotFoundException(script + " (No such file or directory)");
}
}
} catch (IOException e) {
// We haven't found any file directly on the file system,
// now check for files inside the JARs.
InputStream is = getJarScriptSource(scriptFileName);
if (is != null) {
return new BufferedInputStream(is, 8129);
}
throw new MainExitException(1, "Error opening script file: " + e.getMessage());
}
}

private static InputStream findScript(File file) throws IOException {
private static InputStream findScript(InputStream is) throws IOException {
StringBuilder buf = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String currentLine = br.readLine();
while (currentLine != null && !isRubyShebangLine(currentLine)) {
currentLine = br.readLine();
@@ -457,27 +456,6 @@ private static InputStream findScript(File file) throws IOException {
return new BufferedInputStream(new ByteArrayInputStream(buf.toString().getBytes()), 8192);
}

private static InputStream getJarScriptSource(String scriptFileName) {
boolean looksLikeJarURL = scriptFileName.startsWith("file:") && scriptFileName.indexOf("!/") != -1;
if (!looksLikeJarURL) {
return null;
}

String before = scriptFileName.substring("file:".length(), scriptFileName.indexOf("!/"));
String after = scriptFileName.substring(scriptFileName.indexOf("!/") + 2);

try {
JarFile jFile = new JarFile(before);
JarEntry entry = jFile.getJarEntry(after);

if (entry != null && !entry.isDirectory()) {
return jFile.getInputStream(entry);
}
} catch (IOException ignored) {
}
return null;
}

public String displayedFileName() {
if (hasInlineScript) {
if (scriptFileName != null) {
5 changes: 3 additions & 2 deletions core/src/main/java/org/jruby/util/ClasspathResource.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@

public class ClasspathResource extends AbstractFileResource {

public static final String CLASSPATH = "classpath:/";
public static final String CLASSPATH = "classpath:";

private final String uri;

@@ -28,7 +28,8 @@ public class ClasspathResource extends AbstractFileResource {
}

public static URL getResourceURL(String pathname) {
String path = pathname.substring(CLASSPATH.length() );
String path = pathname.substring(CLASSPATH.length());
if (path.startsWith("/")) path = path.substring(1);
// this is the J2EE case
URL url = Thread.currentThread().getContextClassLoader().getResource(path);
if ( url != null ) {
13 changes: 9 additions & 4 deletions core/src/main/java/org/jruby/util/URLResource.java
Original file line number Diff line number Diff line change
@@ -148,10 +148,8 @@ public ChannelDescriptor openDescriptor(ModeFlags flags, int perm) throws Resour
return new ChannelDescriptor(inputStream(), flags);
}

public static FileResource createClassloaderURI(Ruby runtime, String pathname) {
// retrieve the classloader from the runtime if available otherwise mimic how the runtime got its classloader and
// take this
ClassLoader cl = runtime != null ? runtime.getJRubyClassLoader() : URLResource.class.getClassLoader();
public static FileResource create(ClassLoader cl, String pathname) {
// fall back on thread context classloader
if (cl == null ) {
cl = Thread.currentThread().getContextClassLoader();
}
@@ -169,6 +167,13 @@ public static FileResource createClassloaderURI(Ruby runtime, String pathname) {
files);
}

public static FileResource createClassloaderURI(Ruby runtime, String pathname) {
// retrieve the classloader from the runtime if available otherwise mimic how the runtime got its classloader and
// take this
ClassLoader cl = runtime != null ? runtime.getJRubyClassLoader() : URLResource.class.getClassLoader();
return create(cl, pathname);
}

public static FileResource create(Ruby runtime, String pathname)
{
if (!pathname.startsWith(URI)) {
16 changes: 8 additions & 8 deletions core/src/test/java/org/jruby/util/URLResourceTest.java
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ public class URLResourceTest extends TestCase {

public void testDirectory(){
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir" ).toExternalForm();
FileResource resource = URLResource.create( null, "uri:" + uri);
FileResource resource = URLResource.create((ClassLoader) null, "uri:" + uri);

assertNotNull(resource );
assertFalse(resource.isFile());
@@ -20,7 +20,7 @@ public void testDirectory(){

public void testNoneDirectory(){
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir/dir_without_listing" ).toExternalForm();
FileResource resource = URLResource.create( null, "uri:" + uri);
FileResource resource = URLResource.create((ClassLoader) null, "uri:" + uri);

assertNotNull(resource );
// you can open streams on file-system directories
@@ -32,7 +32,7 @@ public void testNoneDirectory(){

public void testFile(){
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir/.jrubydir" ).toExternalForm();
FileResource resource = URLResource.create( null, "uri:" + uri);
FileResource resource = URLResource.create((ClassLoader) null, "uri:" + uri);

assertNotNull(resource );
// you can open streams on file-system directories
@@ -44,7 +44,7 @@ public void testFile(){

public void testNonExistingFile(){
String uri = Thread.currentThread().getContextClassLoader().getResource( "somedir" ).toExternalForm();
FileResource resource = URLResource.create( null, "uri:" + uri + "/not_there");
FileResource resource = URLResource.create((ClassLoader) null, "uri:" + uri + "/not_there");

assertNotNull(resource );
assertFalse(resource.isFile());
@@ -55,7 +55,7 @@ public void testNonExistingFile(){

public void testDirectoryClassloader()
{
FileResource resource = URLResource.create( null, "uri:classloader:/somedir");
FileResource resource = URLResource.create((ClassLoader) null, "uri:classloader:/somedir");

assertNotNull( resource );
assertFalse( resource.isFile() );
@@ -68,7 +68,7 @@ public void testDirectoryClassloader()

public void testNoneDirectoryClassloader()
{
FileResource resource = URLResource.create( null, "uri:classloader:/somedir/dir_without_listing");
FileResource resource = URLResource.create((ClassLoader) null, "uri:classloader:/somedir/dir_without_listing");

assertNotNull( resource );
// you can open streams on file-system directories
@@ -80,7 +80,7 @@ public void testNoneDirectoryClassloader()

public void testFileClassloader()
{
FileResource resource = URLResource.create( null, "uri:classloader:/somedir/.jrubydir" );
FileResource resource = URLResource.create((ClassLoader) null, "uri:classloader:/somedir/.jrubydir" );

assertNotNull( resource );
// you can open streams on file-system directories
@@ -92,7 +92,7 @@ public void testFileClassloader()

public void testNonExistingFileClassloader()
{
FileResource resource = URLResource.create( null, "uri:classloader:/somedir/not_there" );
FileResource resource = URLResource.create((ClassLoader) null, "uri:classloader:/somedir/not_there" );

assertNotNull( resource );
assertFalse( resource.isFile() );
154 changes: 150 additions & 4 deletions maven/jruby/src/it/integrity/pom.xml
Original file line number Diff line number Diff line change
@@ -157,8 +157,154 @@
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<execution>
<id>jruby classpath:hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>classpath:hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby classpath:/hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>classpath:/hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby uri:classloader:/hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>uri:classloader:/hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby uri:classloader://hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>uri:classloader://hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby -C uri:classloader:// hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>-C</argument>
<argument>uri:classloader://</argument>
<argument>hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby jar:file://hello.jar!/helo.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>jar:file://${project.build.outputDirectory}/hello.jar!/helo.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby file://hello.jar!/helo.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>file://${project.build.outputDirectory}/hello.jar!/helo.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby file://hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>file://${project.build.outputDirectory}/hello.rb</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>jruby hello.rb</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath/>
<argument>org.jruby.Main</argument>
<argument>${project.build.outputDirectory}/hello.rb</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Binary file not shown.
1 change: 1 addition & 0 deletions maven/jruby/src/it/integrity/src/main/resources/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "hello #{__FILE__}"
23 changes: 23 additions & 0 deletions maven/jruby/src/it/integrity/verify.bsh
Original file line number Diff line number Diff line change
@@ -43,3 +43,26 @@ if ( !log.contains( expected ) )
{
throw new RuntimeException( "log file does not contain '" + expected + "'" );
}

// we are already OK if those scripts execute and there a duplicates which do
// get checked here
expected = "hello classpath:hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "hello classpath:/hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "hello uri:classloader:/hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "hello uri:classloader://hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "hello hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "maven/jruby/target/it/integrity/target/classes/hello.jar!/helo.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

expected = "maven/jruby/target/it/integrity/target/classes/hello.rb";
if ( !log.contains( expected ) ) throw new RuntimeException( "log file does not contain '" + expected + "'" );

0 comments on commit 4da7547

Please sign in to comment.