Search in sources :

Example 6 with ScriptException

use of javax.script.ScriptException in project zipkin by openzipkin.

the class ZipkinMySQLContainer method containerIsStarted.

@Override
protected void containerIsStarted(InspectContainerResponse containerInfo) {
    String[] scripts = { // Drop all previously created tables in zipkin.*
    "drop_zipkin_tables.sql", // Populate the schema
    "mysql.sql" };
    try (Connection connection = dataSource.getConnection()) {
        for (String script : scripts) {
            URL scriptURL = Resources.getResource(script);
            String statements = Resources.toString(scriptURL, Charset.defaultCharset());
            ScriptUtils.executeSqlScript(connection, script, statements);
        }
    } catch (SQLException | IOException | ScriptException e) {
        throw new RuntimeException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) IOException(java.io.IOException) URL(java.net.URL)

Example 7 with ScriptException

use of javax.script.ScriptException in project camel by apache.

the class ScriptBuilder method evaluateScript.

protected Object evaluateScript(Exchange exchange) {
    try {
        if (reuseScriptEngine(exchange)) {
            // It's not safe to do the evaluation with a single scriptEngine
            synchronized (this) {
                LOG.debug("Calling doEvaluateScript without creating a new scriptEngine");
                return doEvaluateScript(exchange, scriptEngine);
            }
        } else {
            LOG.debug("Calling doEvaluateScript with a new scriptEngine");
            // get a new engine which we must for each exchange
            ScriptEngine engine = scriptEngineFactory.getScriptEngine();
            return doEvaluateScript(exchange, engine);
        }
    } catch (ScriptException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Script evaluation failed: " + e.getMessage(), e);
        }
        if (e.getCause() != null) {
            throw createScriptEvaluationException(e.getCause());
        } else {
            throw createScriptEvaluationException(e);
        }
    } catch (IOException e) {
        throw createScriptEvaluationException(e);
    }
}
Also used : ScriptException(javax.script.ScriptException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine)

Example 8 with ScriptException

use of javax.script.ScriptException in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngineTest method untestUsingClassesIsGood.

public void untestUsingClassesIsGood() throws ScriptException {
    GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
    final Graph g = TinkerGraphFactory.createTinkerGraph();
    final Bindings bindings = engine.createBindings();
    bindings.put("g", g);
    // works when it's all defined together
    assertEquals(true, engine.eval("class c { static def isVadas(v){v.name=='vadas'}};c.isVadas(g.v(2))", bindings));
    // let's reset this piece and make sure isVadas is not hanging around.
    engine = new GremlinGroovyScriptEngine();
    // validate that isVadas throws an exception since it is not defined
    try {
        engine.eval("c.isVadas(g.v(2))", bindings);
        // fail the test if the above doesn't throw an exception
        fail();
    } catch (Exception ex) {
    // this is good...we want this. it means isVadas isn't hanging about
    }
    // now...define the class separately on its own in one script...
    // HERE'S and AWKWARD BIT.........
    // YOU HAVE TO END WITH: null;
    // ....OR ELSE YOU GET:
    // javax.script.ScriptException: javax.script.ScriptException:
    // org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: c.main()
    // is applicable for argument types: ([Ljava.lang.String;) values: [[]]
    // WOULD BE NICE IF WE DIDN'T HAVE TO DO THAT
    engine.eval("class c { static def isVadas(v){v.name=='vadas'}};null;", bindings);
    // make sure the class works on its own...this generates: groovy.lang.MissingPropertyException: No such property: c for class: Script2
    assertEquals(true, engine.eval("c.isVadas(g.v(2))", bindings));
}
Also used : TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException)

Example 9 with ScriptException

use of javax.script.ScriptException in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngineTest method testThreadSafetyOnEngine.

public void testThreadSafetyOnEngine() throws Exception {
    final ScriptEngine engine = new GremlinGroovyScriptEngine(10);
    int runs = 500;
    final CountDownLatch latch = new CountDownLatch(runs);
    final List<String> names = Arrays.asList("marko", "peter", "josh", "vadas", "stephen", "pavel", "matthias");
    final Random random = new Random();
    for (int i = 0; i < runs; i++) {
        new Thread() {

            public void run() {
                String name = names.get(random.nextInt(names.size() - 1));
                try {
                    final Bindings bindings = engine.createBindings();
                    bindings.put("g", TinkerGraphFactory.createTinkerGraph());
                    bindings.put("name", name);
                    final Object result = engine.eval("pipe = g.V('name',name); if(pipe.hasNext()) { pipe.out.count() } else { null }", bindings);
                    if (name.equals("stephen") || name.equals("pavel") || name.equals("matthias"))
                        assertNull(result);
                    else
                        assertNotNull(result);
                } catch (ScriptException e) {
                    //System.out.println(e);
                    assertFalse(true);
                }
                latch.countDown();
            }
        }.start();
    }
    latch.await();
}
Also used : ScriptException(javax.script.ScriptException) CountDownLatch(java.util.concurrent.CountDownLatch) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 10 with ScriptException

use of javax.script.ScriptException in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngine method readFully.

private String readFully(final Reader reader) throws ScriptException {
    char[] arr = new char[8192];
    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)98 IOException (java.io.IOException)41 ScriptEngine (javax.script.ScriptEngine)36 Bindings (javax.script.Bindings)27 ScriptEngineManager (javax.script.ScriptEngineManager)17 CompiledScript (javax.script.CompiledScript)12 InputStreamReader (java.io.InputStreamReader)11 Invocable (javax.script.Invocable)11 ScriptContext (javax.script.ScriptContext)10 Map (java.util.Map)9 SimpleBindings (javax.script.SimpleBindings)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)7 File (java.io.File)6 Writer (java.io.Writer)6 ArrayList (java.util.ArrayList)6 SlingBindings (org.apache.sling.api.scripting.SlingBindings)6 MissingMethodException (groovy.lang.MissingMethodException)5 MissingPropertyException (groovy.lang.MissingPropertyException)5 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)4