-
Notifications
You must be signed in to change notification settings - Fork 461
/
AuditLogHandler.java
172 lines (142 loc) · 4.83 KB
/
AuditLogHandler.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, 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.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.controller.logging.ControllerLogger;
import org.jboss.dmr.ModelNode;
/**
* All methods on this class should be called with {@link ManagedAuditLoggerImpl}'s lock taken.
*
* @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
*/
abstract class AuditLogHandler {
/** Maximum number of consecutive logging failures before we stop logging */
private volatile int maxFailureCount = 10;
/** The number of consecutive failures writing to the log */
private int failureCount;
protected final String name;
private volatile String formatterName;
private final Set<PathAddress> references = new HashSet<PathAddress>();
private AuditLogItemFormatter formatter;
AuditLogHandler(String name, String formatterName, int maxFailureCount){
this.name = name;
this.formatterName = formatterName;
this.maxFailureCount = maxFailureCount;
}
String getName() {
return name;
}
void setFormatter(AuditLogItemFormatter formatter) {
this.formatter = formatter;
}
String getFormatterName() {
return formatterName;
}
public void setMaxFailureCount(int count) {
this.maxFailureCount = count;
}
public void setFormatterName(String formatterName) {
this.formatterName = formatterName;
}
void writeLogItem(AuditLogItem item) {
FailureCountHandler fch = getFailureCountHandler();
try {
initialize();
String formattedItem = item.format(formatter);
writeLogItem(formattedItem);
fch.success();
} catch (Throwable t) {
fch.failure(t);
}
}
void recycle() {
this.failureCount = 0;
stop();
}
boolean isActive() {
return !hasTooManyFailures();
}
boolean isDisabledDueToFailures() {
return hasTooManyFailures();
}
boolean hasTooManyFailures() {
return maxFailureCount > 0 && failureCount >= maxFailureCount;
}
void addReference(PathAddress address){
references.add(address);
}
void removeReference(PathAddress address){
references.remove(address);
if (references.size() == 0){
stop();
}
}
Set<PathAddress> getReferences(){
return references;
}
int getFailureCount() {
return failureCount;
}
FailureCountHandler getFailureCountHandler() {
return new StandardFailureCountHandler();
}
abstract boolean isDifferent(AuditLogHandler other);
abstract void initialize();
abstract void stop();
abstract void writeLogItem(String formattedItem) throws IOException;
List<ModelNode> listLastEntries() {
return Collections.emptyList();
}
interface FailureCountHandler {
void success();
void failure(Throwable t);
}
class StandardFailureCountHandler implements FailureCountHandler {
@Override
public void success() {
failureCount = 0;
}
@Override
public void failure(Throwable t) {
failureCount++;
ControllerLogger.MGMT_OP_LOGGER.logHandlerWriteFailed(t, name);
if (hasTooManyFailures()) {
ControllerLogger.MGMT_OP_LOGGER.disablingLogHandlerDueToFailures(failureCount, name);
}
}
}
class ReconnectFailureCountHandler implements FailureCountHandler {
@Override
public void success() {
failureCount = 0;
}
@Override
public void failure(Throwable t) {
ControllerLogger.MGMT_OP_LOGGER.reconnectToSyslogFailed(name, t);
}
}
}