|
19 | 19 | package org.audit4j.core;
|
20 | 20 |
|
21 | 21 | import java.lang.reflect.Method;
|
| 22 | +import java.util.List; |
22 | 23 |
|
23 |
| -import org.audit4j.core.annotation.AuditAnnotationAttributes; |
| 24 | +import org.audit4j.core.dto.AnnotationAuditEvent; |
24 | 25 | import org.audit4j.core.dto.AuditEvent;
|
| 26 | +import org.audit4j.core.filter.AuditAnnotationFilter; |
25 | 27 |
|
26 | 28 | /**
|
27 | 29 | * The AuditManager. This class is used to submit audit events as well as
|
|
33 | 35 | */
|
34 | 36 | public final class AuditManager {
|
35 | 37 |
|
36 |
| - /** |
37 |
| - * Instantiates a new audit manager. |
38 |
| - */ |
39 |
| - private AuditManager() { |
40 |
| - } |
| 38 | + /** |
| 39 | + * Instantiates a new audit manager. |
| 40 | + */ |
| 41 | + private AuditManager() { |
| 42 | + } |
41 | 43 |
|
42 |
| - /** The audit manager. */ |
43 |
| - private static AuditManager auditManager; |
| 44 | + /** The audit manager. */ |
| 45 | + private static AuditManager auditManager; |
44 | 46 |
|
45 |
| - /** |
46 |
| - * Initializing context and streams. |
47 |
| - * |
48 |
| - * @since 2.0.0 |
49 |
| - */ |
50 |
| - private static void init() { |
51 |
| - Context.init(); |
52 |
| - } |
| 47 | + /** |
| 48 | + * Initializing context and streams. |
| 49 | + * |
| 50 | + * @since 2.0.0 |
| 51 | + */ |
| 52 | + private static void init() { |
| 53 | + Context.init(); |
| 54 | + } |
53 | 55 |
|
54 |
| - /** |
55 |
| - * Audit. |
56 |
| - * |
57 |
| - * @param event |
58 |
| - * the event |
59 |
| - * @return true, if successful |
60 |
| - */ |
61 |
| - public boolean audit(AuditEvent event) { |
62 |
| - Context.getAuditStream().write(event); |
63 |
| - return true; |
64 |
| - } |
| 56 | + /** |
| 57 | + * Audit. |
| 58 | + * |
| 59 | + * @param event |
| 60 | + * the event |
| 61 | + * @return true, if successful |
| 62 | + */ |
| 63 | + public boolean audit(AuditEvent event) { |
| 64 | + Context.getAuditStream().write(event); |
| 65 | + return true; |
| 66 | + } |
65 | 67 |
|
66 |
| - /** |
67 |
| - * Audit with annotation. |
68 |
| - * |
69 |
| - * @param clazz |
70 |
| - * the clazz |
71 |
| - * @param method |
72 |
| - * the method |
73 |
| - * @param args |
74 |
| - * the args |
75 |
| - * @return true, if successful |
76 |
| - */ |
77 |
| - public boolean audit(Class<?> clazz, Method method, Object[] args) { |
78 |
| - final AuditAnnotationAttributes auditAttributes = new AuditAnnotationAttributes(); |
79 |
| - if (auditAttributes.hasAnnotation(clazz) || auditAttributes.hasAnnotation(method)) { |
80 |
| - Context.getAnnotationStream().write(clazz, method, args); |
81 |
| - } |
82 |
| - return true; |
83 |
| - } |
| 68 | + /** |
| 69 | + * Audit with annotation. |
| 70 | + * |
| 71 | + * @param clazz |
| 72 | + * the clazz |
| 73 | + * @param method |
| 74 | + * the method |
| 75 | + * @param args |
| 76 | + * the args |
| 77 | + * @return true, if successful |
| 78 | + * |
| 79 | + * TODO FIXME not purely asynchronous. should moved to the logic in |
| 80 | + * to asynchronous stream. |
| 81 | + */ |
| 82 | + public boolean audit(Class<?> clazz, Method method, Object[] args) { |
| 83 | + return audit(new AnnotationAuditEvent(clazz, method, args)); |
| 84 | + } |
84 | 85 |
|
85 |
| - /** |
86 |
| - * Gets the single instance of AuditHelper. |
87 |
| - * |
88 |
| - * @return single instance of AuditHelper |
89 |
| - */ |
90 |
| - public static AuditManager getInstance() { |
91 |
| - synchronized (AuditManager.class) { |
92 |
| - if (auditManager == null) { |
93 |
| - auditManager = new AuditManager(); |
94 |
| - init(); |
95 |
| - } |
96 |
| - } |
97 |
| - return auditManager; |
98 |
| - } |
| 86 | + /** |
| 87 | + * Audit. |
| 88 | + * |
| 89 | + * @param annotationEvent |
| 90 | + * the annotation event |
| 91 | + * @return true, if successful |
| 92 | + */ |
| 93 | + public boolean audit(AnnotationAuditEvent annotationEvent) { |
| 94 | + List<AuditAnnotationFilter> filters = Context.getConfigContext().getAnnotationFilters(); |
99 | 95 |
|
100 |
| - /** |
101 |
| - * This method allows to external plugins can inject the configurations. |
102 |
| - * Since the security reasons, this allows to create one time configuration |
103 |
| - * setting to Audit4j. |
104 |
| - * |
105 |
| - * @param configuration |
106 |
| - * the configuration |
107 |
| - * @return the configuration instance |
108 |
| - * |
109 |
| - * @since 2.1.0 |
110 |
| - */ |
111 |
| - public static AuditManager getConfigurationInstance(Configuration configuration) { |
112 |
| - Context.setConfig(configuration); |
113 |
| - return getInstance(); |
114 |
| - } |
| 96 | + boolean execute = true; |
| 97 | + if (!filters.isEmpty()) { |
| 98 | + for (AuditAnnotationFilter filter : filters) { |
| 99 | + if (!filter.accepts(annotationEvent)) { |
| 100 | + execute = false; |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + if (execute) { |
| 106 | + Context.getAnnotationStream().write(annotationEvent); |
| 107 | + return true; |
| 108 | + } |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Gets the single instance of AuditHelper. |
| 114 | + * |
| 115 | + * @return single instance of AuditHelper |
| 116 | + */ |
| 117 | + public static AuditManager getInstance() { |
| 118 | + if (auditManager == null) { |
| 119 | + synchronized (AuditManager.class) { |
| 120 | + if (auditManager == null) { |
| 121 | + auditManager = new AuditManager(); |
| 122 | + init(); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + return auditManager; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * This method allows to external plugins can inject the configurations. |
| 131 | + * Since the security reasons, this allows to create one time configuration |
| 132 | + * setting to Audit4j. |
| 133 | + * |
| 134 | + * @param configuration |
| 135 | + * the configuration |
| 136 | + * @return the configuration instance |
| 137 | + * |
| 138 | + * @since 2.1.0 |
| 139 | + */ |
| 140 | + |
| 141 | + public static AuditManager getConfigurationInstance(Configuration configuration) { |
| 142 | + Context.setConfig(configuration); |
| 143 | + return getInstance(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Inits the with configuration. |
| 148 | + * |
| 149 | + * @param configuration |
| 150 | + * the configuration |
| 151 | + * @return the audit manager |
| 152 | + */ |
| 153 | + public static AuditManager initWithConfiguration(Configuration configuration) { |
| 154 | + Context.setConfig(configuration); |
| 155 | + return getInstance(); |
| 156 | + } |
| 157 | + |
| 158 | + public static void shutdown() { |
| 159 | + Context.stop(); |
| 160 | + } |
115 | 161 | }
|
0 commit comments