Java中通過指令碼引擎調用js函數

來源:互聯網
上載者:User

標籤:bsp   param   null   sage   chm   edit   jsf   err   factory   

 1 import java.io.*; 2 import javax.script.Invocable; 3 import javax.script.ScriptEngine; 4 import javax.script.ScriptEngineManager; 5 import javax.script.ScriptException; 6 import org.slf4j.Logger; 7 import org.slf4j.LoggerFactory; 8 import com.ppcredit.common.util.file.FileUtil; 9 10 /**11  * 公用的JS指令碼引擎工具類12  */13 public class CommonScriptEngineUtil {14     private static Logger log = LoggerFactory.getLogger(CommonScriptEngineUtil.class); // 日誌15     16     private static ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("javascript");17     18     private static Invocable invoke = null;19     20     /**21      * 調用js中的函數22      * @param jsName js檔案名稱23      * @param functionName 調用的函數名稱24      * @param param 調用js函數的參數25      * @return26      */27     public static  String invokeJSFuntion(String jsName, String functionName, String param){28         initJS(jsName);29         synchronized (invoke) {30           String result = param;31           if(invoke != null){32               try {33                   long startTime = System.currentTimeMillis();34                   String c = (String)invoke.invokeFunction(functionName, param);35                   long endTime = System.currentTimeMillis();36                   log.info("invokeJSFuntion 耗時 >>>>" + (endTime-startTime));37                   result = c;38               } catch (NoSuchMethodException e) {39                   log.error(e.getMessage());40               } catch (ScriptException e) {41                   //如果出錯就重新載入42                   initJS(jsName);43                   log.error(e.getMessage());44               } 45           }46           return result;47         }48     }49     50     51     private static void initJS(String jsName){52         String basePath = FileUtil.getAppPath(CommonScriptEngineUtil.class);53         String jsFileName = basePath + "/resources/" + jsName;   // 讀取js檔案   54         InputStreamReader reader = null;55         try {56             reader =new InputStreamReader(new FileInputStream(jsFileName),"UTF-8");57             engine.eval(reader);   58             if(engine instanceof Invocable) {    59                 invoke = (Invocable)engine;    60             }   61         } catch (FileNotFoundException e) {62             log.error(e.getMessage());63         } catch (ScriptException e) {64             log.error(e.getMessage());65         } catch (UnsupportedEncodingException e) {66             log.error(e.getMessage());67         } finally {68             if(reader != null){69                 try {70                     reader.close();71                 } catch (IOException e) {72                     log.error(e.getMessage());73                 }74             }75         }76     }77     78     public static void main(String[] args){79         String enc = invokeJSFuntion("aes.js","valAesEncryptSet","123456");80         System.out.println(enc);81     }82 }

 

Java中通過指令碼引擎調用js函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.