Search in sources :

Example 1 with IService

use of cn.cerc.jbean.core.IService in project summer-bean by cn-cerc.

the class AutoService method exec.

public boolean exec() throws ServiceException, UserNotFindException, ServiceException {
    if (service.getService() == null)
        throw new RuntimeException("没有指定 service");
    handle.init(service.getCorpNo(), service.getUserCode(), "127.0.0.1");
    IService bean = Application.getService(this, service.getService());
    if (bean == null)
        throw new RuntimeException("无法创建服务:" + service.getService());
    IStatus status = bean.execute(service.getDataIn(), dataOut);
    boolean result = status.getResult();
    this.setMessage(status.getMessage());
    return result;
}
Also used : IStatus(cn.cerc.jbean.core.IStatus) IService(cn.cerc.jbean.core.IService)

Example 2 with IService

use of cn.cerc.jbean.core.IService in project summer-bean by cn-cerc.

the class LocalService method execute.

// 不带缓存调用服务
public IStatus execute(Object... args) {
    if (args.length > 0) {
        Record headIn = getDataIn().getHead();
        if (args.length % 2 != 0)
            return new ServiceStatus(false, "传入的参数数量必须为偶数!");
        for (int i = 0; i < args.length; i = i + 2) headIn.setField(args[i].toString(), args[i + 1]);
    }
    if (handle == null)
        return new ServiceStatus(false, "handle is null.");
    if (serviceCode == null)
        return new ServiceStatus(false, "service is null.");
    IService bean = Application.getService(handle, serviceCode);
    if (bean == null)
        return new ServiceStatus(false, String.format("bean %s not find", serviceCode));
    if ((bean instanceof Microservice) && ((Microservice) bean).getService() == null)
        ((Microservice) bean).setService(serviceCode);
    try {
        log.info(this.serviceCode);
        IStatus status = bean.execute(dataIn, dataOut);
        message = status.getMessage();
        return status;
    } catch (Exception e) {
        Throwable err = e;
        if (e.getCause() != null)
            err = e.getCause();
        log.error(err.getMessage(), err);
        message = err.getMessage();
        return new ServiceStatus(false, message);
    }
}
Also used : IStatus(cn.cerc.jbean.core.IStatus) ServiceStatus(cn.cerc.jbean.core.ServiceStatus) Record(cn.cerc.jdb.core.Record) IService(cn.cerc.jbean.core.IService)

Example 3 with IService

use of cn.cerc.jbean.core.IService in project summer-bean by cn-cerc.

the class LocalService method exec.

// 带缓存调用服务
@Override
public boolean exec(Object... args) {
    if (args.length > 0) {
        Record headIn = getDataIn().getHead();
        if (args.length % 2 != 0)
            throw new RuntimeException("传入的参数数量必须为偶数!");
        for (int i = 0; i < args.length; i = i + 2) headIn.setField(args[i].toString(), args[i + 1]);
    }
    if (handle == null)
        throw new RuntimeException("handle is null.");
    if (serviceCode == null)
        throw new RuntimeException("service is null.");
    IService bean = Application.getService(handle, serviceCode);
    if (bean == null) {
        this.message = String.format("bean %s not find", serviceCode);
        return false;
    }
    if ((bean instanceof Microservice) && ((Microservice) bean).getService() == null)
        ((Microservice) bean).setService(serviceCode);
    try {
        if (!"AppSessionRestore.byUserCode".equals(this.serviceCode))
            log.info(this.serviceCode);
        if (ServerConfig.getAppLevel() == ServerConfig.appRelease) {
            IStatus status = bean.execute(dataIn, dataOut);
            boolean result = status.getResult();
            message = status.getMessage();
            return result;
        }
        IMemcache buff = Application.getMemcache();
        // 制作临时缓存Key
        String key = MD5.get(handle.getUserCode() + this.serviceCode + dataIn.getJSON());
        if (bufferRead) {
            String buffValue = (String) buff.get(key);
            if (buffValue != null) {
                log.debug("read from buffer: " + this.serviceCode);
                dataOut.setJSON(buffValue);
                message = dataOut.getHead().getString("_message_");
                return dataOut.getHead().getBoolean("_result_");
            }
        }
        // 没有缓存时,直接读取并存入缓存
        bean.init(handle);
        IStatus status = bean.execute(dataIn, dataOut);
        boolean result = status.getResult();
        message = status.getMessage();
        if (bufferWrite) {
            log.debug("write to buffer: " + this.serviceCode);
            dataOut.getHead().setField("_message_", message);
            dataOut.getHead().setField("_result_", result);
            buff.set(key, dataOut.getJSON());
        }
        return result;
    } catch (Exception e) {
        Throwable err = e;
        if (e.getCause() != null)
            err = e.getCause();
        log.error(err.getMessage(), err);
        message = err.getMessage();
        return false;
    }
}
Also used : IStatus(cn.cerc.jbean.core.IStatus) IMemcache(cn.cerc.jdb.cache.IMemcache) Record(cn.cerc.jdb.core.Record) IService(cn.cerc.jbean.core.IService)

Example 4 with IService

use of cn.cerc.jbean.core.IService in project summer-bean by cn-cerc.

the class StartServices method doProcess.

private void doProcess(String method, HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException, IOException {
    String uri = req.getRequestURI();
    AppConfig conf = Application.getAppConfig();
    if (!uri.startsWith("/" + conf.getPathServices()))
        return;
    req.setCharacterEncoding("UTF-8");
    resp.setContentType("text/html;charset=UTF-8");
    ResponseData respData = new ResponseData();
    // 将restPath转成service代码
    DataSet dataIn = new DataSet();
    String str = getParams(req);
    if (null != str && !"[{}]".equals(str))
        dataIn.setJSON(str);
    String serviceCode = getServiceCode(method, req.getRequestURI().substring(1), dataIn.getHead());
    log.info(req.getRequestURI() + " => " + serviceCode);
    if (serviceCode == null) {
        respData.setMessage("restful not find: " + req.getRequestURI());
        resp.getWriter().write(respData.toString());
        return;
    }
    log.debug(serviceCode);
    log.info(dataIn);
    try (AppHandle handle = new AppHandle()) {
        // 执行指定函数
        handle.init(req.getParameter("token"));
        handle.setProperty(sessionId, req.getSession().getId());
        IService bean = Application.getService(handle, serviceCode);
        if (bean == null) {
            respData.setMessage(String.format("service(%s) is null.", serviceCode));
            resp.getWriter().write(respData.toString());
            return;
        }
        if (!bean.checkSecurity(handle)) {
            respData.setMessage("请您先登入系统");
            resp.getWriter().write(respData.toString());
            return;
        }
        DataSet dataOut = new DataSet();
        IStatus status = bean.execute(dataIn, dataOut);
        respData.setResult(status.getResult());
        respData.setMessage(status.getMessage());
        respData.setData(bean.getJSON(dataOut));
    } catch (Exception e) {
        Throwable err = e.getCause() != null ? e.getCause() : e;
        log.error(err.getMessage(), err);
        respData.setResult(false);
        respData.setMessage(err.getMessage());
    }
    resp.getWriter().write(respData.toString());
}
Also used : AppConfig(cn.cerc.jbean.core.AppConfig) IStatus(cn.cerc.jbean.core.IStatus) DataSet(cn.cerc.jdb.core.DataSet) AppHandle(cn.cerc.jbean.core.AppHandle) IService(cn.cerc.jbean.core.IService) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with IService

use of cn.cerc.jbean.core.IService in project summer-bean by cn-cerc.

the class StartServices method loadServices.

private static void loadServices() {
    if (services != null)
        return;
    services = new HashMap<>();
    for (String serviceCode : Application.getServices().getBeanDefinitionNames()) {
        IService service = Application.getService(null, serviceCode);
        if (service instanceof IRestful) {
            String path = ((IRestful) service).getRestPath();
            if (null != path && !"".equals(path)) {
                services.put(path, serviceCode);
                log.info("restful service " + serviceCode + ": " + path);
            }
        }
    }
}
Also used : IRestful(cn.cerc.jbean.core.IRestful) IService(cn.cerc.jbean.core.IService)

Aggregations

IService (cn.cerc.jbean.core.IService)5 IStatus (cn.cerc.jbean.core.IStatus)4 Record (cn.cerc.jdb.core.Record)2 AppConfig (cn.cerc.jbean.core.AppConfig)1 AppHandle (cn.cerc.jbean.core.AppHandle)1 IRestful (cn.cerc.jbean.core.IRestful)1 ServiceStatus (cn.cerc.jbean.core.ServiceStatus)1 IMemcache (cn.cerc.jdb.cache.IMemcache)1 DataSet (cn.cerc.jdb.core.DataSet)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ServletException (javax.servlet.ServletException)1