Skip to content

Commit 93b83ea

Browse files
committed
Removed handlers from annotation
1 parent 0cab07b commit 93b83ea

23 files changed

+306
-229
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3+
* An open-source audit platform for Enterprise java platform.
4+
* http://mechanizedspace.com/audit4j
5+
* http://audit4j.org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.audit4j.core;
21+
22+
/**
23+
* A factory for creating AbstractConfiguration objects.
24+
*
25+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
26+
*/
27+
public abstract class AbstractConfigurationFactory {
28+
29+
/**
30+
* Gets the audit level.
31+
*
32+
* @return the audit level
33+
*/
34+
public abstract String getAuditLevel();
35+
}

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

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.audit4j.core.annotation.SelectionType;
2727
import org.audit4j.core.dto.AnnotationAuditEvent;
2828
import org.audit4j.core.dto.AuditEvent;
29-
import org.audit4j.core.dto.Element;
29+
import org.audit4j.core.dto.Field;
3030
import org.audit4j.core.handler.Handler;
3131

3232

@@ -57,37 +57,33 @@ public void process(AnnotationAuditEvent auditDto) {
5757
final AsyncAuditAnnotationAttributes attributes = new AsyncAuditAnnotationAttributes();
5858
final AuditFieldAnnotationAttribute fieldAttributes = new AuditFieldAnnotationAttribute();
5959
List<Handler> handlers = null;
60-
List<Element> actionItems = null;
60+
List<Field> fields = null;
6161
AuditEvent event = new AuditEvent();
6262
String action = "";
63-
String methodName = "";
64-
6563
if (attributes.hasAnnotation(auditDto.getClass())) {
66-
handlers = attributes.getHandlers(auditDto.getClass());
64+
handlers = Context.getHandlers();
6765

68-
final String selection = attributes.getSelection(auditDto.getClass());
66+
final SelectionType selection = attributes.getSelection(auditDto.getClass());
6967
if (selection.equals(SelectionType.ALL)) {
70-
actionItems = fieldAttributes.getAllActionItems(auditDto.getMethod(), auditDto.getArgs());
68+
fields = fieldAttributes.getAllFields(auditDto.getMethod(), auditDto.getArgs());
7169
} else if (selection.equals(SelectionType.MARKED)) {
72-
actionItems = fieldAttributes.getMarkedActionItems(auditDto.getMethod(), auditDto.getArgs());
70+
fields = fieldAttributes.getMarkedFields(auditDto.getMethod(), auditDto.getArgs());
7371
}
7472
action = attributes.getAction(auditDto.getMethod().getClass(), auditDto.getMethod());
75-
methodName = auditDto.getMethod().getName();
7673
} else if (attributes.hasAnnotation(auditDto.getMethod())) {
77-
handlers = attributes.getHandlers(auditDto.getMethod());
78-
final String selection = attributes.getSelection(auditDto.getMethod());
74+
handlers = Context.getHandlers();
75+
final SelectionType selection = attributes.getSelection(auditDto.getMethod());
7976
if (selection.equals(SelectionType.ALL)) {
80-
actionItems = fieldAttributes.getAllActionItems(auditDto.getMethod(), auditDto.getArgs());
77+
fields = fieldAttributes.getAllFields(auditDto.getMethod(), auditDto.getArgs());
8178
} else if (selection.equals(SelectionType.MARKED)) {
82-
actionItems = fieldAttributes.getMarkedActionItems(auditDto.getMethod(), auditDto.getArgs());
79+
fields = fieldAttributes.getMarkedFields(auditDto.getMethod(), auditDto.getArgs());
8380
}
8481
action = attributes.getAction(auditDto.getMethod());
85-
methodName = auditDto.getMethod().getName();
8682
}
8783

88-
final String query = super.buildQuery(actionItems, action);
84+
final String query = super.buildQuery(fields, action);
8985
event.setAction(action);
90-
event.setEventElements(actionItems);
86+
event.setFields(fields);
9187
super.executeHandlers(handlers, event, query);
9288
}
9389

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.audit4j.core;
2121

2222
import org.audit4j.core.dto.AsyncCallAuditDto;
23+
import org.audit4j.core.exception.HandlerException;
2324
import org.audit4j.core.handler.Handler;
2425

2526

@@ -47,7 +48,11 @@ public void process(AsyncCallAuditDto auditDto) {
4748
for (final Handler handler : auditDto.getHandlers()) {
4849
handler.setQuery(AuditUtil.buildQuery(AuditUtil.transformMap(auditDto.getParamMap()), auditDto.getAction()));
4950
// handler.setParameters(AuditUtil.transformMap(auditDto.getParamMap()));
50-
handler.handle();
51+
try {
52+
handler.handle();
53+
} catch (HandlerException e) {
54+
Log.warn("Failed to submit audit event.");
55+
}
5156
}
5257
}
5358

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.audit4j.core.dto.AsyncAuditMessage;
3030
import org.audit4j.core.dto.AsyncCallAuditDto;
3131
import org.audit4j.core.dto.AuditEvent;
32+
import org.audit4j.core.exception.HandlerException;
3233
import org.audit4j.core.exception.TroubleshootException;
3334
import org.audit4j.core.exception.ValidationException;
3435
import org.audit4j.core.handler.Handler;
@@ -170,7 +171,11 @@ public boolean audit(final List<Handler> handlers, final String action, final Ma
170171
// handler.setMethod(method);
171172
handler.setQuery(AuditUtil.buildQuery(AuditUtil.transformMap(paramMap), action));
172173
// handler.setParameters(AuditUtil.transformMap(paramMap));
173-
handler.handle();
174+
try {
175+
handler.handle();
176+
} catch (HandlerException e) {
177+
Log.warn("Failed to submit audit event.");
178+
}
174179
}
175180
return Boolean.TRUE;
176181
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
import org.audit4j.core.dto.AuditBase;
2626
import org.audit4j.core.dto.AuditEvent;
27-
import org.audit4j.core.dto.Element;
27+
import org.audit4j.core.dto.Field;
28+
import org.audit4j.core.exception.HandlerException;
2829
import org.audit4j.core.handler.Handler;
2930

3031
/**
@@ -56,13 +57,13 @@ public abstract class AuditProcessor<T extends AuditBase> {
5657
* @return the string
5758
*/
5859
@Deprecated
59-
protected String buildQuery(final List<Element> actionItems, String action) {
60+
protected String buildQuery(final List<Field> actionItems, String action) {
6061
if (actionItems != null && !actionItems.isEmpty()) {
6162
final StringBuilder buff = new StringBuilder();
6263
if (action != null) {
6364
buff.append(action).append(CoreConstants.ARROW);
6465
}
65-
for (Element actionItem : actionItems) {
66+
for (Field actionItem : actionItems) {
6667
buff.append(actionItem.getName()).append(CoreConstants.COLON_CHAR).append(actionItem.getValue())
6768
.append(CoreConstants.COMMA_CHAR);
6869
}
@@ -88,7 +89,11 @@ protected void executeHandlers(final List<Handler> handlers, AuditEvent event, f
8889

8990
handler.setAuditEvent(event);
9091
handler.setQuery(query);
91-
handler.handle();
92+
try {
93+
handler.handle();
94+
} catch (HandlerException e) {
95+
Log.warn("Failed to submit audit record.");
96+
}
9297
}
9398
}
9499

@@ -107,7 +112,11 @@ protected void executeHandlers(AuditEvent event) {
107112
for (final Handler handler : getConf().getHandlers()) {
108113
handler.setAuditEvent(event);
109114
handler.setQuery(getConf().getLayout().format(event));
110-
handler.handle();
115+
try {
116+
handler.handle();
117+
} catch (HandlerException e) {
118+
Log.warn("Failed to submit audit event.");
119+
}
111120
}
112121
}
113122

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.audit4j.core;
2121

22+
import java.util.List;
2223
import java.util.Map;
2324

2425
import org.audit4j.core.exception.ConfigurationException;
@@ -28,7 +29,7 @@
2829

2930
/**
3031
* The Class Context.
31-
*
32+
*
3233
* @author <a href="mailto:[email protected]">Janith Bandara</a>
3334
*/
3435
public class Context {
@@ -69,11 +70,11 @@ private static void init() {
6970
throw new InitializationException("Configuration initialization failed.");
7071
} else {
7172
for (Map.Entry<String, String> entry : conf.getProperties().entrySet()) {
72-
if (System.getProperties().containsKey(entry.getValue())) {
73+
if (System.getProperties().containsKey(entry.getValue())) {
7374
conf.getProperties().put(entry.getKey(), System.getProperty(entry.getValue()));
7475
}
7576
}
76-
77+
7778
System.out.println("Initializing Handlers..");
7879
for (Handler handler : conf.getHandlers()) {
7980
try {
@@ -91,11 +92,32 @@ private static void init() {
9192

9293
/**
9394
* Gets the config.
94-
*
95+
*
9596
* @return the config
9697
*/
9798
public static Configuration getConfig() {
9899
init();
99100
return conf;
100101
}
102+
103+
/**
104+
* Gets the handlers.
105+
*
106+
* @return the handlers
107+
*/
108+
public static List<Handler> getHandlers() {
109+
return getConfig().getHandlers();
110+
}
111+
112+
/**
113+
* Checks if is initialized.
114+
*
115+
* @return true, if is initialized
116+
*/
117+
public boolean isInitialized() {
118+
if (null == conf) {
119+
return false;
120+
}
121+
return true;
122+
}
101123
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
*/
2424
public final class CoreConstants {
2525

26+
public static final String APP_NAME = "Audit4j";
27+
2628
public static final String RELEASE_VERSION = "1.0.0.RELEASE";
2729

2830
public static final String RELEASE_DATE = "2014-06-15T11:17:21.077Z";
@@ -85,4 +87,6 @@ public final class CoreConstants {
8587
+ "\nversion: "
8688
+ RELEASE_VERSION
8789
+ "\nhandlers:\n- !org.audit4j.core.handler.db.GeneralDatabaseAuditHandler {}\nlayout:\n!org.audit4j.core.SimpleLayout {}\nmetaData: !org.audit4j.core.DummyMetaData {}\nproperties:\n- {key: log.file.location, value: user.dir}";
90+
91+
public static final String EMBEDED_DB_NAME = "audit4j";
8892
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.audit4j.core;
2+
3+
/**
4+
* The Class Log.
5+
*
6+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
7+
*/
8+
public class Log {
9+
10+
/** The Constant AUDIT4J_INFO. */
11+
private static final String AUDIT4J_INFO = CoreConstants.APP_NAME + ":INFO";
12+
13+
/** The Constant AUDIT4J_WARN. */
14+
private static final String AUDIT4J_WARN = CoreConstants.APP_NAME + ":WARN";
15+
16+
/** The Constant AUDIT4J_ERROR. */
17+
private static final String AUDIT4J_ERROR = CoreConstants.APP_NAME + ":ERROR";
18+
19+
/**
20+
* Info.
21+
*
22+
* @param message the message
23+
*/
24+
public static void info(Object message) {
25+
System.out.println(AUDIT4J_INFO + message.toString());
26+
}
27+
28+
/**
29+
* Warn.
30+
*
31+
* @param message the message
32+
*/
33+
public static void warn(Object message) {
34+
System.err.println(AUDIT4J_WARN + message.toString());
35+
}
36+
37+
/**
38+
* Error.
39+
*
40+
* @param message the message
41+
*/
42+
public static void error(Object message) {
43+
System.err.println(AUDIT4J_ERROR + message.toString());
44+
}
45+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package org.audit4j.core;
2121

2222
import org.audit4j.core.dto.AuditEvent;
23-
import org.audit4j.core.dto.Element;
23+
import org.audit4j.core.dto.Field;
2424

2525
/**
2626
* The Class SimpleLayout.
@@ -43,12 +43,12 @@ public String format(AuditEvent event) {
4343
buff.append(CoreConstants.PIPE);
4444
buff.append(event.getOrigin());
4545
buff.append(CoreConstants.PIPE);
46-
if (event.getEventElements() != null && !event.getEventElements().isEmpty()) {
46+
if (event.getFields() != null && !event.getFields().isEmpty()) {
4747

4848
if (event.getAction() != null) {
4949
buff.append(event.getAction()).append(CoreConstants.ARROW);
5050
}
51-
for (Element actionItem : event.getEventElements()) {
51+
for (Field actionItem : event.getFields()) {
5252
buff.append(actionItem.getName()).append(CoreConstants.SPACE).append(actionItem.getType()).append(CoreConstants.COLON_CHAR)
5353
.append(actionItem.getValue()).append(CoreConstants.COMMA_CHAR);
5454
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.audit4j.core.annotation.SelectionType;
2727
import org.audit4j.core.dto.AnnotationAuditEvent;
2828
import org.audit4j.core.dto.AuditEvent;
29-
import org.audit4j.core.dto.Element;
29+
import org.audit4j.core.dto.Field;
3030
import org.audit4j.core.handler.Handler;
3131

3232

@@ -59,34 +59,34 @@ public void process(AnnotationAuditEvent auditDto) {
5959
final AuditAnnotationAttributes attributes = new AuditAnnotationAttributes();
6060
final AuditFieldAnnotationAttribute fieldAttributes = new AuditFieldAnnotationAttribute();
6161
List<Handler> handlers = null;
62-
List<Element> actionItems = null;
62+
List<Field> fields = null;
6363
AuditEvent event = new AuditEvent();
6464
String action = "";
6565

6666
if (attributes.hasAnnotation(auditDto.getClass())) {
67-
handlers = attributes.getHandlers(auditDto.getClass());
67+
handlers = Context.getHandlers();
6868

69-
final String selection = attributes.getSelection(auditDto.getClass());
69+
final SelectionType selection = attributes.getSelection(auditDto.getClass());
7070
if (selection.equals(SelectionType.ALL)) {
71-
actionItems = fieldAttributes.getAllActionItems(auditDto.getMethod(), auditDto.getArgs());
71+
fields = fieldAttributes.getAllFields(auditDto.getMethod(), auditDto.getArgs());
7272
} else if (selection.equals(SelectionType.MARKED)) {
73-
actionItems = fieldAttributes.getMarkedActionItems(auditDto.getMethod(), auditDto.getArgs());
73+
fields = fieldAttributes.getMarkedFields(auditDto.getMethod(), auditDto.getArgs());
7474
}
7575
action = attributes.getAction(auditDto.getMethod().getClass(), auditDto.getMethod());
7676
} else if (attributes.hasAnnotation(auditDto.getMethod())) {
77-
handlers = attributes.getHandlers(auditDto.getMethod());
78-
final String selection = attributes.getSelection(auditDto.getMethod());
77+
handlers = Context.getHandlers();
78+
final SelectionType selection = attributes.getSelection(auditDto.getMethod());
7979
if (selection.equals(SelectionType.ALL)) {
80-
actionItems = fieldAttributes.getAllActionItems(auditDto.getMethod(), auditDto.getArgs());
80+
fields = fieldAttributes.getAllFields(auditDto.getMethod(), auditDto.getArgs());
8181
} else if (selection.equals(SelectionType.MARKED)) {
82-
actionItems = fieldAttributes.getMarkedActionItems(auditDto.getMethod(), auditDto.getArgs());
82+
fields = fieldAttributes.getMarkedFields(auditDto.getMethod(), auditDto.getArgs());
8383
}
8484
action = attributes.getAction(auditDto.getMethod());
8585
}
8686

87-
final String query = super.buildQuery(actionItems, action);
87+
final String query = super.buildQuery(fields, action);
8888
event.setAction(super.getAction(action, auditDto.getMethod()));
89-
event.setEventElements(actionItems);
89+
event.setFields(fields);
9090
super.executeHandlers(handlers, event, query);
9191
}
9292

0 commit comments

Comments
 (0)