org.jruby.exceptions.RaiseException Java Examples

The following examples show how to use org.jruby.exceptions.RaiseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RubyScript.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private void loadScript(final Writer out, final Writer err, boolean isDebugging) {
    try {
        interpreter = RubyInterpreters.get(getInitRuby(out, err));
        defineVariable("marathon_script_handle", this);
        moduleList = new ModuleList(interpreter, Constants.getMarathonDirectoriesAsStringArray(Constants.PROP_MODULE_DIRS));
        loadAssertionProviders();
        defineVariable("test_file", filename);
        defineVariable("test_name", getTestName());
        defineVariable("project_dir", System.getProperty(Constants.PROP_PROJECT_DIR));
        defineVariable("marathon_home", System.getProperty(Constants.PROP_HOME));
        defineVariable("marathon_project_name", System.getProperty(Constants.PROP_PROJECT_NAME));
        defineVariable("marathon_project_dir", System.getProperty(Constants.PROP_PROJECT_DIR));
        defineVariable("marathon_fixture_dir", System.getProperty("marathon.fixture.dir"));
        defineVariable("marathon_test_dir", System.getProperty(Constants.PROP_TEST_DIR));
        if (dataVariables != null) {
            setDataVariables(dataVariables);
        }
        interpreter.executeScript(script, filename);
    } catch (RaiseException e) {
        throw new ScriptException(e.getException().toString(), e);
    } catch (Throwable t) {
        throw new ScriptException(t.getMessage(), t);
    }
}
 
Example #2
Source File: JRubyScriptUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	if (ReflectionUtils.isEqualsMethod(method)) {
		return (isProxyForSameRubyObject(args[0]));
	}
	else if (ReflectionUtils.isHashCodeMethod(method)) {
		return this.rubyObject.hashCode();
	}
	else if (ReflectionUtils.isToStringMethod(method)) {
		String toStringResult = this.rubyObject.toString();
		if (!StringUtils.hasText(toStringResult)) {
			toStringResult = ObjectUtils.identityToString(this.rubyObject);
		}
		return "JRuby object [" + toStringResult + "]";
	}
	try {
		IRubyObject[] rubyArgs = convertToRuby(args);
		IRubyObject rubyResult =
				this.rubyObject.callMethod(this.ruby.getCurrentContext(), method.getName(), rubyArgs);
		return convertFromRuby(rubyResult, method.getReturnType());
	}
	catch (RaiseException ex) {
		throw new JRubyExecutionException(ex);
	}
}
 
Example #3
Source File: JRubyScriptUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
	if (ReflectionUtils.isEqualsMethod(method)) {
		return (isProxyForSameRubyObject(args[0]));
	}
	else if (ReflectionUtils.isHashCodeMethod(method)) {
		return this.rubyObject.hashCode();
	}
	else if (ReflectionUtils.isToStringMethod(method)) {
		String toStringResult = this.rubyObject.toString();
		if (!StringUtils.hasText(toStringResult)) {
			toStringResult = ObjectUtils.identityToString(this.rubyObject);
		}
		return "JRuby object [" + toStringResult + "]";
	}
	try {
		IRubyObject[] rubyArgs = convertToRuby(args);
		IRubyObject rubyResult =
				this.rubyObject.callMethod(this.ruby.getCurrentContext(), method.getName(), rubyArgs);
		return convertFromRuby(rubyResult, method.getReturnType());
	}
	catch (RaiseException ex) {
		throw new JRubyExecutionException(ex);
	}
}
 
Example #4
Source File: JavaDissectorLibrary.java    From logstash-filter-dissect with Apache License 2.0 6 votes vote down vote up
@JRubyMethod(name = "initialize", required = 2, optional = 2)
public IRubyObject rubyInitialize(final ThreadContext ctx, final IRubyObject[] args) {
    final Ruby ruby = ctx.runtime;
    try {
        dissectors = DissectPair.createArrayFromHash((RubyHash) args[0]);
    } catch (final InvalidFieldException e) {
        throw new RaiseException(e, JavaDissectorLibrary.NativeExceptions.newFieldFormatError(ruby, e));
    }
    plugin = (RubyObject) args[1];
    pluginMetaClass = plugin.getMetaClass();
    conversions = ConvertPair.createArrayFromHash((RubyHash) args[2]);
    for (final ConvertPair convertPair : conversions) {
        if (convertPair.converter().isInvalid()) {
            final RubyClass klass = ruby.getModule("LogStash").getClass("ConvertDatatypeFormatError");
            final String errorMessage = String.format("Dissector datatype conversion, datatype not supported: %s", convertPair.type());
            throw new RaiseException(ruby, klass, errorMessage, true);
        }
    }
    runMatched = args[3] == null || args[3].isTrue();
    failureTags = fetchFailureTags(ctx);
    return ctx.nil;
}
 
Example #5
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
int rubyCompare(RubyObject l, RubyObject r) {
    ThreadContext context = l.getMetaClass().getRuntime().getCurrentContext();
    IRubyObject result;
    try {
        result = l.callMethod(context, "<=>", r);
    } catch (RaiseException e) {
        // handle objects "lying" about responding to <=>, ie: an Array containing non-comparable keys
        if (context.runtime.getNoMethodError().isInstance(e.getException())) {
            return 0;
        }
        throw e;
    }

    return result.isNil() ? 0 : RubyNumeric.num2int(result.convertToInteger());
}
 
Example #6
Source File: ConcurrentHashMapV8.java    From thread_safe with Apache License 2.0 5 votes vote down vote up
int rubyCompare(RubyObject l, RubyObject r) {
    ThreadContext context = l.getMetaClass().getRuntime().getCurrentContext();
    IRubyObject result;
    try {
        result = l.callMethod(context, "<=>", r);
    } catch (RaiseException e) {
        // handle objects "lying" about responding to <=>, ie: an Array containing non-comparable keys
        if (context.runtime.getNoMethodError().isInstance(e.getException())) {
            return 0;
        }
        throw e;
    }

    return result.isNil() ? 0 : RubyNumeric.num2int(result.convertToInteger());
}
 
Example #7
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertFile(File file, Map<String, Object> options, Class<T> expectedResult) {

    this.rubyGemsPreloader.preloadRequiredLibraries(options);

    logger.fine(String.join(" ", AsciidoctorUtils.toAsciidoctorCommand(options, file.getAbsolutePath())));

    String currentDirectory = rubyRuntime.getCurrentDirectory();

    if (options.containsKey(Options.BASEDIR)) {
        rubyRuntime.setCurrentDirectory((String) options.get(Options.BASEDIR));
    }

    final Object toFileOption = options.get(Options.TO_FILE);
    if (toFileOption instanceof OutputStream) {
        options.put(Options.TO_FILE, RubyOutputStreamWrapper.wrap(getRubyRuntime(), (OutputStream) toFileOption));
    }

    RubyHash rubyHash = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);

    try {
        IRubyObject object = getAsciidoctorModule().callMethod("convert_file",
                rubyRuntime.newString(file.getAbsolutePath()), rubyHash);
        if (NodeConverter.NodeType.DOCUMENT_CLASS.isInstance(object)) {
            // If a document is rendered to a file Asciidoctor returns the document, we return null
            return null;
        }
        return RubyUtils.rubyToJava(rubyRuntime, object, expectedResult);
    } catch (RaiseException e) {
        logger.severe(e.getMessage());

        throw new AsciidoctorCoreException(e);
    } finally {
        // we restore current directory to its original value.
        rubyRuntime.setCurrentDirectory(currentDirectory);
    }
}
 
Example #8
Source File: JRubyRackBodyIterator.java    From rack-servlet with Apache License 2.0 5 votes vote down vote up
@Override protected byte[] computeNext() {
  try {
    return enumerator.callMethod(threadContext, "next").convertToString().getBytes();
  } catch (RaiseException e) {
    close();
    return endOfData();
  }
}
 
Example #9
Source File: JRubyAsciidoctor.java    From asciidoctorj with Apache License 2.0 4 votes vote down vote up
public <T> T convert(String content, Map<String, Object> options, Class<T> expectedResult) {

        this.rubyGemsPreloader.preloadRequiredLibraries(options);

        logger.fine(String.join(" ", AsciidoctorUtils.toAsciidoctorCommand(options, "-")));

        if (AsciidoctorUtils.isOptionWithAttribute(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) {
            logger.fine("In order to use Pygments with Asciidoctor, you need to install Pygments (and Python, if you don't have it yet). Read http://asciidoctor.org/news/#syntax-highlighting-with-pygments.");
        }

        String currentDirectory = rubyRuntime.getCurrentDirectory();

        if (options.containsKey(Options.BASEDIR)) {
            rubyRuntime.setCurrentDirectory((String) options.get(Options.BASEDIR));
        }

        final Object toFileOption = options.get(Options.TO_FILE);
        if (toFileOption instanceof OutputStream) {
            options.put(Options.TO_FILE, RubyOutputStreamWrapper.wrap(getRubyRuntime(), (OutputStream) toFileOption));
        }

        RubyHash rubyHash = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);

        try {

            IRubyObject object = getAsciidoctorModule().callMethod("convert",
                    rubyRuntime.newString(content), rubyHash);
            if (NodeConverter.NodeType.DOCUMENT_CLASS.isInstance(object)) {
                // If a document is rendered to a file Asciidoctor returns the document, we return null
                return null;
            }
            return RubyUtils.rubyToJava(rubyRuntime, object, expectedResult);
        } catch (RaiseException e) {
            logger.severe(e.getException().getClass().getCanonicalName());
            throw new AsciidoctorCoreException(e);
        } finally {
            // we restore current directory to its original value.
            rubyRuntime.setCurrentDirectory(currentDirectory);
        }

    }
 
Example #10
Source File: JRubyScriptUtils.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Create a new {@code JRubyException},
 * wrapping the given JRuby {@code RaiseException}.
 * @param ex the cause (must not be {@code null})
 */
public JRubyExecutionException(RaiseException ex) {
	super(ex.getMessage(), ex);
}
 
Example #11
Source File: JRubyScriptUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new {@code JRubyException},
 * wrapping the given JRuby {@code RaiseException}.
 * @param ex the cause (must not be {@code null})
 */
public JRubyExecutionException(RaiseException ex) {
	super(ex.getMessage(), ex);
}