Skip to content

Commit 2c500f9

Browse files
committed
added Classpath scanning for annotation related changes, Added test cases.
1 parent c662657 commit 2c500f9

File tree

68 files changed

+2915
-1089
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2915
-1089
lines changed

src/main/java/org/audit4j/core/AnnotationTransformer.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,47 @@
3737
*/
3838
public class AnnotationTransformer {
3939

40-
/**
41-
* Transform to event.
42-
*
43-
* @param annotationEvent
44-
* the annotation event
45-
* @return the audit event
46-
* @since 2.0.0
47-
*/
48-
public AuditEvent transformToEvent(AnnotationAuditEvent annotationEvent) {
49-
final AuditAnnotationAttributes auditAttributes = new AuditAnnotationAttributes();
50-
final IgnoreAuditAnnotationAttributes ignoreAttributes = new IgnoreAuditAnnotationAttributes();
51-
final AuditFieldAnnotationAttribute fieldAttributes = new AuditFieldAnnotationAttribute();
52-
List<Field> fields = null;
53-
AuditEvent event = null;
54-
String action = "";
40+
/**
41+
* Transform to event.
42+
*
43+
* @param annotationEvent
44+
* the annotation event
45+
* @return the audit event
46+
* @since 2.0.0
47+
*/
48+
public AuditEvent transformToEvent(AnnotationAuditEvent annotationEvent) {
49+
final AuditAnnotationAttributes auditAttributes = new AuditAnnotationAttributes();
50+
final IgnoreAuditAnnotationAttributes ignoreAttributes = new IgnoreAuditAnnotationAttributes();
51+
final AuditFieldAnnotationAttribute fieldAttributes = new AuditFieldAnnotationAttribute();
52+
List<Field> fields = null;
53+
AuditEvent event = null;
54+
String action = "";
5555

56-
if (auditAttributes.hasAnnotation(annotationEvent.getClazz())
57-
&& !ignoreAttributes.hasAnnotation(annotationEvent.getMethod())) {
58-
event = new AuditEvent();
59-
final SelectionType selection = auditAttributes.getSelection(annotationEvent.getClazz());
60-
if (selection.equals(SelectionType.ALL)) {
61-
fields = fieldAttributes.getAllFields(annotationEvent.getMethod(), annotationEvent.getArgs());
62-
} else if (selection.equals(SelectionType.MARKED)) {
63-
fields = fieldAttributes.getMarkedFields(annotationEvent.getMethod(), annotationEvent.getArgs());
64-
}
65-
action = auditAttributes.getAction(annotationEvent.getClazz(), annotationEvent.getMethod());
66-
} else if (!auditAttributes.hasAnnotation(annotationEvent.getClazz()) && auditAttributes.hasAnnotation(annotationEvent.getMethod())) {
67-
event = new AuditEvent();
68-
final SelectionType selection = auditAttributes.getSelection(annotationEvent.getMethod());
69-
if (selection.equals(SelectionType.ALL)) {
70-
fields = fieldAttributes.getAllFields(annotationEvent.getMethod(), annotationEvent.getArgs());
71-
} else if (selection.equals(SelectionType.MARKED)) {
72-
fields = fieldAttributes.getMarkedFields(annotationEvent.getMethod(), annotationEvent.getArgs());
73-
}
74-
action = auditAttributes.getAction(annotationEvent.getMethod());
75-
}
76-
77-
if (action.equals(CoreConstants.ACTION.equals(action))) {
78-
action = annotationEvent.getMethod().getName();
79-
}
80-
event.setAction(action);
81-
event.setFields(fields);
82-
return event;
83-
}
56+
if (auditAttributes.hasAnnotation(annotationEvent.getClazz())
57+
&& !ignoreAttributes.hasAnnotation(annotationEvent.getMethod())) {
58+
event = new AuditEvent();
59+
final SelectionType selection = auditAttributes.getSelection(annotationEvent.getClazz());
60+
if (selection.equals(SelectionType.ALL)) {
61+
fields = fieldAttributes.getAllFields(annotationEvent.getMethod(), annotationEvent.getArgs());
62+
} else if (selection.equals(SelectionType.MARKED)) {
63+
fields = fieldAttributes.getMarkedFields(annotationEvent.getMethod(), annotationEvent.getArgs());
64+
}
65+
action = auditAttributes.getAction(annotationEvent.getClazz(), annotationEvent.getMethod());
66+
event.setAction(action);
67+
event.setFields(fields);
68+
} else if (!auditAttributes.hasAnnotation(annotationEvent.getClazz())
69+
&& auditAttributes.hasAnnotation(annotationEvent.getMethod())) {
70+
event = new AuditEvent();
71+
final SelectionType selection = auditAttributes.getSelection(annotationEvent.getMethod());
72+
if (selection.equals(SelectionType.ALL)) {
73+
fields = fieldAttributes.getAllFields(annotationEvent.getMethod(), annotationEvent.getArgs());
74+
} else if (selection.equals(SelectionType.MARKED)) {
75+
fields = fieldAttributes.getMarkedFields(annotationEvent.getMethod(), annotationEvent.getArgs());
76+
}
77+
action = auditAttributes.getAction(annotationEvent.getMethod());
78+
event.setAction(action);
79+
event.setFields(fields);
80+
}
81+
return event;
82+
}
8483
}

src/main/java/org/audit4j/core/AuditManager.java

Lines changed: 120 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
package org.audit4j.core;
2020

2121
import java.lang.reflect.Method;
22+
import java.util.List;
2223

23-
import org.audit4j.core.annotation.AuditAnnotationAttributes;
24+
import org.audit4j.core.dto.AnnotationAuditEvent;
2425
import org.audit4j.core.dto.AuditEvent;
26+
import org.audit4j.core.filter.AuditAnnotationFilter;
2527

2628
/**
2729
* The AuditManager. This class is used to submit audit events as well as
@@ -33,83 +35,127 @@
3335
*/
3436
public final class AuditManager {
3537

36-
/**
37-
* Instantiates a new audit manager.
38-
*/
39-
private AuditManager() {
40-
}
38+
/**
39+
* Instantiates a new audit manager.
40+
*/
41+
private AuditManager() {
42+
}
4143

42-
/** The audit manager. */
43-
private static AuditManager auditManager;
44+
/** The audit manager. */
45+
private static AuditManager auditManager;
4446

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+
}
5355

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+
}
6567

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+
}
8485

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();
9995

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+
}
115161
}

0 commit comments

Comments
 (0)