/** return a javascript engine as a Compilable */
 private static Compilable getCompilable(final ScriptEngine engine) {
   if (!(engine instanceof Compilable)) {
     throw new IllegalStateException(
         "The current javascript engine ("
             + engine.getClass()
             + ") cannot be cast to Compilable. "
             + "Do you use the SUN/Oracle Java Runtime ?");
   }
   return Compilable.class.cast(engine);
 }
Beispiel #2
0
  /**
   * Perform Script mediation
   *
   * @param synCtx the Synapse message context
   * @return the boolean result from the script invocation
   */
  public boolean mediate(MessageContext synCtx) {

    SynapseLog synLog = getLog(synCtx);

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug("Start : Script mediator");

      if (synLog.isTraceTraceEnabled()) {
        synLog.traceTrace("Message : " + synCtx.getEnvelope());
      }
    }

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug(
          "Scripting language : "
              + language
              + " source "
              + (key == null ? ": specified inline " : " loaded with key : " + key)
              + (function != null ? " function : " + function : ""));
    }

    boolean returnValue;
    if (multiThreadedEngine) {
      returnValue = invokeScript(synCtx);
    } else {
      // TODO: change to use a pool of script engines (requires an update to BSF)
      synchronized (scriptEngine.getClass()) {
        returnValue = invokeScript(synCtx);
      }
    }

    if (synLog.isTraceTraceEnabled()) {
      synLog.traceTrace("Result message after execution of script : " + synCtx.getEnvelope());
    }

    if (synLog.isTraceOrDebugEnabled()) {
      synLog.traceOrDebug("End : Script mediator return value : " + returnValue);
    }

    return returnValue;
  }
Beispiel #3
0
 public void ToJson() throws Exception {
   // 起動時にオプションを指定しなかった場合は、このサンプルデータを使用する。
   String script = gettingURL.getLine();
   if (script.length() > 0) {
     File f = new File(args[0]);
     if (f.exists() && f.isFile()) {
       // 起動時の第1引数がファイルならファイルから読み込み(java 6 対応版なので、try-with-resources
       // すら使えません。実際は、こんな書き方せずにちゃんとエラー処理してください)
       byte[] buf = new byte[new Long(f.length()).intValue()];
       FileInputStream fin = null;
       try {
         fin = new FileInputStream(f);
         fin.read(buf);
       } catch (Exception ex) {
         throw ex;
       } finally {
         if (fin != null) {
           fin.close();
         }
       }
       script =
           args.length > 1
               ? new String(buf, args[1])
               : new String(
                   buf); // ファイルの文字コードがシステムの文字コードと異なる場合は、第2引数で指定。例えば「UTF-8」や「JISAutoDetect」など。
     } else {
       script = args[0]; // ファイルでなければ、第1引数の文字列をそのまま JSON として扱う
     }
   }
   ScriptEngineManager manager = new ScriptEngineManager();
   ScriptEngine engine = manager.getEngineByName("JavaScript");
   // ScriptEngine の eval に JSON を渡す時は、括弧で囲まないと例外が発生します。eval
   // はセキュリティ的には好ましくないので、安全であることが不明なデータを扱うことは想定していません。
   Object obj = engine.eval(String.format("(%s)", script));
   // Rhino は、jdk1.6,7までの JavaScript エンジン。jdk1.8は「jdk.nashorn.api.scripting.NashornScriptEngine」
   Map<String, Object> map =
       jsonToMap(
           obj, engine.getClass().getName().equals("com.sun.script.javascript.RhinoScriptEngine"));
   System.out.println(map.toString());
 }