1 /**
2 * Logback: the reliable, generic, fast and flexible logging framework.
3 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4 *
5 * This program and the accompanying materials are dual-licensed under
6 * either the terms of the Eclipse Public License v1.0 as published by
7 * the Eclipse Foundation
8 *
9 * or (per the licensee's choosing)
10 *
11 * under the terms of the GNU Lesser General Public License version 2.1
12 * as published by the Free Software Foundation.
13 */
14 package ch.qos.logback.core;
15
16 import java.util.Map;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.ThreadPoolExecutor;
21 import java.util.concurrent.locks.ReentrantLock;
22
23 import ch.qos.logback.core.spi.ConfigurationEvent;
24 import ch.qos.logback.core.spi.ConfigurationEventListener;
25 import ch.qos.logback.core.spi.LifeCycle;
26 import ch.qos.logback.core.spi.PropertyContainer;
27 import ch.qos.logback.core.spi.SequenceNumberGenerator;
28 import ch.qos.logback.core.status.StatusManager;
29
30 /**
31 * A context is the main anchorage point of all logback components.
32 *
33 * @author Ceki Gulcu
34 *
35 */
36 public interface Context extends PropertyContainer {
37
38 /**
39 * Return the StatusManager instance in use.
40 *
41 * @return the {@link StatusManager} instance in use.
42 */
43 StatusManager getStatusManager();
44
45 /**
46 * A Context can act as a store for various objects used by LOGBack components.
47 *
48 * @return The object stored under 'key'.
49 */
50 Object getObject(String key);
51
52 /**
53 * Store an object under 'key'. If no object can be found, null is returned.
54 *
55 * @param key
56 * @param value
57 */
58 void putObject(String key, Object value);
59
60 /**
61 * Get all the properties for this context as a Map. Note that the returned cop
62 * might be a copy not the original. Thus, modifying the returned Map will have
63 * no effect (on the original.)
64 *
65 * @return
66 */
67 // public Map<String, String> getPropertyMap();
68 /**
69 * Get the property of this context.
70 */
71 String getProperty(String key);
72
73 /**
74 * Set a property of this context.
75 */
76 void putProperty(String key, String value);
77
78 /**
79 * Get a copy of the property map
80 *
81 * @return
82 * @since 0.9.20
83 */
84 Map<String, String> getCopyOfPropertyMap();
85
86 /**
87 * Contexts are named objects.
88 *
89 * @return the name for this context
90 */
91 String getName();
92
93 /**
94 * The name of the context can be set only once.
95 *
96 * @param name
97 */
98 void setName(String name);
99
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 }