-
Notifications
You must be signed in to change notification settings - Fork 460
/
ManagedAuditLoggerImpl.java
1009 lines (865 loc) · 33.3 KB
/
ManagedAuditLoggerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* * JBoss, Home of Professional Open Source.
* * Copyright 2013, Red Hat, Inc., and individual contributors
* * as indicated by the @author tags. See the copyright.txt file in the
* * distribution for a full listing of individual contributors.
* *
* * This is free software; you can redistribute it and/or modify it
* * under the terms of the GNU Lesser General Public License as
* * published by the Free Software Foundation; either version 2.1 of
* * the License, or (at your option) any later version.
* *
* * This software is distributed in the hope that it will be useful,
* * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* * Lesser General Public License for more details.
* *
* * You should have received a copy of the GNU Lesser General Public
* * License along with this software; if not, write to the Free
* * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
*/
package org.jboss.as.controller.audit;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.jboss.as.controller.OperationContext.ResultAction;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.audit.SyslogAuditLogHandler.Facility;
import org.jboss.as.controller.logging.ControllerLogger;
import org.jboss.as.controller.registry.Resource;
import org.jboss.as.core.security.AccessMechanism;
import org.jboss.dmr.ModelNode;
/**
* Audit logger wrapper
*
* @author Brian Stansberry (c) 2012 Red Hat Inc.
* @author Kabir Khan
*/
public class ManagedAuditLoggerImpl implements ManagedAuditLogger, ManagedAuditLogger.AuditLogHandlerUpdater {
/** Maximum number of consecutive logging failures before we stop logging */
private static final short MAX_FAILURE_COUNT = 10;
private final List<ManagedAuditLoggerImpl> childImpls;
/** If we are the core audit logger, list the children */
private final ManagedAuditLogConfiguration config;
/** Guarded by config's auditLock - updates to the handlers */
private HandlerUpdateTask handlerUpdateTask;
/** Guarded by config's auditLock - the messages logged while in the QUEUEING state */
private final List<AuditLogItem> queuedItems = new ArrayList<AuditLogItem>();
/** Guarded by config's auditLock - the number of failures writing to the log */
private short failureCount;
/** Whether we can ignore logging without taking the lock to check.
* Reliant on loggerStatus not being changed elsewhere, since it is not shared
* Only change with lock held
* Must be reset to false when handler updates need to be performed */
private final AtomicBoolean runDisabledFastPath = new AtomicBoolean(false);
public ManagedAuditLoggerImpl(String asVersion, boolean server) {
config = new CoreAuditLogConfiguration(asVersion, server);
childImpls = new ArrayList<ManagedAuditLoggerImpl>();
}
private ManagedAuditLoggerImpl(ManagedAuditLoggerImpl src, boolean manualCommit) {
assert src.config instanceof CoreAuditLogConfiguration : "Not an instance of CoreAuditLogConfiguration";
config = new NewAuditLogConfiguration((CoreAuditLogConfiguration)src.config, manualCommit);
childImpls = null;
}
@Override
public void log(boolean readOnly, ResultAction resultAction, String userId, String domainUUID, AccessMechanism accessMechanism,
InetAddress remoteAddress, Resource resultantModel, List<ModelNode> operations) {
if (runDisabledFastPath.get())
return;
config.lock();
try {
if (skipLogging(readOnly)) {
return;
}
storeLogItem(
AuditLogItem.createModelControllerItem(config.getAsVersion(), readOnly, config.isBooting(), resultAction, userId, domainUUID,
accessMechanism, remoteAddress, resultantModel, operations));
} catch (Exception e) {
handleLoggingException(e);
} finally {
applyHandlerUpdates();
config.unlock();
}
}
@Override
public void logJmxMethodAccess(boolean readOnly, String userId, String domainUUID, AccessMechanism accessMechanism,
InetAddress remoteAddress, String methodName, String[] methodSignature, Object[] methodParams, Throwable error) {
if (runDisabledFastPath.get())
return;
config.lock();
try {
if (skipLogging(readOnly)) {
return;
}
storeLogItem(
AuditLogItem.createMethodAccessItem(config.getAsVersion(), readOnly, config.isBooting(), userId, domainUUID, accessMechanism,
remoteAddress, methodName, methodSignature, methodParams, error));
} catch (Exception e) {
handleLoggingException(e);
} finally {
applyHandlerUpdates();
config.unlock();
}
}
private boolean skipLogging(boolean readOnly) {
if (config.isBooting() && !isLogBoot() || readOnly && !isLogReadOnly()) {
if (getLoggerStatus() == Status.DISABLED) {
// switch to the fast path for the next event
runDisabledFastPath.set(true);
}
return true;
}
return false;
}
public ManagedAuditLoggerImpl createNewConfiguration(boolean manualCommit) {
if (childImpls == null) {
throw ControllerLogger.ROOT_LOGGER.canOnlyCreateChildAuditLoggerForMainAuditLogger();
}
ManagedAuditLoggerImpl child = new ManagedAuditLoggerImpl(this, manualCommit);
childImpls.add(child);
return child;
}
public boolean isLogReadOnly() {
config.lock();
try {
return config.isLogReadOnly();
} finally {
config.unlock();
}
}
public void setLogReadOnly(boolean logReadOnly) {
config.lock();
try {
config.setLogReadOnly(logReadOnly);
} finally {
config.unlock();
}
}
public boolean isLogBoot() {
config.lock();
try {
return config.isLogBoot();
} finally {
config.unlock();
}
}
public void setLogBoot(boolean logBoot) {
config.lock();
try {
config.setLogBoot(logBoot);
if (logBoot == false && config.isBooting() && !config.isLogBoot()) {
queuedItems.clear();
}
} finally {
config.unlock();
}
}
public Status getLoggerStatus() {
config.lock();
try {
return config.getLoggerStatus();
} finally {
config.unlock();
}
}
@Override
public void recycleHandler(String name) {
config.lock();
try {
config.recycleHandler(name);
} finally {
config.unlock();
}
}
@Override
public void setLoggerStatus(final Status newStatus) {
config.lock();
try {
if (newStatus == Status.DISABLE_NEXT && config.getLoggerStatus() == Status.DISABLED) {
return;
}
config.setLoggerStatus(newStatus);
if (newStatus == Status.LOGGING){
for (AuditLogItem record : queuedItems) {
try {
writeLogItem(record);
} catch (Exception e) {
handleLoggingException(e);
}
}
} else if (newStatus == Status.DISABLED){
queuedItems.clear();
}
runDisabledFastPath.set(false);
} finally {
config.unlock();
}
}
/** protected by config's audit lock */
private void storeLogItem(AuditLogItem item) throws IOException {
switch (getLoggerStatus()) {
case QUEUEING:
queuedItems.add(item);
break;
case LOGGING:
writeLogItem(item);
break;
case DISABLE_NEXT:
writeLogItem(item);
config.setLoggerStatus(Status.DISABLED);
case DISABLED:
// switch to the fast path for the next event
runDisabledFastPath.set(true);
break;
}
}
/** protected by config's audit lock */
private void writeLogItem(AuditLogItem item) throws IOException{
Set<String> formatterNames = new HashSet<String>();
try {
for (AuditLogHandler handler : config.getHandlersForLogging()) {
formatterNames.add(handler.getFormatterName());
handler.writeLogItem(item);
}
} finally {
for (String formatterName : formatterNames) {
config.getFormatter(formatterName).clear();
}
}
}
/** protected by config's audit lock */
private void handleLoggingException(final Exception e) {
ControllerLogger.MGMT_OP_LOGGER.failedToUpdateAuditLog(e);
if (++failureCount == MAX_FAILURE_COUNT) {
// Continuous failure likely indicates some configuration problem
setLoggerStatus(Status.DISABLED);
ControllerLogger.MGMT_OP_LOGGER.disablingLoggingDueToFailures(failureCount);
}
}
public AuditLogHandlerUpdater getUpdater() {
return this;
}
@Override
public void addHandler(AuditLogHandler handler) {
config.lock();
try {
if (handlerUpdateTask == null){
handlerUpdateTask = new HandlerUpdateTask();
}
handlerUpdateTask.addHandler(handler);
runDisabledFastPath.set(false);
} finally {
config.unlock();
}
}
@Override
public void updateHandler(AuditLogHandler handler) {
config.lock();
try {
AuditLogHandler existing = config.getConfiguredHandler(handler.getName());
if (handler.isDifferent(existing)){
if (handlerUpdateTask == null){
handlerUpdateTask = new HandlerUpdateTask();
}
handlerUpdateTask.replaceHandler(handler);
runDisabledFastPath.set(false);
}
} finally {
config.unlock();
}
}
@Override
public void removeHandler(String name) {
config.lock();
try {
if (handlerUpdateTask == null){
handlerUpdateTask = new HandlerUpdateTask();
}
handlerUpdateTask.removeHandler(name);
runDisabledFastPath.set(false);
} finally {
config.unlock();
}
}
@Override
public void addHandlerReference(PathAddress referenceAddress) {
config.lock();
try {
if (handlerUpdateTask == null){
handlerUpdateTask = new HandlerUpdateTask();
}
handlerUpdateTask.addHandlerReference(referenceAddress);
runDisabledFastPath.set(false);
} finally {
config.unlock();
}
}
@Override
public void removeHandlerReference(PathAddress referenceAddress) {
config.lock();
try {
if (handlerUpdateTask == null){
handlerUpdateTask = new HandlerUpdateTask();
}
handlerUpdateTask.removeHandlerReference(referenceAddress);
runDisabledFastPath.set(false);
} finally {
config.unlock();
}
}
@Override
public void rollbackChanges() {
config.lock();
try {
if (handlerUpdateTask != null){
handlerUpdateTask.rollbackChanges();
handlerUpdateTask = null;
}
} finally {
config.unlock();
}
}
@Override
public void applyChanges() {
config.lock();
try {
if (!config.isManualCommit()) {
//i18n not needed, normal users will never end up here
throw new IllegalStateException("Attempt was made to manually apply changes when manual commit was not configured");
}
applyHandlerUpdates();
} finally {
config.unlock();
}
}
/** Call with lock taken */
private void applyHandlerUpdates() {
if (handlerUpdateTask != null) {
handlerUpdateTask.applyChanges();
handlerUpdateTask = null;
}
}
@Override
public void removeFormatter(String name) {
config.lock();
try {
config.removeFormatter(name);
} finally {
config.unlock();
}
}
@Override
public void addFormatter(AuditLogItemFormatter formatter) {
config.lock();
try {
config.addFormatter(formatter);
} finally {
config.unlock();
}
}
@Override
public void updateHandlerFormatter(String name, String formatterName) {
config.lock();
try {
AuditLogHandler handler = config.getConfiguredHandler(name);
handler.setFormatterName(formatterName);
handler.setFormatter(config.getFormatter(formatterName));
} finally {
config.unlock();
}
}
// Immediate updates - TODO find some better way to do these if we end up adding more handler types!
@Override
public void updateHandlerMaxFailureCount(String name, int count) {
config.lock();
try {
AuditLogHandler handler = config.getConfiguredHandler(name);
handler.setMaxFailureCount(count);
} finally {
config.unlock();
}
}
@Override
public int getHandlerFailureCount(String name) {
config.lock();
try {
AuditLogHandler handler = config.getConfiguredHandler(name);
return handler.getFailureCount();
} finally {
config.unlock();
}
}
@Override
public void updateSyslogHandlerFacility(String name, Facility facility) {
config.lock();
try {
SyslogAuditLogHandler handler = (SyslogAuditLogHandler)config.getConfiguredHandler(name);
handler.setFacility(facility);
} finally {
config.unlock();
}
}
@Override
public void updateSyslogHandlerAppName(String name, String appName) {
config.lock();
try {
SyslogAuditLogHandler handler = (SyslogAuditLogHandler)config.getConfiguredHandler(name);
handler.setAppName(appName);
} finally {
config.unlock();
}
}
@Override
public void updateSyslogHandlerReconnectTimeout(String name, int reconnectTimeout) {
config.lock();
try {
SyslogAuditLogHandler handler = (SyslogAuditLogHandler)config.getConfiguredHandler(name);
handler.setReconnectTimeout(reconnectTimeout);
} finally {
config.unlock();
}
}
// Immediate updates
@Override
public boolean getHandlerDisabledDueToFailure(String name) {
config.lock();
try {
AuditLogHandler handler = config.getConfiguredHandler(name);
return handler.isDisabledDueToFailures();
} finally {
config.unlock();
}
}
@Override
public JsonAuditLogItemFormatter getJsonFormatter(String name) {
config.lock();
try {
return (JsonAuditLogItemFormatter)config.getFormatter(name);
} finally {
config.unlock();
}
}
@Override
public List<ModelNode> listLastEntries(String name) {
config.lock();
try {
return config.getConfiguredHandler(name).listLastEntries();
} finally {
config.unlock();
}
}
@Override
public void updateInMemoryHandlerMaxHistory(String name, int maxHistory) {
config.lock();
try {
InMemoryAuditLogHander handler = (InMemoryAuditLogHander)config.getConfiguredHandler(name);
handler.setMaxHistory(maxHistory);
} finally {
config.unlock();
}
}
/**
* Abstract base class for core and new configuration
*
*/
abstract static class ManagedAuditLogConfiguration {
final boolean manualCommit;
final boolean core;
final SharedConfiguration sharedConfiguration;
private volatile Status status = Status.QUEUEING;
private volatile boolean logBoot = true;
private volatile boolean logReadOnly;
/** Guarded by auditLock - the configured handlers we should use */
protected final Set<String> handlerReferences = Collections.synchronizedSet(new HashSet<String>());
ManagedAuditLogConfiguration(SharedConfiguration sharedConfiguration, boolean core, boolean manualCommit) {
this.sharedConfiguration = sharedConfiguration;
this.core = core;
this.manualCommit = manualCommit;
}
boolean isManualCommit() {
return manualCommit;
}
boolean hasHandlerReference(String name) {
return handlerReferences.contains(name);
}
void addHandlerReference(String name) {
handlerReferences.add(name);
}
void removeHandlerReference(String name) {
handlerReferences.remove(name);
}
List<AuditLogHandler> getHandlersForLogging(){
List<AuditLogHandler> list = new ArrayList<>();
for (Map.Entry<String, AuditLogHandler> handlerEntry : sharedConfiguration.getConfiguredHandlers().entrySet()) {
if (handlerEntry.getValue().isActive() && hasHandlerReference(handlerEntry.getKey())) {
list.add(handlerEntry.getValue());
}
}
return list;
}
void lock() {
sharedConfiguration.lock();
}
void unlock() {
sharedConfiguration.unlock();
}
String getAsVersion() {
return sharedConfiguration.getAsVersion();
}
boolean isServer() {
return sharedConfiguration.isServer();
}
/** Call with lock taken */
AuditLogItemFormatter getFormatter(String name) {
return sharedConfiguration.getFormatter(name);
}
/** Call with lock taken */
AuditLogHandler getConfiguredHandler(String name) {
return sharedConfiguration.getConfiguredHandler(name);
}
/** Call with lock taken */
void setBooting(boolean booting) {
sharedConfiguration.setBooting(booting);
}
/** Call with lock taken */
boolean isBooting() {
return sharedConfiguration.isBooting();
}
/** Call with lock taken */
boolean isLogReadOnly() {
return logReadOnly;
}
/** Call with lock taken */
void setLogReadOnly(boolean logReadOnly) {
this.logReadOnly = logReadOnly;
}
/** Call with lock taken */
boolean isLogBoot() {
return logBoot;
}
/** Call with lock taken */
void setLogBoot(boolean logBoot) {
this.logBoot = logBoot;
}
/** Call with lock taken */
Status getLoggerStatus() {
return status;
}
/** Call with lock taken */
void setLoggerStatus(final Status newStatus) {
this.status = newStatus;
}
boolean isCore() {
return core;
}
abstract void putConfiguredHandler(AuditLogHandler handler);
abstract AuditLogHandler removeConfiguredHandler(String name);
abstract void addFormatter(AuditLogItemFormatter formatter);
abstract void removeFormatter(String name);
abstract void recycleHandler(String name);
}
/**
* The configuration for the core audit logger
*/
private static class CoreAuditLogConfiguration extends ManagedAuditLogConfiguration {
CoreAuditLogConfiguration(String asVersion, boolean server) {
super(new SharedConfiguration(asVersion, server), true, false);
}
@Override
void putConfiguredHandler(AuditLogHandler handler) {
sharedConfiguration.putConfiguredHandler(handler);
}
@Override
AuditLogHandler removeConfiguredHandler(String name) {
return sharedConfiguration.removeConfiguredHandler(name);
}
void addFormatter(AuditLogItemFormatter formatter) {
sharedConfiguration.addFormatter(formatter);
}
void removeFormatter(String name) {
sharedConfiguration.removeFormatter(name);
}
void recycleHandler(String name) {
sharedConfiguration.recycleHandler(name);
}
}
/**
* The configuration for other audit loggers, e.g. jmx
*/
private static class NewAuditLogConfiguration extends ManagedAuditLogConfiguration {
NewAuditLogConfiguration(CoreAuditLogConfiguration core, boolean manualCommit) {
super(core.sharedConfiguration, false, manualCommit);
}
@Override
void putConfiguredHandler(AuditLogHandler handler) {
//i18n not needed, this will not be called by user code
throw new IllegalStateException("Only available in core configuration");
}
@Override
AuditLogHandler removeConfiguredHandler(String name) {
//i18n not needed, this will not be called by user code
throw new IllegalStateException("Only available in core configuration");
}
@Override
void addFormatter(AuditLogItemFormatter formatter) {
//i18n not needed, this will not be called by user code
throw new IllegalStateException("Only available in core configuration");
}
@Override
void removeFormatter(String name) {
//i18n not needed, this will not be called by user code
throw new IllegalStateException("Only available in core configuration");
}
@Override
void recycleHandler(String name) {
//i18n not needed, this will not be called by user code
throw new IllegalStateException("Only available in core configuration");
}
}
/**
* Configuration shared among all the configurations and all access to methods will take place with the lock taken.
*/
private static class SharedConfiguration {
/** Should be fair to maintain order. Shared among all configurations */
private final Lock auditLock = new ReentrantLock(true);
private final String asVersion;
private final boolean server;
/** Guarded by auditLock - the formatters configured in the global json-formatters section */
private final Map<String, AuditLogItemFormatter> formatters = new HashMap<String, AuditLogItemFormatter>();
/** Guarded by auditLock - the handlers configured in the global file-handlers and syslog-handlers section */
private final Map<String, AuditLogHandler> configuredHandlers = new HashMap<String, AuditLogHandler>();
/** Guarded by auditLock - whether we are boothing or not */
private boolean booting = true;
SharedConfiguration(String asVersion, boolean server) {
this.asVersion = asVersion;
this.server = server;
}
public void recycleHandler(String name) {
AuditLogHandler handler = configuredHandlers.get(name);
handler.recycle();
}
void lock() {
auditLock.lock();
}
void unlock() {
auditLock.unlock();
}
String getAsVersion() {
return asVersion;
}
boolean isServer() {
return server;
}
Map<String, AuditLogHandler> getConfiguredHandlers() {
return configuredHandlers;
}
AuditLogItemFormatter getFormatter(String name) {
return formatters.get(name);
}
void addFormatter(AuditLogItemFormatter formatter) {
formatters.put(formatter.getName(), formatter);
}
void removeFormatter(String name) {
formatters.remove(name);
}
AuditLogHandler getConfiguredHandler(String name) {
return configuredHandlers.get(name);
}
void putConfiguredHandler(AuditLogHandler handler) {
configuredHandlers.put(handler.getName(), handler);
}
AuditLogHandler removeConfiguredHandler(String name) {
return configuredHandlers.remove(name);
}
void setBooting(boolean booting) {
this.booting = booting;
}
boolean isBooting() {
return booting;
}
}
/**
* When we add a handler(reference) we want that to be part of the current write.
* If we remove/change and handler, and or reference, we don't want that to take effect until the next write.
*/
private class HandlerUpdateTask {
private Map<String, AuditLogHandler> addedHandlers;
private Map<String, AuditLogHandler> replacedHandlers;
private Set<String> removedHandlers;
private Set<PathAddress> addedReferences;
private Set<PathAddress> removedReferences;
private Map<String, String> updatedFormatters;
void addHandler(AuditLogHandler handler){
assert config.isCore() : "Not available for non-core configuration";
if (removedHandlers != null && removedHandlers.contains(handler.getName())) {
throw ControllerLogger.ROOT_LOGGER.attemptToBothRemoveAndAddHandlerUpdateInstead();
}
if (addedHandlers == null){
addedHandlers = new HashMap<String, AuditLogHandler>();
}
addedHandlers.put(handler.getName(), handler);
//Update the 'live' handlers with the addition
config.putConfiguredHandler(handler);
handler.setFormatter(config.getFormatter(handler.getFormatterName()));
}
void replaceHandler(AuditLogHandler handler){
assert config.isCore() : "Not available for non-core configuration";
if (addedHandlers != null && addedHandlers.containsKey(handler.getName())){
//We may have added say a syslog-handler, and end up here as part of adding its protocol in a separate step
addHandler(handler);
return;
}
if (replacedHandlers == null){
replacedHandlers = new HashMap<String, AuditLogHandler>();
}
replacedHandlers.put(handler.getName(), handler);
}
void removeHandler(String name) {
assert config.isCore() : "Not available for non-core configuration";
if (addedHandlers != null && addedHandlers.containsKey(name)){
throw ControllerLogger.ROOT_LOGGER.attemptToBothAddAndRemoveAndHandlerFromCompositeOperation();
}
if (replacedHandlers != null && replacedHandlers.containsKey(name)){
throw ControllerLogger.ROOT_LOGGER.attemptToBothUpdateAndRemoveHandlerFromCompositeOperation();
}
final AuditLogHandler handler = config.getConfiguredHandler(name);
if (handler != null){
Set<PathAddress> references = handler.getReferences();
if (references.size() > 0){
if (!references.containsAll(removedReferences)){
Set<PathAddress> activeReferences = new HashSet<PathAddress>(references);
activeReferences.removeAll(removedReferences);
throw ControllerLogger.ROOT_LOGGER.handlerIsReferencedBy(removedReferences);
}
}
}
config.removeConfiguredHandler(name);
if (removedHandlers == null){
removedHandlers = new HashSet<String>();
}
removedHandlers.add(name);
}
void addHandlerReference(PathAddress referenceAddress){
if (removedReferences != null && removedReferences.contains(referenceAddress)){
throw ControllerLogger.ROOT_LOGGER.attemptToBothRemoveAndAddHandlerReferenceFromCompositeOperation();
}
if (addedReferences == null){
addedReferences = new HashSet<PathAddress>();
}
addedReferences.add(referenceAddress);
//Update the 'live' handlers and references with the addition
config.addHandlerReference(org.jboss.as.controller.operations.common.Util.getNameFromAddress(referenceAddress));
final String name = org.jboss.as.controller.operations.common.Util.getNameFromAddress(referenceAddress);
final AuditLogHandler handler = config.getConfiguredHandler(name);
if (handler == null){
throw ControllerLogger.ROOT_LOGGER.noHandlerCalled(name);
}
handler.addReference(referenceAddress);
}
void removeHandlerReference(PathAddress address){
if (addedReferences != null && addedReferences.contains(address)){
throw ControllerLogger.ROOT_LOGGER.attemptToBothRemoveAndAddHandlerReferenceFromCompositeOperation();
}
if (removedReferences == null){
removedReferences = new HashSet<PathAddress>();
}
removedReferences.add(address);
}
void rollbackChanges(){
if (addedReferences != null && addedReferences.size() > 0){
for (PathAddress address : addedReferences) {
final String name = org.jboss.as.controller.operations.common.Util.getNameFromAddress(address);
config.removeHandlerReference(name);
AuditLogHandler handler = config.getConfiguredHandler(name);
if (handler != null){
handler.removeReference(address);
}
}
}
if (addedHandlers != null && addedHandlers.size() > 0){
for (AuditLogHandler handler : addedHandlers.values()){
config.removeConfiguredHandler(handler.getName());
}
}
}
void applyChanges() {
if (removedHandlers != null && removedHandlers.size() > 0){
for (String name : removedHandlers) {
AuditLogHandler handler = config.removeConfiguredHandler(name);
if (handler != null){
handler.stop();
}
}
}
if (replacedHandlers != null && replacedHandlers.size() > 0){
for (AuditLogHandler handler : replacedHandlers.values()) {
AuditLogHandler existing = config.removeConfiguredHandler(handler.getName());
if (existing != null){
existing.stop();
}
//Update the references for the replaced one
for (PathAddress referenceAddress : existing.getReferences()){
if (removedReferences != null && !removedReferences.contains(referenceAddress)){
handler.addReference(referenceAddress);
}
}
config.putConfiguredHandler(handler);
handler.setFormatter(config.getFormatter(handler.getFormatterName()));
}
}
if (removedReferences != null && removedReferences.size() > 0){
for (PathAddress referenceAddress : removedReferences){
final String name = org.jboss.as.controller.operations.common.Util.getNameFromAddress(referenceAddress);
final AuditLogHandler handler = config.getConfiguredHandler(name);
if (handler != null){
handler.removeReference(referenceAddress);
}
config.removeHandlerReference(name);
}
}
}
}
@Override
public void bootDone() {
config.lock();
try {
config.setBooting(false);
if (childImpls != null) {
for (ManagedAuditLogger child : childImpls) {
child.bootDone();
}
}
if (config.getLoggerStatus() == Status.QUEUEING) {
//There was no configuration to either enable or disable the logger during boot, so disable it
setLoggerStatus(AuditLogger.Status.DISABLED);
}
} finally {
config.unlock();
}
}
@Override
public void startBoot() {
config.lock();
try {
config.setBooting(true);
if (childImpls != null) {