Search in sources :

Example 56 with ScriptException

use of javax.script.ScriptException in project enumerable by hraberg.

the class JRubyTestBase method testUnit.

void testUnit(String file, String testClass) throws ScriptException {
    StringWriter writer = new StringWriter();
    Writer originalWriter = rb.getContext().getWriter();
    rb.getContext().setWriter(writer);
    try {
        require(file);
        require("test/unit/ui/console/testrunner");
        beforeRunningTestUnit();
        eval("r = Test::Unit::UI::Console::TestRunner.run(" + testClass + ")");
        eval("raise r.to_s unless r.passed?");
        if (debug)
            out.println(writer);
    } catch (ScriptException e) {
        out.println(writer);
        throw uncheck(e);
    } finally {
        rb.getContext().setWriter(originalWriter);
    }
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 57 with ScriptException

use of javax.script.ScriptException in project enumerable by hraberg.

the class RubySpecTestBase method mspec.

void mspec(List<String> files) throws Exception {
    StringWriter stdout = new StringWriter();
    StringWriter stderr = new StringWriter();
    Writer originalOut = rb.getContext().getWriter();
    Writer originalErr = rb.getContext().getErrorWriter();
    if (!specdoc)
        rb.getContext().setWriter(stdout);
    if (!debug)
        rb.getContext().setErrorWriter(stderr);
    try {
        // We need to trick MSpec into thinking we're running a real ruby
        eval("RUBY_EXE = '/usr/bin/jruby'");
        // While telling it we're not, to skip specs for our "platform"
        eval("RUBY_PLATFORM = 'enumerable_java'");
        // We support Enumerable from 1.8.8
        eval("RUBY_VERSION = '1.8.8'");
        require("mspec");
        require("mspec/utils/script");
        // Identity won't work as JRuby will turn Ruby objects into Java
        // and then back again.
        eval("class EqualMatcher; def matches?(actual); @actual = actual; @actual == @expected; end; end");
        eval("formatter = SpecdocFormatter.new; formatter.register;");
        eval("MSpec.store :formatter, formatter");
        eval("MSpec.register_files " + files);
        eval("MSpec.process");
        try {
            eval("raise formatter.exceptions[0] unless MSpec.exit_code == 0");
        } catch (RaiseException e) {
            try {
                fail(e.getException().message.asJavaString());
            } catch (AssertionError error) {
                error.setStackTrace(e.getStackTrace());
                throw error;
            }
        }
    } catch (ScriptException e) {
        out.println(stdout.toString());
        err.println(stderr.toString());
        throw uncheck(e);
    } finally {
        rb.getContext().setWriter(originalOut);
        rb.getContext().setErrorWriter(originalErr);
        eval("MSpec.unregister :exception, formatter; MSpec.unregister :before, formatter; " + "MSpec.unregister :after, formatter; MSpec.unregister :finish, formatter; " + "MSpec.unregister :enter, formatter; MSpec.register_exit(nil); " + "MSpec.clear_current; MSpec.clear_modes; MSpec.clear_expectations");
    }
}
Also used : ScriptException(javax.script.ScriptException) StringWriter(java.io.StringWriter) RaiseException(org.jruby.exceptions.RaiseException) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 58 with ScriptException

use of javax.script.ScriptException in project cryptomator by cryptomator.

the class ExitUtil method showTrayNotification.

private void showTrayNotification(TrayIcon trayIcon) {
    int remainingTrayNotification = settings.numTrayNotifications().get();
    if (remainingTrayNotification <= 0) {
        return;
    } else {
        settings.numTrayNotifications().set(remainingTrayNotification - 1);
    }
    final Runnable notificationCmd;
    if (SystemUtils.IS_OS_MAC_OSX) {
        final String title = localization.getString("tray.infoMsg.title");
        final String msg = localization.getString("tray.infoMsg.msg.osx");
        final String notificationCenterAppleScript = String.format("display notification \"%s\" with title \"%s\"", msg, title);
        notificationCmd = () -> {
            try {
                final ScriptEngineManager mgr = new ScriptEngineManager();
                final ScriptEngine engine = mgr.getEngineByName("AppleScriptEngine");
                if (engine != null) {
                    engine.eval(notificationCenterAppleScript);
                } else {
                    Runtime.getRuntime().exec(new String[] { "/usr/bin/osascript", "-e", notificationCenterAppleScript });
                }
            } catch (ScriptException | IOException e) {
            // ignore, user will notice the tray icon anyway.
            }
        };
    } else {
        final String title = localization.getString("tray.infoMsg.title");
        final String msg = localization.getString("tray.infoMsg.msg");
        notificationCmd = () -> {
            trayIcon.displayMessage(title, msg, MessageType.INFO);
        };
    }
    SwingUtilities.invokeLater(() -> {
        notificationCmd.run();
    });
}
Also used : ScriptException(javax.script.ScriptException) ScriptEngineManager(javax.script.ScriptEngineManager) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 59 with ScriptException

use of javax.script.ScriptException in project gradle by gradle.

the class AppleScriptBackedGrowlAnnouncer method send.

@Override
public void send(String title, String message) {
    String isRunning = "\ntell application \"System Events\"\n" + "set isRunning to count of (every process whose bundle identifier is \"com.Growl.GrowlHelperApp\") > 0\n" + "end tell\n" + "return isRunning\n";
    try {
        Object value = engine.eval(isRunning);
        if (value.equals(0)) {
            throw new AnnouncerUnavailableException("Growl is not running.");
        }
        final File icon = iconProvider.getIcon(48, 48);
        String iconDef = icon != null ? "image from location ((POSIX file \"" + icon.getAbsolutePath() + "\") as string) as alias" : "";
        String script = "\ntell application id \"com.Growl.GrowlHelperApp\"\n" + "register as application \"Gradle\" all notifications {\"Build Notification\"} default notifications {\"Build Notification\"}\n" + "notify with name \"Build Notification\" title \"" + escape(title) + "\" description \"" + escape(message) + "\" application name \"Gradle\"" + iconDef + "\nend tell\n";
        engine.eval(script);
    } catch (ScriptException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) AnnouncerUnavailableException(org.gradle.api.plugins.announce.internal.AnnouncerUnavailableException) File(java.io.File)

Example 60 with ScriptException

use of javax.script.ScriptException in project groovy-core by groovy.

the class GroovyScriptEngineImpl method readFully.

private String readFully(Reader reader) throws ScriptException {
    // 8K at a time
    char[] arr = new char[8 * 1024];
    StringBuilder buf = new StringBuilder();
    int numChars;
    try {
        while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
            buf.append(arr, 0, numChars);
        }
    } catch (IOException exp) {
        throw new ScriptException(exp);
    }
    return buf.toString();
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException)

Aggregations

ScriptException (javax.script.ScriptException)106 ScriptEngine (javax.script.ScriptEngine)42 IOException (java.io.IOException)41 Bindings (javax.script.Bindings)30 ScriptEngineManager (javax.script.ScriptEngineManager)20 InputStreamReader (java.io.InputStreamReader)12 CompiledScript (javax.script.CompiledScript)12 Map (java.util.Map)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)11 SimpleBindings (javax.script.SimpleBindings)10 File (java.io.File)8 Reader (java.io.Reader)7 Writer (java.io.Writer)7 HashMap (java.util.HashMap)7 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5