Esempio n. 1
0
  private void setupJRuby(Activity startActivity) {
    System.setProperty("jruby.bytecode.version", "1.5");

    // enable proxy classes
    System.setProperty(
        "jruby.ji.proxyClassFactory", "com.rickbutton.rubydroid.DalvikProxyClassFactory");
    System.setProperty("jruby.ji.upper.case.package.name.allowed", "true");
    System.setProperty("jruby.class.cache.path", appContext.getDir("dex", 0).getAbsolutePath());

    // setup jruby home
    String apkName = getApkName();
    String jrubyHome = "file:" + apkName + "!/jruby.home";
    System.setProperty("jruby.home", jrubyHome);

    // configure jruby
    System.setProperty("jruby.compile.mode", "OFF"); // OFF OFFIR JITIR? FORCE FORCEIR
    // System.setProperty("jruby.compile.backend", "DALVIK");
    System.setProperty("jruby.bytecode.version", "1.6");
    System.setProperty("jruby.interfaces.useProxy", "true");
    System.setProperty("jruby.management.enabled", "false");
    System.setProperty("jruby.objectspace.enabled", "false");
    System.setProperty("jruby.thread.pooling", "true");
    System.setProperty("jruby.native.enabled", "false");
    System.setProperty("jruby.ir.passes", "LocalOptimizationPass,DeadCodeElimination");
    System.setProperty("jruby.backtrace.style", "normal"); // normal raw full mri

    ClassLoader loader = new PathClassLoader(apkName, RubySystem.class.getClassLoader());

    // disable gems
    RubyInstanceConfig config = new RubyInstanceConfig();
    config.setDisableGems(true);
    config.setLoader(loader);
    Ruby.newInstance(config);

    container =
        new ScriptingContainer(LocalContextScope.SINGLETON, LocalVariableBehavior.PERSISTENT);
    container.setClassLoader(loader);

    Thread.currentThread().setContextClassLoader(loader);

    if (appContext.getFilesDir() != null) {
      container.setCurrentDirectory(appContext.getFilesDir().getPath());
    }

    container.put("$package_name", appContext.getPackageName());

    container.put("app_context", appContext);
    container.put("system", this);
    rubyContext = container.runScriptlet(PathType.CLASSPATH, "ruby/droid/init.rb");

    Object mainActivity = container.get("MainActivity");
    container.callMethod(rubyContext, "start_ruby_activity", mainActivity, startActivity);
  }
  /**
   * {@inheritDoc}
   *
   * <p>Latest method of invoking jruby script have been adapted from <a
   * href="http://wiki.jruby.org/wiki/Java_Integration" title="Click to visit JRuby Wiki"> JRuby
   * wiki:</a>
   *
   * @todo create a way to prevent initialization and shutdown with each script invocation.
   */
  protected PicoContainer createContainerFromScript(
      PicoContainer parentContainer, Object assemblyScope) {
    if (parentContainer == null) {
      parentContainer = new EmptyPicoContainer();
    }
    parentContainer =
        new DefaultClassLoadingPicoContainer(
            getClassLoader(), new DefaultPicoContainer(new Caching(), parentContainer));

    RubyInstanceConfig rubyConfig = new RubyInstanceConfig();
    rubyConfig.setLoader(this.getClassLoader());
    Ruby ruby = JavaEmbedUtils.initialize(Collections.EMPTY_LIST, rubyConfig);
    ruby.getLoadService().require("org/picocontainer/script/jruby/scriptedbuilder");
    ruby.defineReadonlyVariable("$parent", JavaEmbedUtils.javaToRuby(ruby, parentContainer));
    ruby.defineReadonlyVariable("$assembly_scope", JavaEmbedUtils.javaToRuby(ruby, assemblyScope));

    try {

      // IRubyObject result = ruby.executeScript(script);
      IRubyObject result = JavaEmbedUtils.newRuntimeAdapter().eval(ruby, script);
      return (PicoContainer) JavaEmbedUtils.rubyToJava(ruby, result, PicoContainer.class);
    } catch (RaiseException re) {
      if (re.getCause() instanceof ScriptedPicoContainerMarkupException) {
        throw (ScriptedPicoContainerMarkupException) re.getCause();
      }
      String message =
          (String) JavaEmbedUtils.rubyToJava(ruby, re.getException().message, String.class);
      if (message.startsWith(MARKUP_EXCEPTION_PREFIX)) {
        throw new ScriptedPicoContainerMarkupException(
            message.substring(MARKUP_EXCEPTION_PREFIX.length()));
      } else {
        throw new PicoCompositionException(message, re);
      }
    } finally {
      JavaEmbedUtils.terminate(ruby);
    }
  }