Search in sources :

Example 1 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class DsqlBuilder method buildSQL.

public final String buildSQL(Map<String, Object> model, Class<?> clazz, String sqlName) throws BusinessException {
    if (model == null) {
        model = new HashMap<>();
    }
    String fileName = sqlName + ".sql.ftl";
    Template template = getTemplate(clazz, fileName);
    if (template == null) {
        throw new BusinessException(ErrorCodes.CONFIG_ERROR).put("package", clazz.getPackage()).put("fileName", fileName).setMessage("在包 ${package} 下面找不到文件: ${fileName}。");
    }
    try {
        String sql = build(template, model);
        if (log.isInfoEnabled()) {
            log.info("\nSQL(模板解析后): \n{}\n参数: {}", sql.trim(), model);
        }
        return sql;
    } catch (IOException | TemplateException e) {
        throw new BusinessException(ErrorCodes.CONFIG_ERROR).put("package", clazz.getPackage()).put("fileName", fileName).put("params", model).setMessage("使用文件 ${fileName} 构建SQL出错:" + e.getMessage());
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Example 2 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class DsqlRepositoryProxy method doModifying.

private Object doModifying(String sqlName, Map<String, Object> args, Method method, DsqlExecutor executor) throws BusinessException {
    SqlInfo sqlInfo = SqlInfo.create(args, proxyInterface, sqlName);
    Class<?> returnType = method.getReturnType();
    if (returnType.equals(Integer.class) || returnType.equals(int.class)) {
        return executor.update(sqlInfo);
    }
    if (returnType.equals(Long.class) || returnType.equals(long.class)) {
        return (long) executor.update(sqlInfo);
    }
    throw new BusinessException(ErrorCodes.CONFIG_ERROR).put("method", method).put("returnType", returnType).setMessage("Unknown returnType: ${returnType}, " + "@DsqlModifying method ONLY support  returnTypes: " + "Long, long, Integer, int");
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException)

Example 3 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class Api2DocCollector method postProcessAfterInitialization.

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    ApiFolderObject folder;
    try {
        folder = toApiFolder(bean, beanName);
    } catch (BusinessException e) {
        throw new BeanDefinitionStoreException("bean上的文档信息定义出错:" + e.getMessage());
    }
    if (folder == null) {
        return bean;
    }
    String id = folder.getId();
    ApiFolderObject existApiFolder = apiDocService.getFolder(id);
    if (existApiFolder != null) {
        String msg = "@Api2Doc id值重复: " + id;
        throw new BeanDefinitionStoreException(msg);
    }
    if (folder != null) {
        apiDocService.addFolder(folder);
    }
    return bean;
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) BeanDefinitionStoreException(org.springframework.beans.factory.BeanDefinitionStoreException)

Example 4 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class JedisCacheService method getObject.

@Override
public <T> T getObject(String key, Class<T> clazz) throws BusinessException {
    if (StringUtils.isBlank(key) || clazz == null) {
        throw new NullPointerException("请检查传入参数");
    }
    Gson g = new Gson();
    T t = null;
    String value = null;
    synchronized (jedis) {
        value = jedis.get(key);
    }
    if (!StringUtils.isBlank(value)) {
        try {
            t = (T) g.fromJson(value, clazz);
        } catch (JsonSyntaxException e) {
            throw new BusinessException(CommonErrorCode.JSON_ERROR, e).put("methodName", "getObject").put("key", key).put("clazz", clazz).put("value", value).setMessage("${methodName}方法, key=${key}, value=${value}, 解析json串失败: " + e.getMessage());
        }
    }
    return t;
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) JsonSyntaxException(com.google.gson.JsonSyntaxException) Gson(com.google.gson.Gson)

Example 5 with BusinessException

use of com.terran4j.commons.util.error.BusinessException in project commons by terran4j.

the class RefluxClientImpl method connect.

@Override
public final boolean connect(String serverURL, String clientId) throws BusinessException {
    try {
        URLEncoder.encode(clientId, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        throw // 
        new BusinessException(CommonErrorCode.INVALID_PARAM, e1).setMessage("参数 clientId 不能进行 URLEncoder 编码: ${clientId}").put("clientId", clientId);
    }
    String url = new StringBuffer(serverURL).append("?clientId=").append(clientId).toString();
    if (log.isInfoEnabled()) {
        log.info("connect server web socket url: {}", url);
    }
    URI endpointURI = null;
    try {
        endpointURI = new URI(url);
    } catch (URISyntaxException e) {
        throw new RuntimeException("连接的URL不正确: " + url, e);
    }
    // 建立 Web Socket 连接。
    ClientConnection oldEndpoint = connections.get(serverURL);
    ClientConnection endpoint = new ClientConnection(serverURL, clientId, messageHandler);
    try {
        Session sesson = container.connectToServer(endpoint, endpointURI);
        if (log.isInfoEnabled()) {
            log.info("目标服务连接成功: {}", url);
        }
        return sesson != null;
    } catch (DeploymentException e) {
        log.error("目标服务部署问题, url = " + url, e);
    } catch (IOException e) {
        log.error("目标服务连接问题, url = " + url, e);
    } finally {
        connections.put(serverURL, endpoint);
    }
    // 如果有旧连接,则释放旧连接。
    if (oldEndpoint != null) {
        try {
            oldEndpoint.close();
        } catch (IOException e) {
            log.error("关闭旧连接失败: " + e.getMessage(), e);
        }
    }
    return false;
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DeploymentException(javax.websocket.DeploymentException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) Session(javax.websocket.Session)

Aggregations

BusinessException (com.terran4j.commons.util.error.BusinessException)25 Gson (com.google.gson.Gson)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 Method (java.lang.reflect.Method)4 InvalidKeyException (java.security.InvalidKeyException)4 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)4 Logger (org.slf4j.Logger)3 IOException (java.io.IOException)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 Test (org.junit.Test)2 MissingServletRequestParameterException (org.springframework.web.bind.MissingServletRequestParameterException)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Message (com.terran4j.commons.reflux.Message)1 OnMessage (com.terran4j.commons.reflux.OnMessage)1