Selaa lähdekoodia

调整c_service_sql表,加入执行java代码脚本功能

wuxw7 8 vuotta sitten
vanhempi
commit
605b9a1a15

+ 2 - 0
.gitignore

@@ -24,3 +24,5 @@ hs_err_pid*
 .idea
 logs
 *.iml
+
+*target/

+ 1 - 1
CenterService/doc/centerService.docx

@@ -983,7 +983,7 @@ query_model
 Varchar
 1
 查询方式
-1、sql,2、存储过程
+1、sql,2、存储过程,3、java脚本
 sql
 Longtext

+ 5 - 1
CodingLog.txt

@@ -16,4 +16,8 @@
 3、实现异步同步数据到业务系统处理(同步方式,需要做事件监听等业务未完成,做事件监听主要为了对报文重构)
 --------------------2018年04月17日-----------------------
 1、完成同步方式下订单侦听处理逻辑(主要是用来修改代码)
-2、完成订单处理逻辑,待测试版
+2、完成订单处理逻辑,待测试版
+--------------------2018年04月23日-----------------------
+1、加入sign 鉴权处理
+2、请求报文和返回报文加密处理
+3、加入java脚本代码处理

+ 1 - 1
OrderService/src/test/java/com/java110/order/executor/PrintInt.java

@@ -11,7 +11,7 @@ public class PrintInt implements Callable<Integer> {
 
         System.out.println("1234567");
 
-        Thread.sleep(10000);
+        //Thread.sleep(10000);
         throwException();
 
         return 1111;

+ 8 - 0
java110-bean/src/main/java/com/java110/entity/service/ServiceSql.java

@@ -24,6 +24,8 @@ public class ServiceSql implements Serializable{
 
     private String proc;
 
+    private String javaScript;
+
     private String template;
 
     private String statusCd;
@@ -109,5 +111,11 @@ public class ServiceSql implements Serializable{
         this.remark = remark;
     }
 
+    public String getJavaScript() {
+        return javaScript;
+    }
 
+    public void setJavaScript(String javaScript) {
+        this.javaScript = javaScript;
+    }
 }

+ 1 - 0
java110-common/src/main/java/com/java110/common/constant/CommonConstant.java

@@ -31,6 +31,7 @@ public class CommonConstant {
 
     public final static String QUERY_MODEL_SQL = "1";
     public final static String QUERY_MODEL_PROC = "2";
+    public final static String QUERY_MODE_JAVA = "3";
 
     /**
      * 加密 开关

+ 1 - 0
java110-config/db/CenterService/create_table.db

@@ -173,6 +173,7 @@ CREATE TABLE c_service_sql(
     query_model VARCHAR(1) NOT NULL COMMENT '查询方式 1、sql,2、存储过程',
     `sql` LONGTEXT COMMENT '执行sql',
     proc VARCHAR(200) COMMENT '存储过程名称',
+    java_script LONGTEXT COMMENT '执行java脚本代码',
     template LONGTEXT COMMENT '输出模板',
     remark VARCHAR(200) COMMENT '描述',
     create_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',

+ 2 - 1
java110-config/src/main/resources/mapper/service/QueryServiceDAOImplMapper.xml

@@ -11,6 +11,7 @@
         <result column="query_model" jdbcType="VARCHAR" property="queryModel"/>
         <result column="sql" jdbcType="CLOB" property="sql"/>
         <result column="proc" jdbcType="VARCHAR" property="proc"/>
+        <result column="java_script" jdbcType="CLOB" property="javaScript"/>
         <result column="template" jdbcType="CLOB" property="template"/>
         <result column="status_cd" jdbcType="VARCHAR" property="statusCd"/>
         <result column="remark" jdbcType="VARCHAR" property="remark"/>
@@ -39,7 +40,7 @@
 
     <select id="qureyServiceSqlAll" resultMap="serviceSqlMap">
         SELECT css.service_code,css.name,css.params,css.query_model,css.sql,
-        css.proc,css.template,css.remark,css.status_cd
+        css.proc,css.java_script,css.template,css.remark,css.status_cd
         FROM c_service_sql css WHERE css.status_cd = '0'
     </select>
 

+ 9 - 0
java110-service/pom.xml

@@ -48,6 +48,10 @@
             <artifactId>pagehelper-spring-boot-starter</artifactId>
         </dependency>
         -->
+        <dependency>
+            <groupId>org.beanshell</groupId>
+            <artifactId>bsh-core</artifactId>
+        </dependency>
 
 
         <dependency>
@@ -86,5 +90,10 @@
             <groupId>org.springframework</groupId>
             <artifactId>spring-jms</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <version>RELEASE</version>
+        </dependency>
     </dependencies>
 </project>

+ 3 - 2
java110-service/src/main/java/com/java110/service/rest/BusinessApi.java

@@ -47,7 +47,7 @@ public class BusinessApi extends BaseController {
      * @param businessInfo
      * @return
      */
-    @RequestMapping(path = "/queryApi/query",method= RequestMethod.POST)
+    @RequestMapping(path = "/businessApi/query",method= RequestMethod.POST)
     public String queryPost(@RequestBody String businessInfo) {
         try {
             DataQuery dataQuery = DataQueryFactory.newInstance().builder(businessInfo);
@@ -59,7 +59,7 @@ public class BusinessApi extends BaseController {
             return ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR,e.getMessage()+e).toJSONString();
         }
     }
-
+    @Deprecated
     @RequestMapping(path = "/businessApi/do",method= RequestMethod.GET)
     public String doGet(HttpServletRequest request) {
         return ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_ERROR,"不支持Get方法请求").toJSONString();
@@ -81,6 +81,7 @@ public class BusinessApi extends BaseController {
      * @param businessInfo
      * @return
      */
+    @Deprecated
     @RequestMapping(path = "/businessApi/do",method= RequestMethod.POST)
     public String doPost(@RequestBody String businessInfo) {
         try {

+ 36 - 0
java110-service/src/main/java/com/java110/service/smo/impl/QueryServiceSMOImpl.java

@@ -1,5 +1,6 @@
 package com.java110.service.smo.impl;
 
+import bsh.Interpreter;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.alibaba.fastjson.JSONPath;
@@ -48,6 +49,9 @@ public class QueryServiceSMOImpl extends LoggerEngine implements IQueryServiceSM
             if (CommonConstant.QUERY_MODEL_SQL.equals(currentServiceSql.getQueryModel())) {
                 doExecuteSql(dataQuery);
                 return;
+            }else if(CommonConstant.QUERY_MODE_JAVA.equals(currentServiceSql.getQueryModel())){
+                doExecuteJava(dataQuery);
+                return ;
             }
             doExecuteProc(dataQuery);
         }catch (BusinessException e){
@@ -72,6 +76,9 @@ public class QueryServiceSMOImpl extends LoggerEngine implements IQueryServiceSM
             if (CommonConstant.QUERY_MODEL_SQL.equals(currentServiceSql.getQueryModel())) {
                 doExecuteUpdateSql(dataQuery);
                 return;
+            }else if(CommonConstant.QUERY_MODE_JAVA.equals(currentServiceSql.getQueryModel())){
+                doExecuteJava(dataQuery);
+                return ;
             }
             doExecuteUpdateProc(dataQuery);
         }catch (BusinessException e){
@@ -123,6 +130,35 @@ public class QueryServiceSMOImpl extends LoggerEngine implements IQueryServiceSM
             throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"数据交互异常。。。");
         }
     }
+
+    /**
+     * 执行java脚本
+     * @param dataQuery
+     * @throws BusinessException
+     */
+    private void doExecuteJava(DataQuery dataQuery) throws BusinessException{
+        try {
+            JSONObject params = dataQuery.getRequestParams();
+            String javaCode = dataQuery.getServiceSql().getJavaScript();
+
+            Interpreter interpreter = new Interpreter();
+            interpreter.eval(javaCode);
+            String param = "";
+            for(String key : params.keySet()){
+                param += (params.getString(key) + ",");
+            }
+
+            if(param.endsWith(",")){
+                param = param.substring(0,param.length()-1);
+            }
+
+            dataQuery.setResponseInfo(ResponseTemplateUtil.createBusinessResponseJson(ResponseConstant.RESULT_CODE_SUCCESS,
+                    "成功",JSONObject.parseObject(interpreter.eval("execute("+param+")").toString())));
+        }catch (Exception e){
+            logger.error("数据交互异常:",e);
+            throw new BusinessException(ResponseConstant.RESULT_CODE_INNER_ERROR,"数据交互异常。。。");
+        }
+    }
     /**
      * {"PARAM:"{
      "param1": "$.a.#A#Object",

+ 48 - 0
java110-service/src/test/java/com/java110/service/InterpreterTest.java

@@ -0,0 +1,48 @@
+package com.java110.service;
+
+import bsh.Interpreter;
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Created by wuxw on 2018/4/23.
+ */
+public class InterpreterTest extends TestCase {
+
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public InterpreterTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( InterpreterTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testInterpreter() throws Exception
+    {
+        String javaCode = "public int add(int a, int b){" +
+                "return a+b;" +
+                "}" +
+                "" +
+                "public int execute(int a,int b){" +
+                "return add(a,b);" +
+                "}";
+        Interpreter interpreter = new Interpreter();
+        interpreter.eval(javaCode);
+        String param = "9,4";
+        System.out.println(interpreter.eval("execute("+param+")").toString());
+    }
+}

+ 7 - 0
pom.xml

@@ -335,6 +335,13 @@
             </dependency>
 
 
+            <dependency>
+                <groupId>org.beanshell</groupId>
+                <artifactId>bsh-core</artifactId>
+                <version>2.0b4</version>
+            </dependency>
+
+
         </dependencies>
 
     </dependencyManagement>