mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 18:46:36 +08:00
commit:权限模块新增分配详情功能
This commit is contained in:
@@ -74,7 +74,7 @@ public class CacheConfig {
|
||||
@Bean
|
||||
public CacheManager cacheManager() {
|
||||
SimpleCacheManager manager = new SimpleCacheManager();
|
||||
//把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口
|
||||
// 把各个cache注册到cacheManager中,CaffeineCache实现了org.springframework.cache.Cache接口
|
||||
ArrayList<CaffeineCache> caches = new ArrayList<>();
|
||||
for (CacheEnum c : CacheEnum.values()) {
|
||||
caches.add(new CaffeineCache(c.name(),
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package com.orange.demo.common.core.cache;
|
||||
|
||||
import com.orange.demo.common.core.exception.MapCacheAccessException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -11,6 +18,7 @@ import java.util.function.Function;
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
|
||||
/**
|
||||
@@ -21,6 +29,14 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* 获取字典主键数据的函数对象。
|
||||
*/
|
||||
protected Function<V, K> idGetter;
|
||||
/**
|
||||
* 由于大部分场景是读取操作,所以使用读写锁提高并发的伸缩性。
|
||||
*/
|
||||
protected ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
/**
|
||||
* 超时时长。单位毫秒。
|
||||
*/
|
||||
protected static final long TIMEOUT = 2000L;
|
||||
|
||||
/**
|
||||
* 当前对象的构造器函数。
|
||||
@@ -52,10 +68,27 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @return 全部字段数据列表。
|
||||
*/
|
||||
@Override
|
||||
public synchronized List<V> getAll() {
|
||||
public List<V> getAll() {
|
||||
List<V> resultList = new LinkedList<>();
|
||||
for (Map.Entry<K, V> entry : dataMap.entrySet()) {
|
||||
resultList.add(entry.getValue());
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
for (Map.Entry<K, V> entry : dataMap.entrySet()) {
|
||||
resultList.add(entry.getValue());
|
||||
}
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
@@ -67,14 +100,31 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @return 对象列表。
|
||||
*/
|
||||
@Override
|
||||
public synchronized List<V> getInList(Set<K> keys) {
|
||||
public List<V> getInList(Set<K> keys) {
|
||||
List<V> resultList = new LinkedList<>();
|
||||
keys.forEach(key -> {
|
||||
V object = dataMap.get(key);
|
||||
if (object != null) {
|
||||
resultList.add(object);
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
keys.forEach(key -> {
|
||||
V object = dataMap.get(key);
|
||||
if (object != null) {
|
||||
resultList.add(object);
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
@@ -84,14 +134,31 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @param dataList 待缓存的数据列表。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void putAll(List<V> dataList) {
|
||||
public void putAll(List<V> dataList) {
|
||||
if (dataList == null) {
|
||||
return;
|
||||
}
|
||||
dataList.forEach(dataObj -> {
|
||||
K id = idGetter.apply(dataObj);
|
||||
dataMap.put(id, dataObj);
|
||||
});
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dataList.forEach(dataObj -> {
|
||||
K id = idGetter.apply(dataObj);
|
||||
dataMap.put(id, dataObj);
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,12 +168,32 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @param force true则强制刷新,如果false,当缓存中存在数据时不刷新。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void reload(List<V> dataList, boolean force) {
|
||||
public void reload(List<V> dataList, boolean force) {
|
||||
if (!force && this.getCount() > 0) {
|
||||
return;
|
||||
}
|
||||
this.invalidateAll();
|
||||
this.putAll(dataList);
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dataMap.clear();
|
||||
dataList.forEach(dataObj -> {
|
||||
K id = idGetter.apply(dataObj);
|
||||
dataMap.put(id, dataObj);
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,8 +203,30 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @return 获取到的数据,如果没有返回null。
|
||||
*/
|
||||
@Override
|
||||
public synchronized V get(K id) {
|
||||
return id == null ? null : dataMap.get(id);
|
||||
public V get(K id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
V data;
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
data = dataMap.get(id);
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,8 +236,25 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @param object 字典数据对象。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void put(K id, V object) {
|
||||
dataMap.put(id, object);
|
||||
public void put(K id, V object) {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dataMap.put(id, object);
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,7 +263,7 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @return 返回缓存的数据数量。
|
||||
*/
|
||||
@Override
|
||||
public synchronized int getCount() {
|
||||
public int getCount() {
|
||||
return dataMap.size();
|
||||
}
|
||||
|
||||
@@ -148,8 +274,30 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @return 返回被删除的对象,如果主键不存在,返回null。
|
||||
*/
|
||||
@Override
|
||||
public synchronized V invalidate(K id) {
|
||||
return id == null ? null : dataMap.remove(id);
|
||||
public V invalidate(K id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
String exceptionMessage;
|
||||
V data;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
data = dataMap.remove(id);
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,19 +306,53 @@ public class MapDictionaryCache<K, V> implements DictionaryCache<K, V> {
|
||||
* @param keys 待删除数据的主键集合。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void invalidateSet(Set<K> keys) {
|
||||
keys.forEach(id -> {
|
||||
if (id != null) {
|
||||
dataMap.remove(id);
|
||||
public void invalidateSet(Set<K> keys) {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
keys.forEach(id -> {
|
||||
if (id != null) {
|
||||
dataMap.remove(id);
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void invalidateAll() {
|
||||
dataMap.clear();
|
||||
public void invalidateAll() {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
dataMap.clear();
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.orange.demo.common.core.cache;
|
||||
|
||||
import com.orange.demo.common.core.exception.MapCacheAccessException;
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
@@ -14,6 +18,7 @@ import java.util.function.Function;
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
@Slf4j
|
||||
public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
|
||||
/**
|
||||
@@ -61,8 +66,27 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
* @param parentId 父主键Id。
|
||||
* @return 子数据列表。
|
||||
*/
|
||||
public synchronized List<V> getListByParentId(K parentId) {
|
||||
return new LinkedList<>(allTreeMap.get(parentId));
|
||||
public List<V> getListByParentId(K parentId) {
|
||||
List<V> resultList = new LinkedList<>();
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
resultList.addAll(allTreeMap.get(parentId));
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return resultList;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,16 +95,33 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
* @param dataList 待缓存的数据列表。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void putAll(List<V> dataList) {
|
||||
public void putAll(List<V> dataList) {
|
||||
if (dataList == null) {
|
||||
return;
|
||||
}
|
||||
super.putAll(dataList);
|
||||
dataList.forEach(data -> {
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
});
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.putAll(dataList);
|
||||
dataList.forEach(data -> {
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,11 +131,28 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
* @param data 字典数据对象。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void put(K id, V data) {
|
||||
super.put(id, data);
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
public void put(K id, V data) {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.put(id, data);
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
allTreeMap.put(parentId, data);
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,11 +162,29 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
* @return 返回被删除的对象,如果主键不存在,返回null。
|
||||
*/
|
||||
@Override
|
||||
public synchronized V invalidate(K id) {
|
||||
V v = super.invalidate(id);
|
||||
if (v != null) {
|
||||
K parentId = parentIdGetter.apply(v);
|
||||
allTreeMap.remove(parentId, v);
|
||||
public V invalidate(K id) {
|
||||
V v;
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
v = super.invalidate(id);
|
||||
if (v != null) {
|
||||
K parentId = parentIdGetter.apply(v);
|
||||
allTreeMap.remove(parentId, v);
|
||||
}
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
@@ -119,24 +195,58 @@ public class MapTreeDictionaryCache<K, V> extends MapDictionaryCache<K, V> {
|
||||
* @param keys 待删除数据的主键集合。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void invalidateSet(Set<K> keys) {
|
||||
keys.forEach(id -> {
|
||||
if (id != null) {
|
||||
V data = dataMap.remove(id);
|
||||
if (data != null) {
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
public void invalidateSet(Set<K> keys) {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
keys.forEach(id -> {
|
||||
if (id != null) {
|
||||
V data = dataMap.remove(id);
|
||||
if (data != null) {
|
||||
K parentId = parentIdGetter.apply(data);
|
||||
allTreeMap.remove(parentId, data);
|
||||
}
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空缓存。
|
||||
*/
|
||||
@Override
|
||||
public synchronized void invalidateAll() {
|
||||
super.invalidateAll();
|
||||
allTreeMap.clear();
|
||||
public void invalidateAll() {
|
||||
String exceptionMessage;
|
||||
try {
|
||||
if (lock.readLock().tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
|
||||
try {
|
||||
super.invalidateAll();
|
||||
allTreeMap.clear();
|
||||
} finally {
|
||||
lock.readLock().unlock();
|
||||
}
|
||||
} else {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
exceptionMessage = String.format(
|
||||
"LOCK Operation of [MapDictionaryCache::getInList] encountered EXCEPTION [%s] for DICT.",
|
||||
e.getClass().getSimpleName());
|
||||
log.warn(exceptionMessage);
|
||||
throw new MapCacheAccessException(exceptionMessage, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@ package com.orange.demo.common.core.constant;
|
||||
*/
|
||||
public final class ApplicationConstant {
|
||||
|
||||
/**
|
||||
* 为字典表数据缓存时,缓存名称的固定后缀。
|
||||
*/
|
||||
public static final String DICT_CACHE_NAME_SUFFIX = "-DICT";
|
||||
/**
|
||||
* 图片文件上传的父目录。
|
||||
*/
|
||||
|
||||
@@ -37,7 +37,7 @@ public enum ErrorCodeEnum {
|
||||
INVALID_USER_STATUS("用户状态错误,请刷新后重试!"),
|
||||
|
||||
HAS_CHILDREN_DATA("数据验证失败,子数据存在,请刷新后重试!"),
|
||||
DATA_VALIDATAED_FAILED("数据验证失败,请核对!"),
|
||||
DATA_VALIDATED_FAILED("数据验证失败,请核对!"),
|
||||
UPLOAD_FILE_FAILED("文件上传失败,请联系管理员!"),
|
||||
DATA_SAVE_FAILED("数据保存失败,请联系管理员!"),
|
||||
DATA_ACCESS_FAILED("数据访问失败,请联系管理员!"),
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.orange.demo.common.core.exception;
|
||||
|
||||
/**
|
||||
* 内存缓存访问失败。比如:获取分布式数据锁超时、等待线程中断等。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-09-24
|
||||
*/
|
||||
public class MapCacheAccessException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* 构造函数。
|
||||
*
|
||||
* @param msg 错误信息。
|
||||
* @param cause 原始异常。
|
||||
*/
|
||||
public MapCacheAccessException(String msg, Throwable cause) {
|
||||
super(msg, cause);
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -45,6 +46,7 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
|
||||
classSet.add(Double.class);
|
||||
classSet.add(Boolean.class);
|
||||
classSet.add(Byte.class);
|
||||
classSet.add(BigDecimal.class);
|
||||
classSet.add(Character.class);
|
||||
}
|
||||
|
||||
@@ -100,7 +102,7 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
|
||||
}
|
||||
// 获取参数类型。
|
||||
Class<?> parameterType = parameter.getParameterType();
|
||||
//基本类型
|
||||
// 基本类型
|
||||
if (parameterType.isPrimitive()) {
|
||||
return parsePrimitive(parameterType.getName(), value);
|
||||
}
|
||||
@@ -196,6 +198,12 @@ public class MyRequestArgumentResolver implements HandlerMethodArgumentResolver
|
||||
return number.doubleValue();
|
||||
} else if (parameterType == Byte.class) {
|
||||
return number.byteValue();
|
||||
} else if (parameterType == BigDecimal.class) {
|
||||
if (value instanceof Double || value instanceof Float) {
|
||||
return BigDecimal.valueOf(number.doubleValue());
|
||||
} else {
|
||||
return BigDecimal.valueOf(number.longValue());
|
||||
}
|
||||
}
|
||||
} else if (parameterType == Boolean.class) {
|
||||
return value.toString();
|
||||
|
||||
@@ -76,6 +76,9 @@ public class MyGroupParam extends ArrayList<MyGroupParam.GroupInfo> {
|
||||
|
||||
private static GroupBaseData parseGroupBaseData(GroupInfo groupInfo, Class<?> modelClazz) {
|
||||
GroupBaseData baseData = new GroupBaseData();
|
||||
if (StringUtils.isBlank(groupInfo.fieldName)) {
|
||||
throw new IllegalArgumentException("GroupInfo.fieldName can't be EMPTY");
|
||||
}
|
||||
String[] stringArray = StringUtils.split(groupInfo.fieldName,'.');
|
||||
if (stringArray.length == 1) {
|
||||
baseData.modelName = modelClazz.getSimpleName();
|
||||
|
||||
@@ -110,11 +110,11 @@ public class LocalUpDownloader extends BaseUpDownloader {
|
||||
try {
|
||||
byte[] bytes = uploadFile.getBytes();
|
||||
Path path = Paths.get(uploadPath + responseInfo.getFilename());
|
||||
//如果没有files文件夹,则创建
|
||||
// 如果没有files文件夹,则创建
|
||||
if (!Files.isWritable(path)) {
|
||||
Files.createDirectories(Paths.get(uploadPath));
|
||||
}
|
||||
//文件写入指定路径
|
||||
// 文件写入指定路径
|
||||
Files.write(path, bytes);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to write uploaded file [" + uploadFile.getOriginalFilename() + " ].", e);
|
||||
|
||||
@@ -6,6 +6,9 @@ import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Spring 系统启动应用感知对象,主要用于获取Spring Bean的上下文对象,后续的代码中可以直接查找系统中加载的Bean对象。
|
||||
*
|
||||
@@ -62,6 +65,19 @@ public class ApplicationContextHolder implements ApplicationContextAware {
|
||||
return applicationContext.getBean(beanType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据Bean的ClassType,获取Bean对象列表。
|
||||
*
|
||||
* @param beanType Bean的Class类型。。
|
||||
* @param <T> 返回的Bean类型。
|
||||
* @return Bean对象列表。
|
||||
*/
|
||||
public static <T> Collection<T> getBeanListOfType(Class<T> beanType) {
|
||||
assertApplicationContext();
|
||||
Map<String, T> beanMap = applicationContext.getBeansOfType(beanType);
|
||||
return beanMap == null ? null : beanMap.values();
|
||||
}
|
||||
|
||||
private static void assertApplicationContext() {
|
||||
if (ApplicationContextHolder.applicationContext == null) {
|
||||
throw new MyRuntimeException("applicaitonContext属性为null,请检查是否注入了ApplicationContextHolder!");
|
||||
|
||||
@@ -31,29 +31,29 @@ public class IpUtil {
|
||||
*/
|
||||
public static String getRemoteIpAddress(HttpServletRequest request) {
|
||||
String ip = null;
|
||||
//X-Forwarded-For:Squid 服务代理
|
||||
// X-Forwarded-For:Squid 服务代理
|
||||
String ipAddresses = request.getHeader("X-Forwarded-For");
|
||||
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
|
||||
//Proxy-Client-IP:apache 服务代理
|
||||
// Proxy-Client-IP:apache 服务代理
|
||||
ipAddresses = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
|
||||
//WL-Proxy-Client-IP:weblogic 服务代理
|
||||
// WL-Proxy-Client-IP:weblogic 服务代理
|
||||
ipAddresses = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
|
||||
//HTTP_CLIENT_IP:有些代理服务器
|
||||
// HTTP_CLIENT_IP:有些代理服务器
|
||||
ipAddresses = request.getHeader("HTTP_CLIENT_IP");
|
||||
}
|
||||
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
|
||||
//X-Real-IP:nginx服务代理
|
||||
// X-Real-IP:nginx服务代理
|
||||
ipAddresses = request.getHeader("X-Real-IP");
|
||||
}
|
||||
//有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
|
||||
// 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
|
||||
if (StringUtils.isNotBlank(ipAddresses)) {
|
||||
ip = ipAddresses.split(",")[0];
|
||||
}
|
||||
//还是不能获取到,最后再通过request.getRemoteAddr();获取
|
||||
// 还是不能获取到,最后再通过request.getRemoteAddr();获取
|
||||
if (StringUtils.isBlank(ipAddresses) || UNKNOWN.equalsIgnoreCase(ipAddresses)) {
|
||||
ip = request.getRemoteAddr();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.orange.demo.common.core.util;
|
||||
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -7,10 +8,9 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
import javax.validation.Validator;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 脚手架中常用的基本工具方法集合,一般而言工程内部使用的方法。
|
||||
@@ -136,6 +136,25 @@ public class MyCommonUtil {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象中,非空字段的名字列表。
|
||||
*
|
||||
* @param object 数据对象。
|
||||
* @param clazz 数据对象的class类型。
|
||||
* @param <T> 数据对象类型。
|
||||
* @return 数据对象中,值不为NULL的字段数组。
|
||||
*/
|
||||
public static <T> String[] getNotNullFieldNames(T object, Class<T> clazz) {
|
||||
Field[] fields = ReflectUtil.getFields(clazz);
|
||||
List<String> fieldNameList = Arrays.stream(fields)
|
||||
.filter(f -> ReflectUtil.getFieldValue(object, f) != null)
|
||||
.map(Field::getName).collect(Collectors.toList());
|
||||
if (CollectionUtils.isNotEmpty(fieldNameList)) {
|
||||
return fieldNameList.toArray(new String[]{});
|
||||
}
|
||||
return new String[]{};
|
||||
}
|
||||
|
||||
/**
|
||||
* 私有构造函数,明确标识该常量类的作用。
|
||||
*/
|
||||
|
||||
@@ -50,6 +50,59 @@ public class MyModelUtil {
|
||||
*/
|
||||
private static Map<String, Tuple2<String, Integer>> cachedColumnInfoMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 拷贝源类型的集合数据到目标类型的集合中,其中源类型和目标类型中的对象字段类型完全相同。
|
||||
* NOTE: 该函数主要应用于框架中,Dto和Model之间的copy,特别针对一对一关联的深度copy。
|
||||
* 在Dto中,一对一对象可以使用Map来表示,而不需要使用从表对象的Dto。
|
||||
*
|
||||
* @param sourceCollection 源类型集合。
|
||||
* @param targetClazz 目标类型的Class对象。
|
||||
* @param <S> 源类型。
|
||||
* @param <T> 目标类型。
|
||||
* @return copy后的目标类型对象集合。
|
||||
*/
|
||||
public static <S, T> List<T> copyCollectionTo(Collection<S> sourceCollection, Class<T> targetClazz) {
|
||||
List<T> targetList = new LinkedList<>();
|
||||
if (CollectionUtils.isNotEmpty(sourceCollection)) {
|
||||
for (S source : sourceCollection) {
|
||||
try {
|
||||
T target = targetClazz.newInstance();
|
||||
BeanUtil.copyProperties(source, target);
|
||||
targetList.add(target);
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to call MyModelUtil.copyCollectionTo", e);
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝源类型的对象数据到目标类型的对象中,其中源类型和目标类型中的对象字段类型完全相同。
|
||||
* NOTE: 该函数主要应用于框架中,Dto和Model之间的copy,特别针对一对一关联的深度copy。
|
||||
* 在Dto中,一对一对象可以使用Map来表示,而不需要使用从表对象的Dto。
|
||||
*
|
||||
* @param source 源类型对象。
|
||||
* @param targetClazz 目标类型的Class对象。
|
||||
* @param <S> 源类型。
|
||||
* @param <T> 目标类型。
|
||||
* @return copy后的目标类型对象。
|
||||
*/
|
||||
public static <S, T> T copyTo(S source, Class<T> targetClazz) {
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
T target = targetClazz.newInstance();
|
||||
BeanUtil.copyProperties(source, target);
|
||||
return target;
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to call MyModelUtil.copyTo", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 映射Model对象的字段反射对象,获取与该字段对应的数据库列名称。
|
||||
*
|
||||
|
||||
@@ -46,9 +46,9 @@ public class RsaUtil {
|
||||
// 得到私钥字符串
|
||||
String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded());
|
||||
// 将公钥和私钥保存到Map
|
||||
//0表示公钥
|
||||
// 0表示公钥
|
||||
keyMap.put(0, publicKeyString);
|
||||
//1表示私钥
|
||||
// 1表示私钥
|
||||
keyMap.put(1, privateKeyString);
|
||||
}
|
||||
|
||||
@@ -61,11 +61,11 @@ public class RsaUtil {
|
||||
* @throws Exception 加密过程中的异常信息
|
||||
*/
|
||||
public static String encrypt(String str, String publicKey) throws Exception {
|
||||
//base64编码的公钥
|
||||
// base64编码的公钥
|
||||
byte[] decoded = Base64.getDecoder().decode(publicKey);
|
||||
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
|
||||
//RSA加密。后面这个更安全,但是SonarQube始终report安全漏洞。"RSA/ECB/PKCS1Padding"
|
||||
//而浏览器自带的Javascript加密功能,目前safari不支持,而且用的人也不太多。所以暂时都不考虑了。
|
||||
// RSA加密。后面这个更安全,但是SonarQube始终report安全漏洞。"RSA/ECB/PKCS1Padding"
|
||||
// 而浏览器自带的Javascript加密功能,目前safari不支持,而且用的人也不太多。所以暂时都不考虑了。
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
|
||||
return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
|
||||
@@ -80,12 +80,12 @@ public class RsaUtil {
|
||||
* @throws Exception 解密过程中的异常信息
|
||||
*/
|
||||
public static String decrypt(String str, String privateKey) throws Exception {
|
||||
//64位解码加密后的字符串
|
||||
// 64位解码加密后的字符串
|
||||
byte[] inputByte = Base64.getDecoder().decode(str);
|
||||
//base64编码的私钥
|
||||
// base64编码的私钥
|
||||
byte[] decoded = Base64.getDecoder().decode(privateKey);
|
||||
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
|
||||
//RSA解密
|
||||
// RSA解密
|
||||
Cipher cipher = Cipher.getInstance("RSA");
|
||||
cipher.init(Cipher.DECRYPT_MODE, priKey);
|
||||
return new String(cipher.doFinal(inputByte));
|
||||
@@ -93,9 +93,9 @@ public class RsaUtil {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
long temp = System.currentTimeMillis();
|
||||
//生成公钥和私钥
|
||||
// 生成公钥和私钥
|
||||
genKeyPair();
|
||||
//加密字符串
|
||||
// 加密字符串
|
||||
System.out.println("公钥:" + keyMap.get(0));
|
||||
System.out.println("私钥:" + keyMap.get(1));
|
||||
System.out.println("生成密钥消耗时间:" + (System.currentTimeMillis() - temp) / 1000.0 + "秒");
|
||||
|
||||
Reference in New Issue
Block a user