35
35
import org .audit4j .core .io .AuditEventOutputStream ;
36
36
import org .audit4j .core .io .AuditOutputStream ;
37
37
import org .audit4j .core .io .AuditProcessOutputStream ;
38
+ import org .audit4j .core .util .EnvUtil ;
38
39
import org .audit4j .core .util .Log ;
39
40
import org .audit4j .core .util .StopWatch ;
40
41
41
42
/**
42
43
* The Audit4j Context. This will load and execute required resources in to the
43
44
* memory when initializing audit4j, Context makes sure necessary resources
44
45
* provide when running audit4j. And this will release the memory allocated by
45
- * audit4j when shutdown the audit4j .
46
+ * audit4j when shutdown.
46
47
*
47
- * <p/ >
48
+ * <p>
48
49
* Available public methods:
49
50
* </p>
50
51
* <ul>
53
54
* Audit4j</li>
54
55
* <li>{@link #enable()} Enable audit4j.</li>
55
56
* <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>
57
61
* </ul>
58
62
*
59
63
* @author <a href="mailto:[email protected] ">Janith Bandara</a>
@@ -80,6 +84,9 @@ public final class Context {
80
84
/** The config context. */
81
85
private static ConcurrentConfigurationContext configContext ;
82
86
87
+ /** The Constant INIT_FAILED. */
88
+ private static final String INIT_FAILED = "initialization failed.!!" ;
89
+
83
90
/**
84
91
* Initialize the Audit4j instance. This will ensure the single audit4j
85
92
* instance and single Configuration repository load in to the memory.
@@ -92,21 +99,24 @@ final static void init() {
92
99
&& (configContext .getRunStatus ().equals (RunStatus .READY ) || configContext .getRunStatus ().equals (
93
100
RunStatus .STOPPED ))) {
94
101
Log .info ("Initializing Audit4j..." );
102
+ // Check system environment;
103
+ checkEnvironment ();
95
104
Log .info ("Loading Configurations..." );
105
+
96
106
if (conf == null ) {
97
107
loadConfig ();
98
108
}
99
109
Log .info ("Validating Configurations..." );
100
110
if (conf == null ) {
101
- configContext . setRunStatus ( RunStatus . TERMINATED );
102
- throw new InitializationException ("initialization failed.!!" );
111
+ terminate ( );
112
+ throw new InitializationException (INIT_FAILED );
103
113
}
104
114
105
115
try {
106
116
ValidationManager .validateConfigurations (conf );
107
117
} catch (ValidationException e1 ) {
108
- configContext . setRunStatus ( RunStatus . TERMINATED );
109
- throw new InitializationException ("initialization failed.!!" , e1 );
118
+ terminate ( );
119
+ throw new InitializationException (INIT_FAILED , e1 );
110
120
}
111
121
112
122
// Extract options.
@@ -147,7 +157,7 @@ final static void init() {
147
157
148
158
stopWatch .stop ();
149
159
Long initializationTime = stopWatch .getLastTaskTimeMillis ();
150
- Log .info ("Audit4j initialized. Total time: " + initializationTime + "ms" );
160
+ Log .info ("Audit4j initialized. Total time: " , initializationTime , "ms" );
151
161
}
152
162
}
153
163
@@ -166,13 +176,12 @@ final static void stop() {
166
176
annotationAuditStream .close ();
167
177
168
178
Log .info ("Shutdown handlers..." );
169
- for (Handler handler : conf .getHandlers ()) {
179
+ for (Handler handler : configContext .getHandlers ()) {
170
180
handler .stop ();
171
181
Log .info (handler .getClass ().getName () + " shutdown." );
172
182
}
173
183
174
184
Log .info ("Disposing configurations..." );
175
- conf = null ;
176
185
initialized = false ;
177
186
configContext .setRunStatus (RunStatus .STOPPED );
178
187
Log .info ("Audit4j shutdown completed." );
@@ -205,6 +214,16 @@ final static void disable() {
205
214
configContext .setRunStatus (RunStatus .DISABLED );
206
215
}
207
216
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
+
208
227
/**
209
228
* Gets the config context.
210
229
*
@@ -214,6 +233,21 @@ static ConcurrentConfigurationContext getConfigContext() {
214
233
return configContext ;
215
234
}
216
235
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
+
217
251
/**
218
252
* Load configuration items.
219
253
*/
@@ -228,11 +262,11 @@ private final static void loadConfig() {
228
262
TroubleshootManager .troubleshootConfiguration (e );
229
263
conf = ConfigUtil .readConfig (configFilePath );
230
264
} catch (TroubleshootException e2 ) {
231
- configContext . setRunStatus ( RunStatus . TERMINATED );
232
- throw new InitializationException ("Initialization failed.!!" , e2 );
265
+ terminate ( );
266
+ throw new InitializationException (INIT_FAILED );
233
267
} catch (ConfigurationException e1 ) {
234
- configContext . setRunStatus ( RunStatus . TERMINATED );
235
- throw new InitializationException ("Initialization failed.!!" , e1 );
268
+ terminate ( );
269
+ throw new InitializationException (INIT_FAILED );
236
270
}
237
271
}
238
272
}
@@ -253,8 +287,9 @@ private static Map<String, String> processOptions(String optionText) {
253
287
String [] args = extractOptions (optionText );
254
288
for (String arg : args ) {
255
289
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 ));
258
293
}
259
294
options .put (option [0 ], option [1 ]);
260
295
}
@@ -278,11 +313,12 @@ private static String[] extractOptions(String optionText) {
278
313
* @since 2.3.0
279
314
*/
280
315
private static void loadRegistry () {
281
- for (AuditEventFilter filter : Registry .getPrefilters ()) {
316
+ // Load audit filters to runtime configurations.
317
+ for (AuditEventFilter filter : PreConfigurationContext .getPrefilters ()) {
282
318
configContext .addFilter (filter );
283
319
}
284
-
285
- for (AuditAnnotationFilter annotationFilter : Registry .getPreannotationfilters ()) {
320
+ // Load audit annotation filters to runtime configurations.
321
+ for (AuditAnnotationFilter annotationFilter : PreConfigurationContext .getPreannotationfilters ()) {
286
322
configContext .addAnnotationFilter (annotationFilter );
287
323
}
288
324
}
@@ -292,11 +328,6 @@ private static void loadRegistry() {
292
328
*/
293
329
private static void initHandlers () {
294
330
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
- }
300
331
for (Handler handler : conf .getHandlers ()) {
301
332
try {
302
333
if (!configContext .getHandlers ().contains (handler )) {
@@ -306,8 +337,10 @@ private static void initHandlers() {
306
337
}
307
338
Log .info (handler .getClass ().getName () + " Initialized." );
308
339
} 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 );
311
344
}
312
345
}
313
346
}
@@ -322,8 +355,10 @@ private static void initLayout() {
322
355
configContext .setLayout (conf .getLayout ());
323
356
Log .info (conf .getLayout ().getClass ().getName () + " Initialized." );
324
357
} 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 );
327
362
}
328
363
329
364
}
@@ -395,7 +430,7 @@ final static AnnotationAuditOutputStream getAnnotationStream() {
395
430
*
396
431
* @return true, if is initialized
397
432
*
398
- * @Deprecated use {@link #getStatus()} instead.
433
+ * @deprecated use {@link #getStatus()} instead.
399
434
*/
400
435
@ Deprecated
401
436
public boolean isInitialized () {
0 commit comments