Skip to content

Commit f51a44b

Browse files
committed
Added reflection based toString implementation
2 parents 9f94049 + 620dd2a commit f51a44b

27 files changed

+575
-624
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public static AuditManager initWithConfiguration(Configuration configuration) {
153153
return getInstance();
154154
}
155155

156-
public static void shutdown() {
156+
public void shutdown() {
157157
Context.stop();
158158
}
159159
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ final static void init() {
136136
}
137137
}
138138
}
139-
139+
configContext.getProperties().putAll(conf.getProperties());
140+
140141
// Initialize handlers.
141142
initHandlers();
142143

@@ -331,7 +332,7 @@ private static void initHandlers() {
331332
for (Handler handler : conf.getHandlers()) {
332333
try {
333334
if (!configContext.getHandlers().contains(handler)) {
334-
handler.setProperties(conf.getProperties());
335+
handler.setProperties(configContext.getProperties());
335336
handler.init();
336337
configContext.addHandler(handler);
337338
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public final class CoreConstants {
5858

5959
/** The Constant DASH_CHAR. */
6060
public static final char DASH_CHAR = '-';
61+
62+
public static final char OPEN_BRACES_CHAR = '{';
63+
64+
public static final char CLOSE_BRACES_CHAR = '}';
65+
66+
public static final String BRACKETS = "[]";
67+
68+
public static final char OPEN_BRACKETS_CHAR = '[';
69+
70+
public static final char CLOSE_BRACKETS_CHAR = ']';
6171

6272
/** The Constant ARROW. */
6373
public static final String ARROW = "==>";
@@ -79,6 +89,8 @@ public final class CoreConstants {
7989

8090
/** The Constant NEW_LINE. */
8191
public static final String NEW_LINE = "\n";
92+
93+
public static final String NULL = "null";
8294

8395
/** The Constant DEFAULT_ACTOR. */
8496
public static final String DEFAULT_ACTOR = "Audit4j User";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* Registry for internal initial resource configurations.
3333
*
3434
* @author <a href="mailto:[email protected]">Janith Bandara</a>
35+
*
36+
* @since 2.3.0
3537
*/
3638
public final class PreConfigurationContext {
3739

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ public static void troubleshootEvent(AuditEvent event) {
4949
throw new TroubleshootException(
5050
"Invalid Audit event type,\n Audit4j: Audit Event should not null, This event will not be logged by the Audit4j.");
5151
} else if (event.getActor() == null) {
52-
if (Context.getConfigContext().getMetaData() == null || Context.getConfigContext().getMetaData().getClass().equals(DummyMetaData.class)) {
52+
if (Context.getConfigContext().getMetaData() == null
53+
|| Context.getConfigContext().getMetaData().getClass().equals(DummyMetaData.class)) {
5354
event.setActor(CoreConstants.DEFAULT_ACTOR);
54-
Log.warn("Audit4j:WARN If you are not parsing the actor to the AuditEvent,\n"
55-
+ "Audit4j:WARN you should make a your own AuditMetaData implementation. \n"
56-
+ "Audit4j:WARN otherwise actor will be hard coded as \"" + CoreConstants.DEFAULT_ACTOR
57-
+ "\" in the audit log. " + "\nAudit4j: See " + ErrorGuide.NULL_ACTOR + " for further details.");
55+
Log.warn("If you are not parsing the actor to the AuditEvent, "
56+
+ "you should make a your own AuditMetaData implementation. "
57+
+ "otherwise actor will be hard coded as \"" + CoreConstants.DEFAULT_ACTOR
58+
+ "\" in the audit log. See " + ErrorGuide.NULL_ACTOR + " for further details.");
5859

5960
} else {
6061
event.setActor(Context.getConfigContext().getMetaData().getActor());
@@ -95,4 +96,5 @@ public static boolean isWindows() {
9596
String os = System.getProperty("os.name");
9697
return os.startsWith("Windows");
9798
}
99+
98100
}

src/main/java/org/audit4j/core/annotation/AuditFieldAnnotationAttribute.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525

2626
import org.audit4j.core.dto.Field;
27+
import org.audit4j.core.util.ToString;
2728

2829
/**
2930
* The Class AuditFieldAnnotationAttribute.
@@ -54,7 +55,7 @@ public List<Field> getAllFields(final Method method, final Object[] arg1) {
5455
Class<?> paramType = null;
5556
for (final Annotation[] annotations : parameterAnnotations) {
5657
final Object object = arg1[i++];
57-
paramValue = object.toString();
58+
paramValue = ToString.toStringIfNotImplemented(object);
5859
paramType = object.getClass();
5960
for (final Annotation annotation : annotations) {
6061
if (annotation instanceof AuditField) {
@@ -101,7 +102,7 @@ public List<Field> getMarkedFields(final Method method, final Object[] arg1) {
101102
Class<?> paramType = null;
102103
for (final Annotation[] annotations : parameterAnnotations) {
103104
final Object object = arg1[i++];
104-
paramValue = object.toString();
105+
paramValue = ToString.toStringIfNotImplemented(object);
105106
paramType = object.getClass();
106107
for (final Annotation annotation : annotations) {
107108
if (annotation instanceof AuditField) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24+
import org.audit4j.core.util.ToString;
25+
2426
/**
2527
* The Class AuditEvent.
2628
*
@@ -162,7 +164,7 @@ public void addField(String name, Object value, Object type) {
162164
if (value == null) {
163165
this.fields.add(new Field(name, null, null));
164166
} else {
165-
this.fields.add(new Field(name, value.toString(), type.toString()));
167+
this.fields.add(new Field(name, ToString.toStringIfNotImplemented(value), type.toString()));
166168
}
167169
}
168170

@@ -178,7 +180,7 @@ public void addField(String name, Object value) {
178180
if (value == null) {
179181
this.fields.add(new Field(name, null, null));
180182
} else {
181-
this.fields.add(new Field(name, value.toString(), value.getClass().getName()));
183+
this.fields.add(new Field(name, ToString.toStringIfNotImplemented(value), value.getClass().getName()));
182184
}
183185
}
184186

src/main/java/org/audit4j/core/filter/AuditAnnotationFilter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,21 @@
2020

2121
import org.audit4j.core.dto.AnnotationAuditEvent;
2222

23+
/**
24+
* The Interface AuditAnnotationFilter.
25+
*
26+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
27+
*
28+
* @since 2.3.0
29+
*/
2330
public interface AuditAnnotationFilter {
2431

32+
/**
33+
* Accepts.
34+
*
35+
* @param annotationEvent the annotation event
36+
* @return true, if successful
37+
*/
2538
boolean accepts(AnnotationAuditEvent annotationEvent);
2639

2740
}

src/main/java/org/audit4j/core/filter/AuditEventFilter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@
2626
* The Interface AuditEventFilter.
2727
*
2828
* @author <a href="mailto:[email protected]">Janith Bandara</a>
29+
*
30+
* @since 2.2.0
2931
*/
3032
public interface AuditEventFilter extends Serializable {
3133

34+
/**
35+
* Accepts.
36+
*
37+
* @param event the event
38+
* @return true, if successful
39+
*/
3240
boolean accepts(AuditEvent event);
3341

3442
}

src/main/java/org/audit4j/core/filter/impl/ScanAnnotatedFilter.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,31 @@
2424
import org.audit4j.core.dto.AnnotationAuditEvent;
2525
import org.audit4j.core.filter.AuditAnnotationFilter;
2626

27+
/**
28+
* The Class ScanAnnotatedFilter.
29+
*
30+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
31+
*
32+
* @since 2.3.0
33+
*/
2734
public class ScanAnnotatedFilter implements AuditAnnotationFilter {
2835

36+
/** The classes. */
2937
public Set<Class<?>> classes = new HashSet<>();
3038

39+
/* (non-Javadoc)
40+
* @see org.audit4j.core.filter.AuditAnnotationFilter#accepts(org.audit4j.core.dto.AnnotationAuditEvent)
41+
*/
3142
@Override
3243
public boolean accepts(AnnotationAuditEvent auditDto) {
3344
return classes.contains(auditDto.getClazz());
3445
}
3546

47+
/**
48+
* Adds the class.
49+
*
50+
* @param clazz the clazz
51+
*/
3652
public void addClass(Class<?> clazz){
3753
classes.add(clazz);
3854
}

0 commit comments

Comments
 (0)