Package javax.script

Examples of javax.script.Invocable


  }

  public Invocable engine() throws HttpException {
    ScriptEngineManager engineMgr = new ScriptEngineManager();
    ScriptEngine scriptEngine = engineMgr.getEngineByName("jython");
    Invocable invocableEngine = null;
    try {
      scriptEngine.eval(chargeScript);
      scriptEngine.put("contract", this);
      invocableEngine = (Invocable) scriptEngine;
    } catch (ScriptException e) {
View Full Code Here


            Object result = null;

            String methodName = method.getName();
            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
            if (bindings.containsKey(methodName)) {
                Invocable invocable;
                try {
                    invocable = (Invocable)scriptEngine;
                } catch (ClassCastException exception) {
                    throw new SerializationException(exception);
                }

                result = invocable.invokeFunction(methodName, args);
            }

            // If the function didn't return a value, return the default
            if (result == null) {
                Class<?> returnType = method.getReturnType();
View Full Code Here

      /* Initialize the script engine */
      ScriptEngineManager m = new ScriptEngineManager();
      ScriptEngine rubyEngine = m.getEngineByName("jruby");
      ScriptContext scriptContext = rubyEngine.getContext();
      Invocable rubyInvocableEngine = (Invocable) rubyEngine;

      /*
       * Sample 1 : Pass a parameter to the JRuby script that display the
       * value of the parameter passed
       */

      System.out.println("###SAMPLE 1");
      scriptContext.setAttribute("param_from_java", "MyParamValue",
          ScriptContext.ENGINE_SCOPE);
      scriptReader = new FileReader(
          "JRubyScripts/test_param_java_ruby.rb");
      rubyEngine.eval(scriptReader, scriptContext);
      scriptReader.close();

      /*
       * Sample 2 : Pass a parameter to the JRuby script that modify the
       * parameter and return it
       */

      System.out.println("###SAMPLE 2");
      scriptContext.setAttribute("param_from_java", "MyParamValue",
          ScriptContext.ENGINE_SCOPE);
      scriptReader = new FileReader(
          "JRubyScripts/test_param_java_ruby_with_return.rb");
      obj = rubyEngine.eval(scriptReader, scriptContext);
      scriptReader.close();
      System.out
          .printf(
              "Parameter before script call '%s' and after script call '%s'\n",
              "MyParamValue", obj);

      /*
       * Sample 3 : Call a JRuby script to obtain a object (thread
       * subclass defined in JRuby script)
       */

      System.out.println("###SAMPLE 3");
      scriptContext.removeAttribute("param_from_java",
          ScriptContext.ENGINE_SCOPE);
      scriptReader = new FileReader(
          "JRubyScripts/test_param_ruby_java_with_return.rb");
      obj = rubyEngine.eval(scriptReader, scriptContext);
      Thread thread = (Thread) obj;
      scriptReader.close();
      System.out.printf("Thread name : %s\n", thread.getName());
      thread.start();
      Thread.sleep(1000);// Let the thread finish...

      /*
       * Sample 4 : Call a specific function of a JRuby script
       */
      System.out.println("###SAMPLE 4");
      scriptReader = new FileReader("JRubyScripts/test_multifunctions.rb");
      // Evaluate (execute) the script. Once evaluated, any functions
      // in the script can be called with the invokeFunction method.
      rubyEngine.eval(scriptReader);
      // Call function
      obj = rubyInvocableEngine.invokeFunction("function_two",
          "HelloWord");
      // Call method (procedure)
      rubyInvocableEngine.invokeMethod("", "function_one", new Object[0]);
      scriptReader.close();
      System.out.printf("Return of the function '%s'\n", obj);

    } catch (ScriptException se) {
      System.out.println("########EXCEPTION");
View Full Code Here

        + "print( entries[entry].getValue());\n" + "}}"; //$NON-NLS-2$
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("keyOne", "valueOne"); //$NON-NLS-2$
    String functionName = "one";
    engine.eval(script);
    Invocable inv = (Invocable) engine;
    inv.invokeFunction(functionName, map);
  }
View Full Code Here

        + "print( entries[entry].getValue()[0]);\n" + "}}";
    HashMap<String, String[]> map = new HashMap<String, String[]>();
    map.put("keyOne", new String[] { "valueOne" });
    String functionName = "one";
    engine.eval(script);
    Invocable inv = (Invocable) engine;
    inv.invokeFunction(functionName, map);
  }
View Full Code Here

        this.baseFolder = ".";
    }

    public ColumnTransformer getColumnTransformer(String scriptName) {
        Reader content = load(COLUMN_SCRIPT_FOLDER, scriptName);
        Invocable invocable = (Invocable) engine;
        try {
            engine.eval(content);
        } catch (ScriptException ex) {
            throw new IllegalStateException("Cannot evaluate script", ex);
        }
        return invocable.getInterface(ColumnTransformer.class);
    }
View Full Code Here

    @Override
    public String process() throws Exception {
        Object result = compiledScript == null ? engine.eval(scriptContent) : compiledScript.eval();
        if (engine instanceof Invocable) {
            final Invocable invocable = (Invocable) engine;

            try {
                result = invocable.invokeFunction("process");
            } catch (final NoSuchMethodException e) {
                //ignore
            }
        }
View Full Code Here

            Object result = null;

            String methodName = method.getName();
            Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
            if (bindings.containsKey(methodName)) {
                Invocable invocable;
                try {
                    invocable = (Invocable)scriptEngine;
                } catch (ClassCastException exception) {
                    throw new SerializationException(exception);
                }

                result = invocable.invokeFunction(methodName, args);
            }

            // If the function didn't return a value, return the default
            if (result == null) {
                Class<?> returnType = method.getReturnType();
View Full Code Here

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName(language);
        try
        {
            engine.eval(script);
            Invocable inv = (Invocable)engine;
            TupleJoin join = inv.getInterface(TupleJoin.class);
            TupleMetadata outputMetadata =
                join.getMetadata(
                        (TupleMetadata)tuples1.getMetadataWrapper().getMetadata(),
                        (TupleMetadata)tuples2.getMetadataWrapper().getMetadata());
            Tuple tuple;
View Full Code Here

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName(language);
        try
        {
            engine.eval(script);
            Invocable inv = (Invocable)engine;
            TupleProject project = inv.getInterface(TupleProject.class);
            mOutput.write(ControlBlock.LIST_BEGIN);
            TupleMetadata outputMetadata =
                project.getMetadata((TupleMetadata)tuples.getMetadataWrapper().getMetadata());
            mOutput.write(outputMetadata);
            Tuple tuple;
View Full Code Here

TOP

Related Classes of javax.script.Invocable

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.