~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/Core/.svn/text-base/CompactRepositorySelector.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
5
 
// Licensed under the Apache License, Version 2.0 (the "License");
6
 
// you may not use this file except in compliance with the License.
7
 
// You may obtain a copy of the License at
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
11
 
// Unless required by applicable law or agreed to in writing, software
12
 
// distributed under the License is distributed on an "AS IS" BASIS,
13
 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
// See the License for the specific language governing permissions and
15
 
// limitations under the License.
16
 
//
17
 
#endregion
18
 
 
19
 
using System;
20
 
using System.Collections;
21
 
using System.Reflection;
22
 
 
23
 
using log4net.Appender;
24
 
using log4net.Util;
25
 
using log4net.Repository;
26
 
 
27
 
 
28
 
namespace log4net.Core
29
 
{
30
 
        /// <summary>
31
 
        /// The implementation of the <see cref="IRepositorySelector"/> interface suitable
32
 
        /// for use with the compact framework
33
 
        /// </summary>
34
 
        /// <remarks>
35
 
        /// <para>
36
 
        /// This <see cref="IRepositorySelector"/> implementation is a simple
37
 
        /// mapping between repository name and <see cref="ILoggerRepository"/>
38
 
        /// object.
39
 
        /// </para>
40
 
        /// <para>
41
 
        /// The .NET Compact Framework 1.0 does not support retrieving assembly
42
 
        /// level attributes therefore unlike the <c>DefaultRepositorySelector</c>
43
 
        /// this selector does not examine the calling assembly for attributes.
44
 
        /// </para>
45
 
        /// </remarks>
46
 
        /// <author>Nicko Cadell</author>
47
 
        public class CompactRepositorySelector : IRepositorySelector
48
 
        {
49
 
                #region Member Variables
50
 
 
51
 
                private const string DefaultRepositoryName = "log4net-default-repository";
52
 
 
53
 
                private readonly Hashtable m_name2repositoryMap = new Hashtable();
54
 
                private readonly Type m_defaultRepositoryType;
55
 
 
56
 
                private event LoggerRepositoryCreationEventHandler m_loggerRepositoryCreatedEvent;
57
 
 
58
 
                #endregion
59
 
 
60
 
                #region Constructors
61
 
 
62
 
                /// <summary>
63
 
                /// Create a new repository selector
64
 
                /// </summary>
65
 
                /// <param name="defaultRepositoryType">the type of the repositories to create, must implement <see cref="ILoggerRepository"/></param>
66
 
                /// <remarks>
67
 
                /// <para>
68
 
                /// Create an new compact repository selector.
69
 
                /// The default type for repositories must be specified,
70
 
                /// an appropriate value would be <see cref="log4net.Repository.Hierarchy.Hierarchy"/>.
71
 
                /// </para>
72
 
                /// </remarks>
73
 
                /// <exception cref="ArgumentNullException">throw if <paramref name="defaultRepositoryType"/> is null</exception>
74
 
                /// <exception cref="ArgumentOutOfRangeException">throw if <paramref name="defaultRepositoryType"/> does not implement <see cref="ILoggerRepository"/></exception>
75
 
                public CompactRepositorySelector(Type defaultRepositoryType)
76
 
                {
77
 
                        if (defaultRepositoryType == null)
78
 
                        {
79
 
                                throw new ArgumentNullException("defaultRepositoryType");
80
 
                        }
81
 
 
82
 
                        // Check that the type is a repository
83
 
                        if (! (typeof(ILoggerRepository).IsAssignableFrom(defaultRepositoryType)) )
84
 
                        {
85
 
                                throw log4net.Util.SystemInfo.CreateArgumentOutOfRangeException("defaultRepositoryType", (object)defaultRepositoryType, "Parameter: defaultRepositoryType, Value: ["+defaultRepositoryType+"] out of range. Argument must implement the ILoggerRepository interface");
86
 
                        }
87
 
 
88
 
                        m_defaultRepositoryType = defaultRepositoryType;
89
 
 
90
 
                        LogLog.Debug("CompactRepositorySelector: defaultRepositoryType ["+m_defaultRepositoryType+"]");
91
 
                }
92
 
 
93
 
                #endregion
94
 
 
95
 
                #region Implementation of IRepositorySelector
96
 
 
97
 
                /// <summary>
98
 
                /// Get the <see cref="ILoggerRepository"/> for the specified assembly
99
 
                /// </summary>
100
 
                /// <param name="assembly">not used</param>
101
 
                /// <returns>The default <see cref="ILoggerRepository"/></returns>
102
 
                /// <remarks>
103
 
                /// <para>
104
 
                /// The <paramref name="assembly"/> argument is not used. This selector does not create a
105
 
                /// separate repository for each assembly. 
106
 
                /// </para>
107
 
                /// <para>
108
 
                /// As a named repository is not specified the default repository is 
109
 
                /// returned. The default repository is named <c>log4net-default-repository</c>.
110
 
                /// </para>
111
 
                /// </remarks>
112
 
                public ILoggerRepository GetRepository(Assembly assembly)
113
 
                {
114
 
                        return CreateRepository(assembly, m_defaultRepositoryType);
115
 
                }
116
 
 
117
 
                /// <summary>
118
 
                /// Get the named <see cref="ILoggerRepository"/>
119
 
                /// </summary>
120
 
                /// <param name="repositoryName">the name of the repository to lookup</param>
121
 
                /// <returns>The named <see cref="ILoggerRepository"/></returns>
122
 
                /// <remarks>
123
 
                /// <para>
124
 
                /// Get the named <see cref="ILoggerRepository"/>. The default 
125
 
                /// repository is <c>log4net-default-repository</c>. Other repositories 
126
 
                /// must be created using the <see cref="CreateRepository(string, Type)"/>.
127
 
                /// If the named repository does not exist an exception is thrown.
128
 
                /// </para>
129
 
                /// </remarks>
130
 
                /// <exception cref="ArgumentNullException">throw if <paramref name="repositoryName"/> is null</exception>
131
 
                /// <exception cref="LogException">throw if the <paramref name="repositoryName"/> does not exist</exception>
132
 
                public ILoggerRepository GetRepository(string repositoryName)
133
 
                {
134
 
                        if (repositoryName == null)
135
 
                        {
136
 
                                throw new ArgumentNullException("repositoryName");
137
 
                        }
138
 
 
139
 
                        lock(this)
140
 
                        {
141
 
                                // Lookup in map
142
 
                                ILoggerRepository rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
143
 
                                if (rep == null)
144
 
                                {
145
 
                                        throw new LogException("Repository ["+repositoryName+"] is NOT defined.");
146
 
                                }
147
 
                                return rep;
148
 
                        }
149
 
                }
150
 
 
151
 
                /// <summary>
152
 
                /// Create a new repository for the assembly specified 
153
 
                /// </summary>
154
 
                /// <param name="assembly">not used</param>
155
 
                /// <param name="repositoryType">the type of repository to create, must implement <see cref="ILoggerRepository"/></param>
156
 
                /// <returns>the repository created</returns>
157
 
                /// <remarks>
158
 
                /// <para>
159
 
                /// The <paramref name="assembly"/> argument is not used. This selector does not create a
160
 
                /// separate repository for each assembly. 
161
 
                /// </para>
162
 
                /// <para>
163
 
                /// If the <paramref name="repositoryType"/> is <c>null</c> then the
164
 
                /// default repository type specified to the constructor is used.
165
 
                /// </para>
166
 
                /// <para>
167
 
                /// As a named repository is not specified the default repository is 
168
 
                /// returned. The default repository is named <c>log4net-default-repository</c>.
169
 
                /// </para>
170
 
                /// </remarks>
171
 
                public ILoggerRepository CreateRepository(Assembly assembly, Type repositoryType)
172
 
                {
173
 
                        // If the type is not set then use the default type
174
 
                        if (repositoryType == null)
175
 
                        {
176
 
                                repositoryType = m_defaultRepositoryType;
177
 
                        }
178
 
 
179
 
                        lock(this)
180
 
                        {
181
 
                                // This method should not throw if the default repository already exists.
182
 
 
183
 
                                // First check that the repository does not exist
184
 
                                ILoggerRepository rep = m_name2repositoryMap[DefaultRepositoryName] as ILoggerRepository;
185
 
                                if (rep == null)
186
 
                                {
187
 
                                        // Must create the repository
188
 
                                        rep = CreateRepository(DefaultRepositoryName, repositoryType);
189
 
                                }
190
 
 
191
 
                                return rep;
192
 
                        }               
193
 
                }
194
 
 
195
 
                /// <summary>
196
 
                /// Create a new repository for the repository specified
197
 
                /// </summary>
198
 
                /// <param name="repositoryName">the repository to associate with the <see cref="ILoggerRepository"/></param>
199
 
                /// <param name="repositoryType">the type of repository to create, must implement <see cref="ILoggerRepository"/>.
200
 
                /// If this param is null then the default repository type is used.</param>
201
 
                /// <returns>the repository created</returns>
202
 
                /// <remarks>
203
 
                /// <para>
204
 
                /// The <see cref="ILoggerRepository"/> created will be associated with the repository
205
 
                /// specified such that a call to <see cref="GetRepository(string)"/> with the
206
 
                /// same repository specified will return the same repository instance.
207
 
                /// </para>
208
 
                /// <para>
209
 
                /// If the named repository already exists an exception will be thrown.
210
 
                /// </para>
211
 
                /// <para>
212
 
                /// If <paramref name="repositoryType"/> is <c>null</c> then the default 
213
 
                /// repository type specified to the constructor is used.
214
 
                /// </para>
215
 
                /// </remarks>
216
 
                /// <exception cref="ArgumentNullException">throw if <paramref name="repositoryName"/> is null</exception>
217
 
                /// <exception cref="LogException">throw if the <paramref name="repositoryName"/> already exists</exception>
218
 
                public ILoggerRepository CreateRepository(string repositoryName, Type repositoryType)
219
 
                {
220
 
                        if (repositoryName == null)
221
 
                        {
222
 
                                throw new ArgumentNullException("repositoryName");
223
 
                        }
224
 
 
225
 
                        // If the type is not set then use the default type
226
 
                        if (repositoryType == null)
227
 
                        {
228
 
                                repositoryType = m_defaultRepositoryType;
229
 
                        }
230
 
 
231
 
                        lock(this)
232
 
                        {
233
 
                                ILoggerRepository rep = null;
234
 
 
235
 
                                // First check that the repository does not exist
236
 
                                rep = m_name2repositoryMap[repositoryName] as ILoggerRepository;
237
 
                                if (rep != null)
238
 
                                {
239
 
                                        throw new LogException("Repository ["+repositoryName+"] is already defined. Repositories cannot be redefined.");
240
 
                                }
241
 
                                else
242
 
                                {
243
 
                                        LogLog.Debug("DefaultRepositorySelector: Creating repository ["+repositoryName+"] using type ["+repositoryType+"]");
244
 
 
245
 
                                        // Call the no arg constructor for the repositoryType
246
 
                                        rep = (ILoggerRepository)Activator.CreateInstance(repositoryType);
247
 
 
248
 
                                        // Set the name of the repository
249
 
                                        rep.Name = repositoryName;
250
 
 
251
 
                                        // Store in map
252
 
                                        m_name2repositoryMap[repositoryName] = rep;
253
 
 
254
 
                                        // Notify listeners that the repository has been created
255
 
                                        OnLoggerRepositoryCreatedEvent(rep);
256
 
                                }
257
 
 
258
 
                                return rep;
259
 
                        }
260
 
                }
261
 
 
262
 
                /// <summary>
263
 
                /// Test if a named repository exists
264
 
                /// </summary>
265
 
                /// <param name="repositoryName">the named repository to check</param>
266
 
                /// <returns><c>true</c> if the repository exists</returns>
267
 
                /// <remarks>
268
 
                /// <para>
269
 
                /// Test if a named repository exists. Use <see cref="CreateRepository(string, Type)"/>
270
 
                /// to create a new repository and <see cref="GetRepository(string)"/> to retrieve 
271
 
                /// a repository.
272
 
                /// </para>
273
 
                /// </remarks>
274
 
                public bool ExistsRepository(string repositoryName)
275
 
                {
276
 
                        lock(this)
277
 
                        {
278
 
                                return m_name2repositoryMap.ContainsKey(repositoryName);
279
 
                        }
280
 
                }
281
 
 
282
 
                /// <summary>
283
 
                /// Gets a list of <see cref="ILoggerRepository"/> objects
284
 
                /// </summary>
285
 
                /// <returns>an array of all known <see cref="ILoggerRepository"/> objects</returns>
286
 
                /// <remarks>
287
 
                /// <para>
288
 
                /// Gets an array of all of the repositories created by this selector.
289
 
                /// </para>
290
 
                /// </remarks>
291
 
                public ILoggerRepository[] GetAllRepositories()
292
 
                {
293
 
                        lock(this)
294
 
                        {
295
 
                                ICollection reps = m_name2repositoryMap.Values;
296
 
                                ILoggerRepository[] all = new ILoggerRepository[reps.Count];
297
 
                                reps.CopyTo(all, 0);
298
 
                                return all;
299
 
                        }
300
 
                }
301
 
 
302
 
                #endregion
303
 
 
304
 
                /// <summary>
305
 
                /// Event to notify that a logger repository has been created.
306
 
                /// </summary>
307
 
                /// <value>
308
 
                /// Event to notify that a logger repository has been created.
309
 
                /// </value>
310
 
                /// <remarks>
311
 
                /// <para>
312
 
                /// Event raised when a new repository is created.
313
 
                /// The event source will be this selector. The event args will
314
 
                /// be a <see cref="LoggerRepositoryCreationEventArgs"/> which
315
 
                /// holds the newly created <see cref="ILoggerRepository"/>.
316
 
                /// </para>
317
 
                /// </remarks>
318
 
                public event LoggerRepositoryCreationEventHandler LoggerRepositoryCreatedEvent
319
 
                {
320
 
                        add { m_loggerRepositoryCreatedEvent += value; }
321
 
                        remove { m_loggerRepositoryCreatedEvent -= value; }
322
 
                }
323
 
 
324
 
                /// <summary>
325
 
                /// Notify the registered listeners that the repository has been created
326
 
                /// </summary>
327
 
                /// <param name="repository">The repository that has been created</param>
328
 
                /// <remarks>
329
 
                /// <para>
330
 
                /// Raises the <event cref="LoggerRepositoryCreatedEvent">LoggerRepositoryCreatedEvent</event>
331
 
                /// event.
332
 
                /// </para>
333
 
                /// </remarks>
334
 
                protected virtual void OnLoggerRepositoryCreatedEvent(ILoggerRepository repository)
335
 
                {
336
 
                        LoggerRepositoryCreationEventHandler handler = m_loggerRepositoryCreatedEvent;
337
 
                        if (handler != null)
338
 
                        {
339
 
                                handler(this, new LoggerRepositoryCreationEventArgs(repository));
340
 
                        }
341
 
                }
342
 
        }
343
 
}