Skip to content

Commit e4d5e9d

Browse files
author
Jan Zyka
committed
Make annotation transformer and object serializer pluggable
1 parent e0f181e commit e4d5e9d

File tree

6 files changed

+79
-17
lines changed

6 files changed

+79
-17
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
/**
2525
* Interface for different annotation transformer implementations. This can be
2626
* used to transform annotation information in to simple audit event.
27-
*
27+
*
2828
* @author <a href="mailto:[email protected]">Janith Bandara</a>
2929
*
3030
* @since 2.4.1
3131
*/
32-
public interface AnnotationTransformer {
32+
public interface AnnotationTransformer<T extends AuditEvent> {
3333

3434
/**
3535
* Transform to event.
@@ -38,5 +38,5 @@ public interface AnnotationTransformer {
3838
* input Annotation AuditEvent
3939
* @return transform audit event
4040
*/
41-
AuditEvent transformToEvent(AnnotationAuditEvent annotationEvent);
41+
T transformToEvent(AnnotationAuditEvent annotationEvent);
4242
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public class Configuration implements Serializable {
6363

6464
/** The jmx configurations. */
6565
private JMXConfig jmx;
66-
66+
67+
/**
68+
* Annotation transformer to be used
69+
*/
70+
private AnnotationTransformer annotationTransformer;
71+
6772
/** Return new static instance.
6873
*
6974
* @since 2.3.1
@@ -293,4 +298,21 @@ public JMXConfig getJmx() {
293298
public void setJmx(JMXConfig jmx) {
294299
this.jmx = jmx;
295300
}
301+
302+
/**
303+
* Gets the annotation transformer to use when converting {@code AnnotationAuditEvent}. Allows to inject custom
304+
* conversion logic and custom output type.
305+
*
306+
* @return the annotation transformer
307+
*/
308+
public AnnotationTransformer getAnnotationTransformer() {
309+
return annotationTransformer;
310+
}
311+
312+
/**
313+
* Sets annotation transformer to use.
314+
*/
315+
public void setAnnotationTransformer(AnnotationTransformer annotationTransformer) {
316+
this.annotationTransformer = annotationTransformer;
317+
}
296318
}

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,12 @@ final static void init() {
161161
initLayout();
162162

163163
// Initialize annotation transformer.
164-
DefaultAnnotationTransformer defaultAnnotationTransformer = new DefaultAnnotationTransformer();
165-
ObjectSerializerCommand serializerCommand = (ObjectSerializerCommand) PreConfigurationContext
166-
.getCommandByName("-objectSerializer");
167-
if (serializerCommand.getSerializer() == null) {
168-
defaultAnnotationTransformer.setSerializer(new ObjectToFieldsSerializer());
164+
165+
if (conf.getAnnotationTransformer() == null) {
166+
annotationTransformer = getDefaultAnnotationTransformer();
169167
} else {
170-
defaultAnnotationTransformer.setSerializer(serializerCommand.getSerializer());
168+
annotationTransformer = conf.getAnnotationTransformer();
171169
}
172-
annotationTransformer = defaultAnnotationTransformer;
173170

174171
// Initialize IO streams.
175172
initStreams();
@@ -414,6 +411,24 @@ private static void initStreams() {
414411
Log.info("Audit Streams Initialized.");
415412
}
416413

414+
/**
415+
* Initializes and returns the default annotation transformer.
416+
*
417+
* @return default annotation transformer
418+
*/
419+
private static AnnotationTransformer<AuditEvent> getDefaultAnnotationTransformer() {
420+
DefaultAnnotationTransformer defaultAnnotationTransformer = new DefaultAnnotationTransformer();
421+
ObjectSerializerCommand serializerCommand = (ObjectSerializerCommand) PreConfigurationContext
422+
.getCommandByName("-objectSerializer");
423+
if (serializerCommand.getSerializer() == null) {
424+
defaultAnnotationTransformer.setSerializer(new ObjectToFieldsSerializer());
425+
} else {
426+
defaultAnnotationTransformer.setSerializer(serializerCommand.getSerializer());
427+
}
428+
429+
return defaultAnnotationTransformer;
430+
}
431+
417432
/**
418433
* Sets the config.
419434
*

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@
3939
*
4040
* @since 2.0.0
4141
*/
42-
public class DefaultAnnotationTransformer implements AnnotationTransformer {
42+
public class DefaultAnnotationTransformer implements AnnotationTransformer<AuditEvent> {
4343

4444
private final static String ACTION = "action";
4545

4646
// Default Fields serializer
47-
private ObjectSerializer serializer = new ObjectToFieldsSerializer();
47+
private ObjectSerializer serializer;
48+
49+
public DefaultAnnotationTransformer() {
50+
this.serializer = new ObjectToFieldsSerializer();
51+
}
52+
53+
public DefaultAnnotationTransformer(ObjectSerializer objectSerializer) {
54+
this.serializer = objectSerializer;
55+
}
4856

4957
/**
5058
* Transform annotation informations to Audit Event object.

src/main/java/org/audit4j/core/dto/AnnotationAuditEvent.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class AnnotationAuditEvent extends AuditEvent {
4141
/** The args. */
4242
Object[] args;
4343

44+
/** The return of hte method, may be null */
45+
private Object methodCallResult;
46+
4447
/**
4548
* Instantiates a new annotation audit event.
4649
*/
@@ -65,6 +68,11 @@ public AnnotationAuditEvent(Class<?> clazz, Method method, Object[] args) {
6568
this.args = args;
6669
}
6770

71+
public AnnotationAuditEvent(Class<?> clazz, Method method, Object[] args, Object methodCallResult) {
72+
this(clazz, method, args);
73+
this.methodCallResult = methodCallResult;
74+
}
75+
6876
/**
6977
* Gets the clazz.
7078
*
@@ -122,4 +130,13 @@ public void setArgs(Object[] args) {
122130
this.args = args;
123131
}
124132

133+
/**
134+
* Gets the return object of method call. May be {@code null} in the case the method was {@code void} or in the case
135+
* the @Before aspect was used and therefore no return type was available.
136+
*
137+
* @return the {@code Object} returned by the audited method, if captured
138+
*/
139+
public Object getMethodCallResult() {
140+
return methodCallResult;
141+
}
125142
}

src/main/java/org/audit4j/core/handler/Handler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Janith Bandara
3333
*/
34-
public abstract class Handler implements Initializable, Serializable {
34+
public abstract class Handler<T extends AuditEvent> implements Initializable, Serializable {
3535

3636
/** The Constant serialVersionUID. */
3737
private static final long serialVersionUID = -8636058037478806582L;
@@ -40,7 +40,7 @@ public abstract class Handler implements Initializable, Serializable {
4040
private String query;
4141

4242
/** The audit event. */
43-
private AuditEvent auditEvent;
43+
private T auditEvent;
4444

4545
/** The properties. */
4646
private Map<String, String> properties;
@@ -76,7 +76,7 @@ public void setQuery(final String query) {
7676
*
7777
* @return the audit event
7878
*/
79-
public AuditEvent getAuditEvent() {
79+
public T getAuditEvent() {
8080
return auditEvent;
8181
}
8282

@@ -86,7 +86,7 @@ public AuditEvent getAuditEvent() {
8686
* @param auditEvent
8787
* the new audit event
8888
*/
89-
public void setAuditEvent(final AuditEvent auditEvent) {
89+
public void setAuditEvent(final T auditEvent) {
9090
this.auditEvent = auditEvent;
9191
}
9292

0 commit comments

Comments
 (0)