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());
}
}
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");
}
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;
}
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;
}
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;
}
Aggregations