Ejemplo n.º 1
0
 private Object convertFromRuby(IRubyObject rubyResult, Class returnType) {
   Object result = JavaEmbedUtils.rubyToJava(this.ruby, rubyResult, returnType);
   if (result instanceof RubyArray && returnType.isArray()) {
     result = convertFromRubyArray(((RubyArray) result).toJavaArray(), returnType);
   }
   return result;
 }
  /**
   * {@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);
    }
  }
 public RackResponse call(final RackEnvironment env) {
   Ruby runtime = getRuntime();
   try {
     RackBaseInput io = createRackInput(runtime, env);
     try {
       IRubyObject servlet_env = JavaEmbedUtils.javaToRuby(runtime, env);
       adapter.setInstanceVariable(servlet_env, "@_io", io);
       IRubyObject response = __call(servlet_env);
       return (RackResponse) JavaEmbedUtils.rubyToJava(runtime, response, RackResponse.class);
     } finally {
       io.close();
     }
   } catch (IOException ex) {
     throw RaiseException.createNativeRaiseException(runtime, ex);
   }
 }
  private <T> T call(
      MethodType type,
      Class<T> returnType,
      Object receiver,
      String methodName,
      Block block,
      EmbedEvalUnit unit,
      Object... args) {
    if (methodName == null || methodName.length() == 0) {
      return null;
    }
    Ruby runtime = container.getProvider().getRuntime();
    RubyObject rubyReceiver = getReceiverObject(runtime, receiver);

    boolean sharing_variables = true;
    Object obj = container.getAttribute(AttributeName.SHARING_VARIABLES);
    if (obj != null && obj instanceof Boolean && ((Boolean) obj) == false) {
      sharing_variables = false;
    }
    try {
      if (sharing_variables) {
        ManyVarsDynamicScope scope;
        if (unit != null && unit.getScope() != null) scope = unit.getScope();
        else scope = EmbedRubyRuntimeAdapterImpl.getManyVarsDynamicScope(container, 0);
        container.getVarMap().inject(scope, 0, rubyReceiver);
        runtime.getCurrentContext().pushScope(scope);
      }
      IRubyObject result = callEachType(type, rubyReceiver, methodName, block, args);
      if (sharing_variables) {
        container.getVarMap().retrieve(rubyReceiver);
      }
      if (!(result instanceof RubyNil) && returnType != null) {
        Object ret = JavaEmbedUtils.rubyToJava(runtime, result, returnType);
        return ret != null ? returnType.cast(ret) : null;
      }
      return null;
    } catch (RaiseException e) {
      runtime.printError(e.getException());
      throw new InvokeFailedException(e.getMessage(), e);
    } catch (Throwable e) {
      throw new InvokeFailedException(e);
    } finally {
      if (sharing_variables) {
        runtime.getCurrentContext().popScope();
      }
    }
  }