Merge pull request #2098 from evincarofautumn/fix-gchandle-assert
[mono.git] / mcs / class / System.Web / System.Web / DynamicModuleManager.cs
1 //
2 // DynamicModuleManager.cs: Manager for dynamic Http Modules.
3 //
4 // Author:
5 //   Matthias Bogad (bogad@cs.tum.edu)
6 //
7 // (C) 2015
8 //
9
10 using System;
11 using System.Collections.Generic;
12
13 namespace System.Web {
14         sealed class DynamicModuleManager {
15                 const string moduleNameFormat = "__Module__{0}_{1}";
16         
17                 readonly List<DynamicModuleInfo> entries = new List<DynamicModuleInfo> ();
18                 bool entriesAreReadOnly = false;
19                 readonly object mutex = new object ();
20                 
21                 public void Add (Type moduleType) 
22                 {
23                         if (moduleType == null)
24                                 throw new ArgumentException ("moduleType");
25                         
26                         if (!typeof (IHttpModule).IsAssignableFrom (moduleType))
27                                 throw new ArgumentException ("Given object does not implement IHttpModule.", "moduleType");
28                         
29                         lock (mutex) {
30                                 if (entriesAreReadOnly)
31                                         throw new InvalidOperationException ("A module was to be added to the dynamic module list, but the list was already initialized. The dynamic module list can only be initialized once.");
32
33                                 entries.Add (new DynamicModuleInfo (moduleType,
34                                                         string.Format (moduleNameFormat, moduleType.AssemblyQualifiedName, Guid.NewGuid ())));
35                         }
36                 }
37                 
38                 public ICollection<DynamicModuleInfo> LockAndGetModules ()
39                 {
40                         lock (mutex) {
41                                 entriesAreReadOnly = true;
42                                 return entries;
43                         }
44                 }
45         }
46
47         struct DynamicModuleInfo {
48                 public readonly string Name;
49                 public readonly Type Type;
50
51                 public DynamicModuleInfo (Type type, string name)
52                 {
53                         Name = name;
54                         Type = type;
55                 }
56         }
57 }