Use WebSphere ILOG JRules to develop insurance application system [2] - New WLS

write picture description here

WELL


  1. HelloWorldRequestBOM.java

package com.hello.bom;

import java.io.Serializable;

public class HelloWorldRequestBOM implements Serializable {
    private String requestType="";

    private String name="";

    private int age=0;

    private String result="";

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRequestType() {
        return requestType;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

}
2. HelloWorldResponseBOM.java:
package com.hello.bom;

import java.io.Serializable;

public class HelloWorldResponseBOM implements Serializable {
    private String requestType="";

    private String name="";

    private int age=0;

    private String result="";


    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRequestType() {
        return requestType;
    }

    public void setRequestType(String requestType) {
        this.requestType = requestType;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

}

controller


  1. HelloWorldController.java
package com.hello.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hello.bom.HelloWorldRequestBOM;
import com.hello.bom.HelloWorldResponseBOM;
import com.hello.service.HelloWorldService;
import com.hello.utils.Byte2StringUtils;
import com.hello.utils.ObjectXmlConvertor;




@Controller
@RequestMapping(value = "/helloWorldService")
public class HelloWorldController {
    @Resource(name = "HelloWorldServiceImpl")
    private HelloWorldService servcie;

    private final static String errorMsg = "规则库调用异常,堆栈信息如下:\n";

    /**
     * helloWorldRule
     * 
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(params = "method=helloWorldRule")
    public void helloWorldRule(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String data = null;
        try {
            String xml = Byte2StringUtils.toString(request.getReader());
            if (xml == null || "".equals(xml)) {
                String ret = new Date().toLocaleString()
                        + ",helloWorldRule调用失败,接口xml传值为空";
                System.out.println(ret);
                data = ret;
            } else {
                System.out.println("xml start:" + xml + "\n");
                HelloWorldRequestBOM requestDto = (HelloWorldRequestBOM) ObjectXmlConvertor
                        .xml2javaBean(xml, HelloWorldRequestBOM.class);

                HelloWorldResponseBOM responseBOM = servcie
                        .helloWorldService(requestDto);
                data = ObjectXmlConvertor.javaBean2Xml(responseBOM);
            }

        } catch (Exception e) {
            e.printStackTrace();
            data = errorMsg + e.getMessage();
        }
        sendResponseData(response, data);
    }

    /**
     * sendResponseData:将结果xml串返回给客户端
     * 
     * @param
     * @param
     * @throws IOException
     */
    private void sendResponseData(HttpServletResponse response, String data) {
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.println(data);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

engine

RuleExecuteSession.java:

package com.hello.engine;

import ilog.rules.bres.session.IlrRuleSessionProvider;
import ilog.rules.bres.session.IlrRuleSessionProviderFactory;
import ilog.rules.bres.session.IlrSessionExecutionResult;
import ilog.rules.bres.session.IlrSessionExecutionSettings;
import ilog.rules.bres.session.IlrSessionParameters;
import ilog.rules.bres.session.IlrSessionRequest;
import ilog.rules.bres.session.IlrSessionResponse;
import ilog.rules.bres.session.IlrStatelessRuleSession;

import java.io.Serializable;

import com.hello.bom.HelloWorldRequestBOM;
import com.hello.bom.HelloWorldResponseBOM;


public class RuleExecuteSession implements Serializable {
    private static final long serialVersionUID = -5923262005370268548L;

    private static final String helloWorldRequestBOM_in = "helloWorldRequestBOM";

    private static final String helloWorldResponseBOM_out = "helloWorldResponseBOM";

    /**
     * 执行helloworld提示规则
     * 
     * @param bom
     * @param rulePath
     * @return
     * @throws Exception
     */
    public Object runDocumentRules(HelloWorldRequestBOM bom,
            HelloWorldResponseBOM resultBOM, String rulePath) throws Exception {
        /*
         * Properties p = new Properties();
         * 
         * p.load(RuleExecuteSession.class.getClassLoader().getResourceAsStream("jndi.properties"));
         * IlrRuleSessionProvider rsProvider = new
         * IlrRuleSessionProviderFactory.Builder(p).build();
         * 
         * IlrStatelessRuleSession rs = rsProvider.createStatelessRuleSession();
         * IlrSessionRequest request = new IlrSessionRequest(rulePath);
         */

        String sessionType = "IlrSimpleRuleSessionProvider";
        IlrRuleSessionProvider provider = new IlrRuleSessionProviderFactory.Builder(
                sessionType).build();
        IlrStatelessRuleSession rs = provider.createStatelessRuleSession();
        IlrSessionRequest request = new IlrSessionRequest(rulePath);

        IlrSessionExecutionSettings settings = request.getExecutionSettings();
        IlrSessionParameters pars = settings.getInputParameters();
        pars.setParameter(helloWorldRequestBOM_in, bom);
        pars.setParameter(helloWorldResponseBOM_out, resultBOM);
        IlrSessionResponse response = rs.executeRules(request);
        System.out.println("------------执行helloworld提示规则-----------------");
        IlrSessionExecutionResult result = response.getExecutionResult();
        IlrSessionParameters outPars = result.getOutputParameters();
        resultBOM = (HelloWorldResponseBOM) outPars
                .getObjectValue(helloWorldResponseBOM_out);

        return resultBOM;
    }

}

service

1. HelloWorldService.java:
package com.hello.service;

import com.hello.bom.HelloWorldRequestBOM;
import com.hello.bom.HelloWorldResponseBOM;

public interface HelloWorldService {
    public HelloWorldResponseBOM helloWorldService(HelloWorldRequestBOM helloWorldRequestBOM);
}
2. HelloWorldServiceImpl.java:
package com.hello.service.impl;

import org.springframework.stereotype.Service;

import com.hello.bom.HelloWorldRequestBOM;
import com.hello.bom.HelloWorldResponseBOM;
import com.hello.engine.RuleExecuteSession;
import com.hello.service.HelloWorldService;





@Service("HelloWorldServiceImpl")
public class HelloWorldServiceImpl implements HelloWorldService {
    private RuleExecuteSession session = new RuleExecuteSession();

    public HelloWorldResponseBOM helloWorldService(
            HelloWorldRequestBOM requestBOM) {

        HelloWorldResponseBOM responseBOM = new HelloWorldResponseBOM();
        try {
            System.out.println("helloWorldService开始执行规则*******************:");
            // 执行规则库
            /*responseBOM = (HelloWorldResponseBOM)*/ session.runDocumentRules(
                    requestBOM,responseBOM, "/HelloWorldRuleApp/HelloWorldRule");
            System.out.println("helloWorldService请求成功*******************:");

        } catch (Exception e) {
            e.printStackTrace();
        }
        // 返回结果对象
        return responseBOM;
    }
}

utils

 1. Byte2StringUtils.java
package com.hello.utils;

import java.io.BufferedReader;
import java.io.IOException;

public class Byte2StringUtils {
    public static String toString(BufferedReader br) throws IOException {
        String inputLine = null;
        StringBuffer sb = new StringBuffer();
        try {
            while ((inputLine = br.readLine()) != null) {
                sb.append(inputLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }finally{
            br.close();
        }
        return sb.toString();
    }

}
 2. JavaBeanUtils.java:
package com.hello.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class JavaBeanUtils {
    @SuppressWarnings("unchecked")
    public static Method getGetMethod(Class objectClass, String fieldName) throws SecurityException, NoSuchMethodException {

        StringBuffer sb = new StringBuffer();
        sb.append("get");
        sb.append(fieldName.substring(0, 1).toUpperCase());
        sb.append(fieldName.substring(1));
        return objectClass.getMethod(sb.toString());
    }

    @SuppressWarnings("unchecked")
    public static Method getSetMethod(Class objectClass, String fieldName) throws SecurityException, NoSuchFieldException, NoSuchMethodException {
            Class[] parameterTypes = new Class[1];
            Field field = objectClass.getDeclaredField(fieldName);
            parameterTypes[0] = field.getType();
            StringBuffer sb = new StringBuffer();
            sb.append("set");
            sb.append(fieldName.substring(0, 1).toUpperCase());
            sb.append(fieldName.substring(1));
            Method method = objectClass.getMethod(sb.toString(), parameterTypes);
            return method;
    }

    public static void invokeSet(Object o, String fieldName, Object value) throws SecurityException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Method method = getSetMethod(o.getClass(), fieldName);
        if(value!=null)
        method.invoke(o, new Object[] { value });
    }

    public static Object invokeGet(Object o, String fieldName) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        Method method = getGetMethod(o.getClass(), fieldName);
        return method.invoke(o, new Object[0]);
    }

}
3. ObjectXmlConvertor.java:
package com.hello.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wutka.jox.JOXBeanInputStream;
import com.wutka.jox.JOXBeanOutputStream;

public class ObjectXmlConvertor {
    protected static final Log logger = LogFactory.getLog(ObjectXmlConvertor.class);
    public static String javaBean2Xml(Object o){
        OutputStream xmlData = null;
        try {
            xmlData = new ByteArrayOutputStream();
            JOXBeanOutputStream joxOut = new JOXBeanOutputStream(xmlData,"gbk");
            joxOut.writeObject(o.getClass().getName(), o);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("JavaBean To  XML Error",e);
        }
         return xmlData.toString();
    }

    public static Object xml2javaBean(String xml,Class className){
        try {
            InputStream in = new ByteArrayInputStream(xml.getBytes("gbk"));
            JOXBeanInputStream joxIn = new JOXBeanInputStream(in);
            Object o = (Object) joxIn.readObject(className);
            return o;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("XML To JavaBean Error",e);
            return null;
        }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522984&siteId=291194637