Search in sources :

Example 16 with BusinessException

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

the class JedisCacheService method getHashEntry.

@Override
public <T> T getHashEntry(String key, String entryKey, Class<T> clazz) throws BusinessException {
    if (StringUtils.isBlank(key) || StringUtils.isBlank(entryKey) || clazz == null) {
        throw new NullPointerException("请检查传入参数");
    }
    Gson g = new Gson();
    T t = null;
    String value = null;
    synchronized (jedis) {
        value = jedis.hget(key, entryKey);
    }
    if (!StringUtils.isBlank(value)) {
        try {
            t = g.fromJson(value, clazz);
        } catch (JsonSyntaxException e) {
            throw new BusinessException(CommonErrorCode.JSON_ERROR, e).put("methodName", "getHashEntry").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 17 with BusinessException

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

the class JedisCacheService method getHashMap.

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

Example 18 with BusinessException

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

the class RedisTemplateCacheService method getHashEntry.

@Override
public <T> T getHashEntry(String key, String entryKey, Class<T> clazz) throws BusinessException {
    if (StringUtils.isBlank(key)) {
        throw new NullPointerException("key is blank");
    }
    if (StringUtils.isBlank(entryKey)) {
        throw new NullPointerException("entryKey is blank");
    }
    if (clazz == null) {
        throw new NullPointerException("clazz is null");
    }
    Gson g = new Gson();
    T t = null;
    String value = (String) redisTemplate.opsForHash().get(key, entryKey);
    if (!StringUtils.isBlank(value)) {
        try {
            t = g.fromJson(value, clazz);
        } catch (JsonSyntaxException e) {
            throw new BusinessException(CommonErrorCode.JSON_ERROR, e).put("methodName", "getHashEntry").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 19 with BusinessException

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

the class DSynchronizedTest method testGetLockKey.

@Test
public void testGetLockKey() {
    Method method = ReflectionUtils.findMethod(getClass(), "doSomething", int.class, int.class);
    Assert.assertNotNull(method);
    String keyEL = "#target.toKey()";
    String key = null;
    try {
        key = DSynchronizedAspect.getLockKey(keyEL, this, method, new Object[] { 1, 2 });
        Assert.assertEquals("1-2", key);
        key = DSynchronizedAspect.getLockKey(keyEL, this, method, new Object[] { 3, 2 });
        Assert.assertEquals("2-3", key);
    } catch (BusinessException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 20 with BusinessException

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

the class DSynchronizedTest method testGetLockKeyWithArgs.

@Test
public void testGetLockKeyWithArgs() {
    Method method = ReflectionUtils.findMethod(getClass(), "doSomething", int.class, int.class);
    Assert.assertNotNull(method);
    String keyEL = "#target.toKey(#a, #b)";
    String key = null;
    try {
        key = DSynchronizedAspect.getLockKey(keyEL, this, method, new Object[] { 1, 2 });
        Assert.assertEquals("1-2", key);
        key = DSynchronizedAspect.getLockKey(keyEL, this, method, new Object[] { 3, 2 });
        Assert.assertEquals("2-3", key);
    } catch (BusinessException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : BusinessException(com.terran4j.commons.util.error.BusinessException) Method(java.lang.reflect.Method) Test(org.junit.Test)

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