提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.datapermission.core.aop; |
H |
2 |
|
|
3 |
import com.iailab.framework.datapermission.core.annotation.DataPermission; |
|
4 |
import com.alibaba.ttl.TransmittableThreadLocal; |
|
5 |
|
|
6 |
import java.util.LinkedList; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
/** |
|
10 |
* {@link DataPermission} 注解的 Context 上下文 |
|
11 |
* |
|
12 |
* @author iailab |
|
13 |
*/ |
|
14 |
public class DataPermissionContextHolder { |
|
15 |
|
|
16 |
/** |
|
17 |
* 使用 List 的原因,可能存在方法的嵌套调用 |
|
18 |
*/ |
|
19 |
private static final ThreadLocal<LinkedList<DataPermission>> DATA_PERMISSIONS = |
|
20 |
TransmittableThreadLocal.withInitial(LinkedList::new); |
|
21 |
|
|
22 |
/** |
|
23 |
* 获得当前的 DataPermission 注解 |
|
24 |
* |
|
25 |
* @return DataPermission 注解 |
|
26 |
*/ |
|
27 |
public static DataPermission get() { |
|
28 |
return DATA_PERMISSIONS.get().peekLast(); |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* 入栈 DataPermission 注解 |
|
33 |
* |
|
34 |
* @param dataPermission DataPermission 注解 |
|
35 |
*/ |
|
36 |
public static void add(DataPermission dataPermission) { |
|
37 |
DATA_PERMISSIONS.get().addLast(dataPermission); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* 出栈 DataPermission 注解 |
|
42 |
* |
|
43 |
* @return DataPermission 注解 |
|
44 |
*/ |
|
45 |
public static DataPermission remove() { |
|
46 |
DataPermission dataPermission = DATA_PERMISSIONS.get().removeLast(); |
|
47 |
// 无元素时,清空 ThreadLocal |
|
48 |
if (DATA_PERMISSIONS.get().isEmpty()) { |
|
49 |
DATA_PERMISSIONS.remove(); |
|
50 |
} |
|
51 |
return dataPermission; |
|
52 |
} |
|
53 |
|
|
54 |
/** |
|
55 |
* 获得所有 DataPermission |
|
56 |
* |
|
57 |
* @return DataPermission 队列 |
|
58 |
*/ |
|
59 |
public static List<DataPermission> getAll() { |
|
60 |
return DATA_PERMISSIONS.get(); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* 清空上下文 |
|
65 |
* |
|
66 |
* 目前仅仅用于单测 |
|
67 |
*/ |
|
68 |
public static void clear() { |
|
69 |
DATA_PERMISSIONS.remove(); |
|
70 |
} |
|
71 |
|
|
72 |
} |