Java中通过脚本引擎调用js函数

时间:2024-03-02 18:29:19
 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      * @return
26      */
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 }