001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Objects;
022import java.util.concurrent.TimeUnit;
023import java.util.concurrent.locks.Lock;
024import java.util.concurrent.locks.ReentrantLock;
025
026import org.apache.logging.log4j.Level;
027import org.apache.logging.log4j.Logger;
028import org.apache.logging.log4j.core.AbstractLifeCycle;
029import org.apache.logging.log4j.core.LoggerContext;
030import org.apache.logging.log4j.core.config.Configuration;
031import org.apache.logging.log4j.core.config.ConfigurationException;
032import org.apache.logging.log4j.core.lookup.StrSubstitutor;
033import org.apache.logging.log4j.message.Message;
034import org.apache.logging.log4j.status.StatusLogger;
035
036/**
037 * Abstract base class used to register managers.
038 * <p>
039 * This class implements {@link AutoCloseable} mostly to allow unit tests to be written safely and succinctly. While
040 * managers do need to allocate resources (usually on construction) and then free these resources, a manager is longer
041 * lived than other auto-closeable objects like streams. None the less, making a manager AutoCloseable forces readers to
042 * be aware of the pattern: allocate resources on construction and call {@link #close()} at some point.
043 * </p>
044 */
045public abstract class AbstractManager implements AutoCloseable {
046
047    /**
048     * Implementations should extend this class for passing data between the getManager method and the manager factory
049     * class.
050     */
051    protected abstract static class AbstractFactoryData {
052
053        private final Configuration configuration;
054
055        /**
056         * Constructs the base factory data.
057         *
058         * @param configuration Configuration creating this instance.
059         */
060        protected AbstractFactoryData(final Configuration configuration) {
061            this.configuration = configuration;
062        }
063
064        /**
065         * Gets my configuration.
066         *
067         * @return my configuration.
068         */
069        public Configuration getConfiguration() {
070            return configuration;
071        }
072
073    }
074
075    /**
076     * Allow subclasses access to the status logger without creating another instance.
077     */
078    protected static final Logger LOGGER = StatusLogger.getLogger();
079
080    // Need to lock that map instead of using a ConcurrentMap due to stop removing the
081    // manager from the map and closing the stream, requiring the whole stop method to be locked.
082    private static final Map<String, AbstractManager> MAP = new HashMap<>();
083
084    private static final Lock LOCK = new ReentrantLock();
085
086    /**
087     * Number of Appenders using this manager.
088     */
089    protected int count;
090
091    private final String name;
092
093    private final LoggerContext loggerContext;
094
095    protected AbstractManager(final LoggerContext loggerContext, final String name) {
096        this.loggerContext = loggerContext;
097        this.name = name;
098        LOGGER.debug("Starting {} {}", this.getClass().getSimpleName(), name);
099    }
100
101    /**
102     * Called to signify that this Manager is no longer required by an Appender.
103     */
104    @Override
105    public void close() {
106        stop(AbstractLifeCycle.DEFAULT_STOP_TIMEOUT, AbstractLifeCycle.DEFAULT_STOP_TIMEUNIT);
107    }
108
109    public boolean stop(final long timeout, final TimeUnit timeUnit) {
110        boolean stopped = true;
111        LOCK.lock();
112        try {
113            --count;
114            if (count <= 0) {
115                MAP.remove(name);
116                LOGGER.debug("Shutting down {} {}", this.getClass().getSimpleName(), getName());
117                stopped = releaseSub(timeout, timeUnit);
118                LOGGER.debug("Shut down {} {}, all resources released: {}", this.getClass().getSimpleName(), getName(), stopped);
119            }
120        } finally {
121            LOCK.unlock();
122        }
123        return stopped;
124    }
125
126    /**
127     * Retrieves a Manager if it has been previously created or creates a new Manager.
128     * @param name The name of the Manager to retrieve.
129     * @param factory The Factory to use to create the Manager.
130     * @param data An Object that should be passed to the factory when creating the Manager.
131     * @param <M> The Type of the Manager to be created.
132     * @param <T> The type of the Factory data.
133     * @return A Manager with the specified name and type.
134     */
135    // @SuppressWarnings("resource"): this is a factory method, the resource is allocated and released elsewhere.
136    @SuppressWarnings("resource")
137    public static <M extends AbstractManager, T> M getManager(final String name, final ManagerFactory<M, T> factory,
138                                                              final T data) {
139        LOCK.lock();
140        try {
141            @SuppressWarnings("unchecked")
142            M manager = (M) MAP.get(name);
143            if (manager == null) {
144                manager = Objects.requireNonNull(factory, "factory").createManager(name, data);
145                if (manager == null) {
146                    throw new IllegalStateException("ManagerFactory [" + factory + "] unable to create manager for ["
147                            + name + "] with data [" + data + "]");
148                }
149                MAP.put(name, manager);
150            } else {
151                manager.updateData(data);
152            }
153            manager.count++;
154            return manager;
155        } finally {
156            LOCK.unlock();
157        }
158    }
159
160    /**
161     * Used by Log4j to update the Manager during reconfiguration. This method should be considered private.
162     * Implementations may not be thread safe. This method may be made protected in a future release.
163     * @param data The data to update.
164     */
165    public void updateData(final Object data) {
166        // This default implementation does nothing.
167    }
168
169    /**
170     * Determines if a Manager with the specified name exists.
171     * @param name The name of the Manager.
172     * @return True if the Manager exists, false otherwise.
173     */
174    public static boolean hasManager(final String name) {
175        LOCK.lock();
176        try {
177            return MAP.containsKey(name);
178        } finally {
179            LOCK.unlock();
180        }
181    }
182
183    /**
184     * Returns the specified manager, cast to the specified narrow type.
185     * @param narrowClass the type to cast to
186     * @param manager the manager object to return
187     * @param <M> the narrow type
188     * @return the specified manager, cast to the specified narrow type
189     * @throws ConfigurationException if the manager cannot be cast to the specified type, which only happens when
190     *          the configuration has multiple incompatible appenders pointing to the same resource
191     * @since 2.9
192     * @see <a href="https://issues.apache.org/jira/browse/LOG4J2-1908">LOG4J2-1908</a>
193     */
194    protected static <M extends AbstractManager> M narrow(final Class<M> narrowClass, final AbstractManager manager) {
195        if (narrowClass.isAssignableFrom(manager.getClass())) {
196            return (M) manager;
197        }
198        throw new ConfigurationException(
199                "Configuration has multiple incompatible Appenders pointing to the same resource '" +
200                        manager.getName() + "'");
201    }
202
203    protected static StatusLogger logger() {
204        return StatusLogger.getLogger();
205    }
206
207    /**
208     * For testing purposes.
209     */
210    static int getManagerCount() {
211        return MAP.size();
212    }
213
214    /**
215     * May be overridden by managers to perform processing while the manager is being released and the
216     * lock is held. A timeout is passed for implementors to use as they see fit.
217     * @param timeout timeout
218     * @param timeUnit timeout time unit
219     * @return true if all resources were closed normally, false otherwise.
220     */
221    protected boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
222        // This default implementation does nothing.
223        return true;
224    }
225
226    protected int getCount() {
227        return count;
228    }
229
230    /**
231     * Gets the logger context used to create this instance or null. The logger context is usually set when an appender
232     * creates a manager and that appender is given a Configuration. Not all appenders are given a Configuration by
233     * their factory method or builder.
234     *
235     * @return the logger context used to create this instance or null.
236     */
237    public LoggerContext getLoggerContext() {
238        return loggerContext;
239    }
240
241    /**
242     * Called to signify that this Manager is no longer required by an Appender.
243     * @deprecated In 2.7, use {@link #close()}.
244     */
245    @Deprecated
246    public void release() {
247        close();
248    }
249
250    /**
251     * Returns the name of the Manager.
252     * @return The name of the Manager.
253     */
254    public String getName() {
255        return name;
256    }
257
258    /**
259     * Provide a description of the content format supported by this Manager.  Default implementation returns an empty
260     * (unspecified) Map.
261     *
262     * @return a Map of key/value pairs describing the Manager-specific content format, or an empty Map if no content
263     * format descriptors are specified.
264     */
265    public Map<String, String> getContentFormat() {
266        return new HashMap<>();
267    }
268
269    /**
270     * Gets my configuration's StrSubstitutor or null.
271     *
272     * @return my configuration's StrSubstitutor or null.
273     */
274    protected StrSubstitutor getStrSubstitutor() {
275        if (loggerContext == null) {
276            return null;
277        }
278        final Configuration configuration = loggerContext.getConfiguration();
279        if (configuration == null) {
280            return null;
281        }
282        return configuration.getStrSubstitutor();
283    }
284
285    protected void log(final Level level, final String message, final Throwable throwable) {
286        final Message m = LOGGER.getMessageFactory().newMessage("{} {} {}: {}",
287                getClass().getSimpleName(), getName(), message, throwable);
288        LOGGER.log(level, m, throwable);
289    }
290
291    protected void logDebug(final String message, final Throwable throwable) {
292        log(Level.DEBUG, message, throwable);
293    }
294
295    protected void logError(final String message, final Throwable throwable) {
296        log(Level.ERROR, message, throwable);
297    }
298
299    protected void logWarn(final String message, final Throwable throwable) {
300        log(Level.WARN, message, throwable);
301    }
302
303}