Skip to content

Commit 10d23db

Browse files
committed
Code refactoring, DateTime formatter api, Buffered array list
1 parent 44cc59b commit 10d23db

31 files changed

+946
-683
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,10 @@ public boolean audit(AnnotationAuditEvent annotationEvent) {
115115
* @return single instance of AuditHelper
116116
*/
117117
public static AuditManager getInstance() {
118-
if (auditManager == null) {
119-
synchronized (AuditManager.class) {
120-
if (auditManager == null) {
121-
auditManager = new AuditManager();
122-
init();
123-
}
118+
synchronized (AuditManager.class) {
119+
if (auditManager == null) {
120+
auditManager = new AuditManager();
121+
init();
124122
}
125123
}
126124
return auditManager;
@@ -137,7 +135,7 @@ public static AuditManager getInstance() {
137135
*
138136
* @since 2.1.0
139137
*/
140-
138+
141139
public static AuditManager getConfigurationInstance(Configuration configuration) {
142140
Context.setConfig(configuration);
143141
return getInstance();

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
package org.audit4j.core;
2020

21-
import java.lang.reflect.Method;
22-
2321
import org.audit4j.core.dto.AuditBase;
2422
import org.audit4j.core.dto.AuditEvent;
2523
import org.audit4j.core.exception.HandlerException;
@@ -80,35 +78,6 @@ protected void executeHandlers(AuditEvent event) {
8078
}
8179
}
8280

83-
/**
84-
* Gets the actor.
85-
*
86-
* @param handler
87-
* the handler
88-
* @return the actor
89-
*/
90-
@Deprecated
91-
protected String getActor(final Handler handler) {
92-
return configContext.getMetaData().getActor();
93-
}
94-
95-
/**
96-
* Gets the action.
97-
*
98-
* @param action
99-
* the action
100-
* @param method
101-
* the method
102-
* @return the action
103-
*/
104-
@Deprecated
105-
protected String getAction(String action, Method method) {
106-
if (action.equals(CoreConstants.ACTION.equals(action))) {
107-
return method.getName();
108-
}
109-
return action;
110-
}
111-
11281
public void setConfigContext(ConcurrentConfigurationContext configContext) {
11382
this.configContext = configContext;
11483
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@
3333
* Configuration item store. All configuration items which are used by the
3434
* running components store here. Items loaded by the context.
3535
*
36-
* <p/>Below Items store here:
37-
* </p>
36+
* <p>Below Items store here:</p>
3837
* <ul>
3938
* <li>Available handlers.</li>
4039
* <li>Audit event filters.</li>
@@ -43,7 +42,7 @@
4342
* <li>Additional configuration properties.</li>
4443
* </ul>
4544
*
46-
* </p> #ThreadSafe#
45+
* <p> #ThreadSafe# </p>
4746
*
4847
* @author <a href="mailto:[email protected]">Janith Bandara</a>
4948
*/

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.io.FileWriter;
2626
import java.io.IOException;
2727
import java.util.ArrayList;
28-
import java.util.Date;
2928
import java.util.HashMap;
3029
import java.util.List;
3130
import java.util.Map;
@@ -62,8 +61,6 @@ private ConfigUtil() {
6261
*/
6362
static Configuration createDummyConfig() {
6463
Configuration conf = new Configuration();
65-
conf.setVersion("1.0.0.RELEASE");
66-
conf.setReleased(new Date());
6764
List<Handler> handlers = new ArrayList<>();
6865
/*DatabaseAuditHandler handler = new DatabaseAuditHandler();
6966
handler.setEmbedded("true");

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

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.Serializable;
2222
import java.util.ArrayList;
23-
import java.util.Date;
2423
import java.util.HashMap;
2524
import java.util.List;
2625
import java.util.Map;
@@ -41,12 +40,6 @@ public class Configuration implements Serializable {
4140
/** The Constant serialVersionUID. */
4241
private static final long serialVersionUID = -3370288956459623002L;
4342

44-
/** The released. */
45-
Date released;
46-
47-
/** The version. */
48-
String version;
49-
5043
/** The layout. */
5144
private Layout layout;
5245

@@ -71,45 +64,7 @@ public class Configuration implements Serializable {
7164
public Configuration() {
7265

7366
}
74-
75-
/**
76-
* Gets the released.
77-
*
78-
* @return the released
79-
*/
80-
public Date getReleased() {
81-
return released;
82-
}
83-
84-
/**
85-
* Sets the released.
86-
*
87-
* @param released
88-
* the new released
89-
*/
90-
public void setReleased(Date released) {
91-
this.released = released;
92-
}
93-
94-
/**
95-
* Gets the version.
96-
*
97-
* @return the version
98-
*/
99-
public String getVersion() {
100-
return version;
101-
}
102-
103-
/**
104-
* Sets the version.
105-
*
106-
* @param version
107-
* the new version
108-
*/
109-
public void setVersion(String version) {
110-
this.version = version;
111-
}
112-
67+
11368
/**
11469
* Gets the meta data.
11570
*

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

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@
3535
import org.audit4j.core.io.AuditEventOutputStream;
3636
import org.audit4j.core.io.AuditOutputStream;
3737
import org.audit4j.core.io.AuditProcessOutputStream;
38+
import org.audit4j.core.util.EnvUtil;
3839
import org.audit4j.core.util.Log;
3940
import org.audit4j.core.util.StopWatch;
4041

4142
/**
4243
* The Audit4j Context. This will load and execute required resources in to the
4344
* memory when initializing audit4j, Context makes sure necessary resources
4445
* provide when running audit4j. And this will release the memory allocated by
45-
* audit4j when shutdown the audit4j.
46+
* audit4j when shutdown.
4647
*
47-
* <p/>
48+
* <p>
4849
* Available public methods:
4950
* </p>
5051
* <ul>
@@ -53,7 +54,10 @@
5354
* Audit4j</li>
5455
* <li>{@link #enable()} Enable audit4j.</li>
5556
* <li>{@link #disable()} Disable audit4j.</li>
56-
* <li>{@link #getConfigContext()} get audit4j configurations.</li>
57+
* <li>{@link #terminate()} Terminate audit4j.</li>
58+
* <li>{@link #getConfigContext()} get initialized configurations.</li>
59+
* <li>{@link #setConfig(Configuration conf)} set initial and external configurations in to
60+
* context.</li>
5761
* </ul>
5862
*
5963
* @author <a href="mailto:[email protected]">Janith Bandara</a>
@@ -80,6 +84,9 @@ public final class Context {
8084
/** The config context. */
8185
private static ConcurrentConfigurationContext configContext;
8286

87+
/** The Constant INIT_FAILED. */
88+
private static final String INIT_FAILED = "initialization failed.!!";
89+
8390
/**
8491
* Initialize the Audit4j instance. This will ensure the single audit4j
8592
* instance and single Configuration repository load in to the memory.
@@ -92,21 +99,24 @@ final static void init() {
9299
&& (configContext.getRunStatus().equals(RunStatus.READY) || configContext.getRunStatus().equals(
93100
RunStatus.STOPPED))) {
94101
Log.info("Initializing Audit4j...");
102+
// Check system environment;
103+
checkEnvironment();
95104
Log.info("Loading Configurations...");
105+
96106
if (conf == null) {
97107
loadConfig();
98108
}
99109
Log.info("Validating Configurations...");
100110
if (conf == null) {
101-
configContext.setRunStatus(RunStatus.TERMINATED);
102-
throw new InitializationException("initialization failed.!!");
111+
terminate();
112+
throw new InitializationException(INIT_FAILED);
103113
}
104114

105115
try {
106116
ValidationManager.validateConfigurations(conf);
107117
} catch (ValidationException e1) {
108-
configContext.setRunStatus(RunStatus.TERMINATED);
109-
throw new InitializationException("initialization failed.!!", e1);
118+
terminate();
119+
throw new InitializationException(INIT_FAILED, e1);
110120
}
111121

112122
// Extract options.
@@ -147,7 +157,7 @@ final static void init() {
147157

148158
stopWatch.stop();
149159
Long initializationTime = stopWatch.getLastTaskTimeMillis();
150-
Log.info("Audit4j initialized. Total time: " + initializationTime + "ms");
160+
Log.info("Audit4j initialized. Total time: ", initializationTime, "ms");
151161
}
152162
}
153163

@@ -166,13 +176,12 @@ final static void stop() {
166176
annotationAuditStream.close();
167177

168178
Log.info("Shutdown handlers...");
169-
for (Handler handler : conf.getHandlers()) {
179+
for (Handler handler : configContext.getHandlers()) {
170180
handler.stop();
171181
Log.info(handler.getClass().getName() + " shutdown.");
172182
}
173183

174184
Log.info("Disposing configurations...");
175-
conf = null;
176185
initialized = false;
177186
configContext.setRunStatus(RunStatus.STOPPED);
178187
Log.info("Audit4j shutdown completed.");
@@ -205,6 +214,16 @@ final static void disable() {
205214
configContext.setRunStatus(RunStatus.DISABLED);
206215
}
207216

217+
/**
218+
* Terminate Audit4j core services.
219+
*
220+
* @since 2.3.0
221+
*/
222+
final static void terminate() {
223+
Log.warn("Audit4j Terminated due to critical error.");
224+
configContext.setRunStatus(RunStatus.TERMINATED);
225+
}
226+
208227
/**
209228
* Gets the config context.
210229
*
@@ -214,6 +233,21 @@ static ConcurrentConfigurationContext getConfigContext() {
214233
return configContext;
215234
}
216235

236+
/**
237+
* Check environment.
238+
*
239+
* @since 2.3.0
240+
*/
241+
private final static void checkEnvironment() {
242+
// Check java support.!
243+
boolean javaSupport = EnvUtil.isJDK7OrHigher();
244+
if (!javaSupport) {
245+
Log.error("Your Java version (", EnvUtil.getJavaersion(), ") is not supported for Audit4j. ",
246+
ErrorGuide.getGuide(ErrorGuide.JAVA_VERSION_ERROR));
247+
throw new InitializationException("Java version is not supported.");
248+
}
249+
}
250+
217251
/**
218252
* Load configuration items.
219253
*/
@@ -228,11 +262,11 @@ private final static void loadConfig() {
228262
TroubleshootManager.troubleshootConfiguration(e);
229263
conf = ConfigUtil.readConfig(configFilePath);
230264
} catch (TroubleshootException e2) {
231-
configContext.setRunStatus(RunStatus.TERMINATED);
232-
throw new InitializationException("Initialization failed.!!", e2);
265+
terminate();
266+
throw new InitializationException(INIT_FAILED);
233267
} catch (ConfigurationException e1) {
234-
configContext.setRunStatus(RunStatus.TERMINATED);
235-
throw new InitializationException("Initialization failed.!!", e1);
268+
terminate();
269+
throw new InitializationException(INIT_FAILED);
236270
}
237271
}
238272
}
@@ -253,8 +287,9 @@ private static Map<String, String> processOptions(String optionText) {
253287
String[] args = extractOptions(optionText);
254288
for (String arg : args) {
255289
String[] option = StringUtils.split(arg, CoreConstants.EQ_CHAR);
256-
if (!Registry.getOptions().contains(option[0])) {
257-
Log.warn("Invalid option: " + option[0] + " Please check your configurations.");
290+
if (!PreConfigurationContext.getOptions().contains(option[0])) {
291+
Log.warn("Invalid option: ", option[0], " Please check your configurations. ",
292+
ErrorGuide.getGuide(ErrorGuide.INVALID_OPTION));
258293
}
259294
options.put(option[0], option[1]);
260295
}
@@ -278,11 +313,12 @@ private static String[] extractOptions(String optionText) {
278313
* @since 2.3.0
279314
*/
280315
private static void loadRegistry() {
281-
for (AuditEventFilter filter : Registry.getPrefilters()) {
316+
// Load audit filters to runtime configurations.
317+
for (AuditEventFilter filter : PreConfigurationContext.getPrefilters()) {
282318
configContext.addFilter(filter);
283319
}
284-
285-
for (AuditAnnotationFilter annotationFilter : Registry.getPreannotationfilters()) {
320+
// Load audit annotation filters to runtime configurations.
321+
for (AuditAnnotationFilter annotationFilter : PreConfigurationContext.getPreannotationfilters()) {
286322
configContext.addAnnotationFilter(annotationFilter);
287323
}
288324
}
@@ -292,11 +328,6 @@ private static void loadRegistry() {
292328
*/
293329
private static void initHandlers() {
294330
Log.info("Initializing Handlers...");
295-
if (conf.getHandlers() == null || conf.getHandlers().isEmpty()) {
296-
Log.error("No handlers found in the config file.");
297-
disable();
298-
return;
299-
}
300331
for (Handler handler : conf.getHandlers()) {
301332
try {
302333
if (!configContext.getHandlers().contains(handler)) {
@@ -306,8 +337,10 @@ private static void initHandlers() {
306337
}
307338
Log.info(handler.getClass().getName() + " Initialized.");
308339
} catch (InitializationException e) {
309-
configContext.setRunStatus(RunStatus.TERMINATED);
310-
throw new InitializationException("initialization failed.!!", e);
340+
Log.error("There is a problem in the hander: ", handler.getClass().getName(),
341+
ErrorGuide.getGuide(ErrorGuide.HANDLER_ERROR));
342+
terminate();
343+
throw new InitializationException(INIT_FAILED, e);
311344
}
312345
}
313346
}
@@ -322,8 +355,10 @@ private static void initLayout() {
322355
configContext.setLayout(conf.getLayout());
323356
Log.info(conf.getLayout().getClass().getName() + " Initialized.");
324357
} catch (InitializationException e) {
325-
configContext.setRunStatus(RunStatus.TERMINATED);
326-
throw new InitializationException("initialization failed.!!", e);
358+
Log.error("There is a problem in the layout: ", conf.getLayout().getClass().getName(), " you configured.",
359+
ErrorGuide.getGuide(ErrorGuide.LAYOUT_ERROR));
360+
terminate();
361+
throw new InitializationException(INIT_FAILED);
327362
}
328363

329364
}
@@ -395,7 +430,7 @@ final static AnnotationAuditOutputStream getAnnotationStream() {
395430
*
396431
* @return true, if is initialized
397432
*
398-
* @Deprecated use {@link #getStatus()} instead.
433+
* @deprecated use {@link #getStatus()} instead.
399434
*/
400435
@Deprecated
401436
public boolean isInitialized() {

0 commit comments

Comments
 (0)