Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Embedding JRuby - Profiles prevent loading external scripts.  (Read 451 times)
0 Members and 1 Guest are viewing this topic.
Offline quew8

JGO Coder


Medals: 31



« Posted 2013-07-29 13:03:19 »

So I am embedding JRuby using the org.jruby.embed.* stuff (I think it used to be called RedBridge). Everything went fine until I tried to set the profile to prevent access to the file system and to the rest of the JVM from jruby. Using the Profile.NO_FILE_CLASS (I have also tried my own custom profile) I get a NullPointerException when I try to load external scripts (through runScriplet("require '" + name + "'"); )

Here is the code for setting up the ScriptingContainer
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
this.console = console;
this.stdout = new PrintWriter(new BufferedWriter(console.getInput()));
this.stdin = new BufferedReader(console.getOutput());

this.scripter = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
this.scripter.setProfile(Profile.NO_FILE_CLASS);
System.out.println(this.scripter.getSupportedRubyVersion());
this.scripter.setLoadPaths(Arrays.asList(
          new String[]{
                GENERIC_COMPUTER.toString() + File.separator + "include",
                GENERIC_COMPUTER.toString() + File.separator + "bin",
            }
            ));
this.scripter.setOutput(stdout);
this.scripter.setError(stdout);
this.scripter.setInput(stdin);
includeDir(new File(GENERIC_COMPUTER.toString() + File.separator + "include"));
nativeCommandList = includeDir(new File(GENERIC_COMPUTER.toString() + File.separator + "bin"));
//this.scripter.setProfile(Profile.NO_FILE_CLASS);


And here is the code to include files:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
private String[] includeDir(File file) {
        File[] files = file.listFiles();
        String[] names = new String[files.length];
        for(int i = 0; i < files.length; i++) {
            if(files[i].isFile()) {
                String scriptName = files[i].getName().substring(0, files[i].getName().indexOf("."));
                names[i] = scriptName;
                include(scriptName);
            } else {
                includeDir(files[i]);
            }
        }
        return names;
    }
   
    private void include(String s) {
        this.scripter.runScriptlet("require '" + s + "'");
    }


And here is the Stack Trace of the NPE:
Quote
Exception in thread "main" java.lang.NullPointerException
   at org.jruby.RubyFileStat.createFileStatClass(RubyFileStat.java:75)
   at org.jruby.Ruby.initCore(Ruby.java:1410)
   at org.jruby.Ruby.bootstrap(Ruby.java:1205)
   at org.jruby.Ruby.init(Ruby.java:1140)
   at org.jruby.Ruby.newInstance(Ruby.java:276)
   at org.jruby.embed.internal.SingletonLocalContextProvider.getRuntime(SingletonLocalContextProvider.java:95)
   at org.jruby.embed.internal.EmbedRubyRuntimeAdapterImpl.runParser(EmbedRubyRuntimeAdapterImpl.java:156)
   at org.jruby.embed.internal.EmbedRubyRuntimeAdapterImpl.parse(EmbedRubyRuntimeAdapterImpl.java:93)
   at org.jruby.embed.ScriptingContainer.parse(ScriptingContainer.java:1196)
   at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1256)
   at com.quew8.solar.Computer.include(Computer.java:196)
   at com.quew8.solar.Computer.includeDir(Computer.java:186)
   at com.quew8.solar.Computer.setIO(Computer.java:174)
   at com.quew8.solar.ComputerIO.setComputer(ComputerIO.java:71)
   at com.quew8.solar.ComputerIO.<init>(ComputerIO.java:62)
   at com.quew8.solar.Solar.init(Solar.java:168)
   at com.quew8.solar.Solar.main(Solar.java:338)
Java Result: 1

You can't set the profile after loading the scripts since settings can't be changed once the jruby runtime is initialized (which happens as soon as you run a script)

I'm looking for a way to include (require in ruby language) external scripts with the NO_FILE_CLASS profile (or any other that disallows file system access). Any and all suggestions are appreciated.

Thanks in advance
Online SHC
« Reply #1 - Posted 2013-07-29 13:39:15 »

I don't know any ruby but I think require is trying to access the filesystem to get the scripts. What if you remove require and instead pass the script through that runScriptlet() ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private void include(String s) {
    BufferedReader reader = new BufferedReader(new TextReader(new File(s)));
    String code = "";

    String line;
    while ( (line = reader.readLine()) != null)
    {
        code += line + "\n";
    }

    this.scripter.runScriptlet(code);
}

Offline quew8

JGO Coder


Medals: 31



« Reply #2 - Posted 2013-07-29 17:33:12 »

Require is the ruby equivalent of Java's import. It's not that I'm trying to run them, I want to make the contents available to other scripts.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (22 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!