1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender;
18
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25
26 import org.apache.logging.log4j.Level;
27 import org.apache.logging.log4j.Logger;
28 import org.apache.logging.log4j.core.AbstractLifeCycle;
29 import org.apache.logging.log4j.core.LoggerContext;
30 import org.apache.logging.log4j.core.config.ConfigurationException;
31 import org.apache.logging.log4j.message.Message;
32 import org.apache.logging.log4j.status.StatusLogger;
33
34
35
36
37
38
39
40
41
42
43 public abstract class AbstractManager implements AutoCloseable {
44
45
46
47
48 protected static final Logger LOGGER = StatusLogger.getLogger();
49
50
51
52 private static final Map<String, AbstractManager> MAP = new HashMap<>();
53
54 private static final Lock LOCK = new ReentrantLock();
55
56
57
58
59 protected int count;
60
61 private final String name;
62
63 private final LoggerContext loggerContext;
64
65 protected AbstractManager(final LoggerContext loggerContext, final String name) {
66 this.loggerContext = loggerContext;
67 this.name = name;
68 LOGGER.debug("Starting {} {}", this.getClass().getSimpleName(), name);
69 }
70
71
72
73
74 @Override
75 public void close() {
76 stop(AbstractLifeCycle.DEFAULT_STOP_TIMEOUT, AbstractLifeCycle.DEFAULT_STOP_TIMEUNIT);
77 }
78
79 public boolean stop(final long timeout, final TimeUnit timeUnit) {
80 boolean stopped = true;
81 LOCK.lock();
82 try {
83 --count;
84 if (count <= 0) {
85 MAP.remove(name);
86 LOGGER.debug("Shutting down {} {}", this.getClass().getSimpleName(), getName());
87 stopped = releaseSub(timeout, timeUnit);
88 LOGGER.debug("Shut down {} {}, all resources released: {}", this.getClass().getSimpleName(), getName(), stopped);
89 }
90 } finally {
91 LOCK.unlock();
92 }
93 return stopped;
94 }
95
96
97
98
99
100
101
102
103
104
105
106 @SuppressWarnings("resource")
107 public static <M extends AbstractManager, T> M getManager(final String name, final ManagerFactory<M, T> factory,
108 final T data) {
109 LOCK.lock();
110 try {
111 @SuppressWarnings("unchecked")
112 M manager = (M) MAP.get(name);
113 if (manager == null) {
114 manager = Objects.requireNonNull(factory, "factory").createManager(name, data);
115 if (manager == null) {
116 throw new IllegalStateException("ManagerFactory [" + factory + "] unable to create manager for ["
117 + name + "] with data [" + data + "]");
118 }
119 MAP.put(name, manager);
120 } else {
121 manager.updateData(data);
122 }
123 manager.count++;
124 return manager;
125 } finally {
126 LOCK.unlock();
127 }
128 }
129
130 public void updateData(final Object data) {
131
132 }
133
134
135
136
137
138
139 public static boolean hasManager(final String name) {
140 LOCK.lock();
141 try {
142 return MAP.containsKey(name);
143 } finally {
144 LOCK.unlock();
145 }
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159 protected static <M extends AbstractManager> M narrow(final Class<M> narrowClass, final AbstractManager manager) {
160 if (narrowClass.isAssignableFrom(manager.getClass())) {
161 return (M) manager;
162 }
163 throw new ConfigurationException(
164 "Configuration has multiple incompatible Appenders pointing to the same resource '" +
165 manager.getName() + "'");
166 }
167
168 protected static StatusLogger logger() {
169 return StatusLogger.getLogger();
170 }
171
172
173
174
175
176
177
178
179 protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
180
181 return true;
182 }
183
184 protected int getCount() {
185 return count;
186 }
187
188
189
190
191
192
193
194
195 public LoggerContext getLoggerContext() {
196 return loggerContext;
197 }
198
199
200
201
202
203 @Deprecated
204 public void release() {
205 close();
206 }
207
208
209
210
211
212 public String getName() {
213 return name;
214 }
215
216
217
218
219
220
221
222
223 public Map<String, String> getContentFormat() {
224 return new HashMap<>();
225 }
226
227 protected void log(final Level level, final String message, final Throwable throwable) {
228 final Message m = LOGGER.getMessageFactory().newMessage("{} {} {}: {}",
229 getClass().getSimpleName(), getName(), message, throwable);
230 LOGGER.log(level, m, throwable);
231 }
232
233 protected void logDebug(final String message, final Throwable throwable) {
234 log(Level.DEBUG, message, throwable);
235 }
236
237 protected void logError(final String message, final Throwable throwable) {
238 log(Level.ERROR, message, throwable);
239 }
240
241 protected void logWarn(final String message, final Throwable throwable) {
242 log(Level.WARN, message, throwable);
243 }
244
245 }