2004-04-12 David Sheldon <dave-mono@earth.li>
[mono.git] / mcs / class / corlib / System.Security.Policy / Evidence.cs
1 //
2 // System.Security.Policy.Evidence
3 //
4 // Authors:
5 //      Sean MacIsaac (macisaac@ximian.com)
6 //      Nick Drochak (ndrochak@gol.com)
7 //      Jackson Harper (Jackson@LatitudeGeo.com)
8 //      Sebastien Pouliot (spouliot@motus.com)
9 //
10 // (C) 2001 Ximian, Inc.
11 // Portions (C) 2003, 2004 Motus Technologies Inc. (http://www.motus.com)
12 //
13
14 using System;
15 using System.Collections;
16 using System.Security.Permissions;
17
18 namespace System.Security.Policy {
19
20         [Serializable]
21         public sealed class Evidence : ICollection, IEnumerable {
22         
23                 private bool _locked;
24                 private ArrayList hostEvidenceList;     
25                 private ArrayList assemblyEvidenceList;
26                 
27                 public Evidence () 
28                 {
29                         hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
30                         assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
31                 }
32
33                 public Evidence (Evidence evidence) : this ()
34                 {
35                         if (evidence != null)
36                                 Merge (evidence);       
37                 }
38
39                 public Evidence (object[] hostEvidence, object[] assemblyEvidence) : this ()
40                 {
41                         if (null != hostEvidence)
42                                 hostEvidenceList.AddRange (hostEvidence);
43                         if (null != assemblyEvidence)
44                                 assemblyEvidenceList.AddRange (assemblyEvidence);
45                 }
46                 
47                 //
48                 // Public Properties
49                 //
50         
51                 public int Count {
52                         get {
53                                 return (hostEvidenceList.Count + assemblyEvidenceList.Count);
54                         }
55                 }
56
57                 public bool IsReadOnly {
58                         get{ return false; }
59                 }
60                 
61                 // LAMESPEC: Always TRUE (not FALSE)
62                 public bool IsSynchronized {
63                         get { return true; }
64                 }
65
66                 public bool Locked {
67                         get { return _locked; }
68                         set { 
69                                 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
70                                 _locked = value; 
71                         }
72                 }       
73
74                 public object SyncRoot {
75                         get { return this; }
76                 }
77
78                 //
79                 // Public Methods
80                 //
81
82                 public void AddAssembly (object id) 
83                 {
84                         assemblyEvidenceList.Add (id);
85                 }
86
87                 public void AddHost (object id) 
88                 {
89                         if (_locked) {
90                                 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
91                         }
92                         hostEvidenceList.Add (id);
93                 }
94
95                 public void CopyTo (Array array, int index) 
96                 {
97                         if (hostEvidenceList.Count > 0) 
98                                 hostEvidenceList.CopyTo (array, index);
99                         if (assemblyEvidenceList.Count > 0) 
100                                 assemblyEvidenceList.CopyTo (array, index + hostEvidenceList.Count);
101                 }
102
103                 public IEnumerator GetEnumerator () 
104                 {
105                         return new EvidenceEnumerator (hostEvidenceList.GetEnumerator (), 
106                                 assemblyEvidenceList.GetEnumerator ());
107                 }
108
109                 public IEnumerator GetAssemblyEnumerator () 
110                 {
111                         return assemblyEvidenceList.GetEnumerator ();
112                 }
113
114                 public IEnumerator GetHostEnumerator () 
115                 {
116                         return hostEvidenceList.GetEnumerator ();
117                 }
118
119                 public void Merge (Evidence evidence) 
120                 {
121                         IEnumerator hostenum, assemblyenum;
122                         
123                         hostenum = evidence.GetHostEnumerator ();
124                         while( hostenum.MoveNext () ) {
125                                 AddHost (hostenum.Current);
126                         }
127
128                         assemblyenum = evidence.GetAssemblyEnumerator ();
129                         while( assemblyenum.MoveNext () ) {
130                                 AddAssembly (assemblyenum.Current);
131                         }
132                 }
133         
134                 private class EvidenceEnumerator : IEnumerator {
135                         
136                         private IEnumerator currentEnum, hostEnum, assemblyEnum;                
137         
138                         public EvidenceEnumerator (IEnumerator hostenum, IEnumerator assemblyenum) 
139                         {
140                                 this.hostEnum = hostenum;
141                                 this.assemblyEnum = assemblyenum;
142                                 currentEnum = hostEnum;                 
143                         }
144
145                         public bool MoveNext () 
146                         {
147                                 bool ret = currentEnum.MoveNext ();
148                                 
149                                 if ( !ret && hostEnum == currentEnum ) {
150                                         currentEnum = assemblyEnum;
151                                         ret = assemblyEnum.MoveNext ();
152                                 }
153
154                                 return ret;
155                         }
156
157                         public void Reset () 
158                         {
159                                 hostEnum.Reset ();
160                                 assemblyEnum.Reset ();
161                                 currentEnum = hostEnum;
162                         }
163
164                         public object Current {
165                                 get {
166                                         return currentEnum.Current;
167                                 }
168                         }
169                 }
170         }
171 }
172