jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
 
using System;
using System.Collections;
 
using log4net.Repository;
 
namespace log4net.Core
{
    #region WrapperCreationHandler
 
    /// <summary>
    /// Delegate used to handle creation of new wrappers.
    /// </summary>
    /// <param name="logger">The logger to wrap in a wrapper.</param>
    /// <remarks>
    /// <para>
    /// Delegate used to handle creation of new wrappers. This delegate
    /// is called from the <see cref="WrapperMap.CreateNewWrapperObject"/>
    /// method to construct the wrapper for the specified logger.
    /// </para>
    /// <para>
    /// The delegate to use is supplied to the <see cref="WrapperMap"/>
    /// constructor.
    /// </para>
    /// </remarks>
    public delegate ILoggerWrapper WrapperCreationHandler(ILogger logger);
 
    #endregion WrapperCreationHandler
 
    /// <summary>
    /// Maps between logger objects and wrapper objects.
    /// </summary>
    /// <remarks>
    /// <para>
    /// This class maintains a mapping between <see cref="ILogger"/> objects and
    /// <see cref="ILoggerWrapper"/> objects. Use the <see cref="GetWrapper"/> method to 
    /// lookup the <see cref="ILoggerWrapper"/> for the specified <see cref="ILogger"/>.
    /// </para>
    /// <para>
    /// New wrapper instances are created by the <see cref="CreateNewWrapperObject"/>
    /// method. The default behavior is for this method to delegate construction
    /// of the wrapper to the <see cref="WrapperCreationHandler"/> delegate supplied
    /// to the constructor. This allows specialization of the behavior without
    /// requiring subclassing of this type.
    /// </para>
    /// </remarks>
    /// <author>Nicko Cadell</author>
    /// <author>Gert Driesen</author>
    public class WrapperMap
    {
        #region Public Instance Constructors
 
        /// <summary>
        /// Initializes a new instance of the <see cref="WrapperMap" />
        /// </summary>
        /// <param name="createWrapperHandler">The handler to use to create the wrapper objects.</param>
        /// <remarks>
        /// <para>
        /// Initializes a new instance of the <see cref="WrapperMap" /> class with 
        /// the specified handler to create the wrapper objects.
        /// </para>
        /// </remarks>
        public WrapperMap(WrapperCreationHandler createWrapperHandler) 
        {
            m_createWrapperHandler = createWrapperHandler;
 
            // Create the delegates for the event callbacks
            m_shutdownHandler = new LoggerRepositoryShutdownEventHandler(ILoggerRepository_Shutdown);
        }
 
        #endregion Public Instance Constructors
 
        #region Public Instance Properties
 
        /// <summary>
        /// Gets the wrapper object for the specified logger.
        /// </summary>
        /// <returns>The wrapper object for the specified logger</returns>
        /// <remarks>
        /// <para>
        /// If the logger is null then the corresponding wrapper is null.
        /// </para>
        /// <para>
        /// Looks up the wrapper it it has previously been requested and
        /// returns it. If the wrapper has never been requested before then
        /// the <see cref="CreateNewWrapperObject"/> virtual method is
        /// called.
        /// </para>
        /// </remarks>
        virtual public ILoggerWrapper GetWrapper(ILogger logger)
        {
            // If the logger is null then the corresponding wrapper is null
            if (logger == null)
            {
                return null;
            }
 
            lock(this)
            {
                // Lookup hierarchy in map.
                Hashtable wrappersMap = (Hashtable)m_repositories[logger.Repository];
 
                if (wrappersMap == null)
                {
                    // Hierarchy does not exist in map.
                    // Must register with hierarchy
 
                    wrappersMap = new Hashtable();
                    m_repositories[logger.Repository] = wrappersMap;
 
                    // Register for config reset & shutdown on repository
                    logger.Repository.ShutdownEvent += m_shutdownHandler;
                }
 
                // Look for the wrapper object in the map
                ILoggerWrapper wrapperObject = wrappersMap[logger] as ILoggerWrapper;
 
                if (wrapperObject == null)
                {
                    // No wrapper object exists for the specified logger
 
                    // Create a new wrapper wrapping the logger
                    wrapperObject = CreateNewWrapperObject(logger);
                    
                    // Store wrapper logger in map
                    wrappersMap[logger] = wrapperObject;
                }
 
                return wrapperObject;
            }
        }
 
        #endregion Public Instance Properties
 
        #region Protected Instance Properties
 
        /// <summary>
        /// Gets the map of logger repositories.
        /// </summary>
        /// <value>
        /// Map of logger repositories.
        /// </value>
        /// <remarks>
        /// <para>
        /// Gets the hashtable that is keyed on <see cref="ILoggerRepository"/>. The
        /// values are hashtables keyed on <see cref="ILogger"/> with the
        /// value being the corresponding <see cref="ILoggerWrapper"/>.
        /// </para>
        /// </remarks>
        protected Hashtable Repositories 
        {
            get { return this.m_repositories; }
        }
 
        #endregion Protected Instance Properties
 
        #region Protected Instance Methods
 
        /// <summary>
        /// Creates the wrapper object for the specified logger.
        /// </summary>
        /// <param name="logger">The logger to wrap in a wrapper.</param>
        /// <returns>The wrapper object for the logger.</returns>
        /// <remarks>
        /// <para>
        /// This implementation uses the <see cref="WrapperCreationHandler"/>
        /// passed to the constructor to create the wrapper. This method
        /// can be overridden in a subclass.
        /// </para>
        /// </remarks>
        virtual protected ILoggerWrapper CreateNewWrapperObject(ILogger logger)
        {
            if (m_createWrapperHandler != null)
            {
                return m_createWrapperHandler(logger);
            }
            return null;
        }
 
        /// <summary>
        /// Called when a monitored repository shutdown event is received.
        /// </summary>
        /// <param name="repository">The <see cref="ILoggerRepository"/> that is shutting down</param>
        /// <remarks>
        /// <para>
        /// This method is called when a <see cref="ILoggerRepository"/> that this
        /// <see cref="WrapperMap"/> is holding loggers for has signaled its shutdown
        /// event <see cref="ILoggerRepository.ShutdownEvent"/>. The default
        /// behavior of this method is to release the references to the loggers
        /// and their wrappers generated for this repository.
        /// </para>
        /// </remarks>
        virtual protected void RepositoryShutdown(ILoggerRepository repository)
        {
            lock(this)
            {
                // Remove the repository from map
                m_repositories.Remove(repository);
 
                // Unhook events from the repository
                repository.ShutdownEvent -= m_shutdownHandler;
            }
        }
 
        /// <summary>
        /// Event handler for repository shutdown event.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">The event args.</param>
        private void ILoggerRepository_Shutdown(object sender, EventArgs e)
        {
            ILoggerRepository repository = sender as ILoggerRepository;
            if (repository != null)
            {
                // Remove all repository from map
                RepositoryShutdown(repository);
            }
        }
 
        #endregion Protected Instance Methods
 
        #region Private Instance Variables
 
        /// <summary>
        /// Map of logger repositories to hashtables of ILogger to ILoggerWrapper mappings
        /// </summary>
        private readonly Hashtable m_repositories = new Hashtable();
 
        /// <summary>
        /// The handler to use to create the extension wrapper objects.
        /// </summary>
        private readonly WrapperCreationHandler m_createWrapperHandler;
 
        /// <summary>
        /// Internal reference to the delegate used to register for repository shutdown events.
        /// </summary>
        private readonly LoggerRepositoryShutdownEventHandler m_shutdownHandler;
 
        #endregion Private Instance Variables
    }
}