001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core;
015
016import java.util.Map;
017import java.util.concurrent.ExecutorService;
018import java.util.concurrent.ScheduledExecutorService;
019import java.util.concurrent.ScheduledFuture;
020import java.util.concurrent.ThreadPoolExecutor;
021import java.util.concurrent.locks.ReentrantLock;
022
023import ch.qos.logback.core.spi.ConfigurationEvent;
024import ch.qos.logback.core.spi.ConfigurationEventListener;
025import ch.qos.logback.core.spi.LifeCycle;
026import ch.qos.logback.core.spi.PropertyContainer;
027import ch.qos.logback.core.spi.SequenceNumberGenerator;
028import ch.qos.logback.core.status.StatusManager;
029
030/**
031 * A context is the main anchorage point of all logback components.
032 * 
033 * @author Ceki Gulcu
034 * 
035 */
036public interface Context extends PropertyContainer {
037
038    /**
039     * Return the StatusManager instance in use.
040     * 
041     * @return the {@link StatusManager} instance in use.
042     */
043    StatusManager getStatusManager();
044
045    /**
046     * A Context can act as a store for various objects used by LOGBack components.
047     * 
048     * @return The object stored under 'key'.
049     */
050    Object getObject(String key);
051
052    /**
053     * Store an object under 'key'. If no object can be found, null is returned.
054     * 
055     * @param key
056     * @param value
057     */
058    void putObject(String key, Object value);
059
060    /**
061     * Get all the properties for this context as a Map. Note that the returned cop
062     * might be a copy not the original. Thus, modifying the returned Map will have
063     * no effect (on the original.)
064     * 
065     * @return
066     */
067    // public Map<String, String> getPropertyMap();
068    /**
069     * Get the property of this context.
070     */
071    String getProperty(String key);
072
073    /**
074     * Set a property of this context.
075     */
076    void putProperty(String key, String value);
077
078    /**
079     * Get a copy of the property map
080     * 
081     * @return
082     * @since 0.9.20
083     */
084    Map<String, String> getCopyOfPropertyMap();
085
086    /**
087     * Contexts are named objects.
088     * 
089     * @return the name for this context
090     */
091    String getName();
092
093    /**
094     * The name of the context can be set only once.
095     * 
096     * @param name
097     */
098    void setName(String name);
099
100    /**
101     * The time at which this context was created, expressed in millisecond elapsed
102     * since the epoch (1.1.1970).
103     * 
104     * @return The time as measured when this class was created.
105     */
106    long getBirthTime();
107
108    /**
109     * Object used for synchronization purposes. INTENDED FOR INTERNAL USAGE.
110     */
111    ReentrantLock getConfigurationLock();
112
113    /**
114     * Returns the ScheduledExecutorService for this context.
115     * 
116     * @return ScheduledExecutorService
117     * @since 1.1.7
118     */
119    // Apparently ScheduledThreadPoolExecutor has limitation where a task cannot be
120    // submitted from within a running task. ThreadPoolExecutor does not have this limitation.
121    // This causes tests failures in
122    // SocketReceiverTest.testDispatchEventForEnabledLevel and
123    // ServerSocketReceiverFunctionalTest.testLogEventFromClient.
124    ScheduledExecutorService getScheduledExecutorService();
125
126    /**
127     * Every context has an ExecutorService which be invoked to execute certain
128     * tasks in a separate thread.
129     *
130     * @return the executor for this context.
131     * @since 1.0.00 (undeprecated in 1.4.7)
132     *
133     */
134    ExecutorService getExecutorService();
135
136    /**
137     * Return an alternate {@link ExecutorService} used for one task per thread execution.
138     * @return ExecutorService
139     */
140    default ExecutorService getAlternateExecutorService() {
141        return getExecutorService();
142    }
143
144    /**
145     * Register a component that participates in the context's life cycle.
146     * <p>
147     * All components registered via this method will be stopped and removed from
148     * the context when the context is reset.
149     * 
150     * @param component the subject component
151     */
152    void register(LifeCycle component);
153
154    /**
155     * Add scheduledFuture parameter to the list of known futures.
156     *
157     * @param scheduledFuture
158     */
159    void addScheduledFuture(ScheduledFuture<?> scheduledFuture);
160
161    SequenceNumberGenerator getSequenceNumberGenerator();
162
163    void setSequenceNumberGenerator(SequenceNumberGenerator sequenceNumberGenerator);
164
165    /**
166     * Add a {@link ConfigurationEventListener} to this context.
167     *
168     * <p>Configuration events are supposed to be rare and listeners to such events rarer still.</p>
169     *
170     * <p>The propagation of {@link ConfigurationEvent configuration events} is intended for internal testing
171     * as well as some coordination between configurators.</p>
172     *
173     * @param listener
174     * @since 1.3.6/1.4.6
175     */
176    void addConfigurationEventListener(ConfigurationEventListener listener);
177
178    /**
179     * Remove an existing ConfigurationEventListener
180     * @param listener
181     * @since 1.5.7
182     */
183    default void removeConfigurationEventListener(ConfigurationEventListener listener) {};
184    /**
185     * Fire {@link ConfigurationEvent} by invoking {@link #addConfigurationEventListener registered listeners}.
186     *
187     * <p>Note that it is the role of configurators to invoke this method as a context does
188     * not necessarily know when it is being configured.</p>
189     *
190     * @param configurationEvent
191     * @since 1.3.6/1.4.6
192     */
193    void fireConfigurationEvent(ConfigurationEvent configurationEvent);
194
195}