This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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 //
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 //
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
24 // 
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
27 // 
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 //
36
37 using System;
38 using System.Collections;
39 using System.Security.Permissions;
40
41 namespace System.Security.Policy {
42
43         [Serializable]
44         [MonoTODO ("Fix serialization compatibility with MS.NET")]
45         public sealed class Evidence : ICollection, IEnumerable {
46         
47                 private bool _locked;
48                 private ArrayList hostEvidenceList;     
49                 private ArrayList assemblyEvidenceList;
50                 
51                 public Evidence () 
52                 {
53                         hostEvidenceList = ArrayList.Synchronized (new ArrayList ());
54                         assemblyEvidenceList = ArrayList.Synchronized (new ArrayList ());
55                 }
56
57                 public Evidence (Evidence evidence) : this ()
58                 {
59                         if (evidence != null)
60                                 Merge (evidence);       
61                 }
62
63                 public Evidence (object[] hostEvidence, object[] assemblyEvidence) : this ()
64                 {
65                         if (null != hostEvidence)
66                                 hostEvidenceList.AddRange (hostEvidence);
67                         if (null != assemblyEvidence)
68                                 assemblyEvidenceList.AddRange (assemblyEvidence);
69                 }
70                 
71                 //
72                 // Public Properties
73                 //
74         
75                 public int Count {
76                         get {
77                                 return (hostEvidenceList.Count + assemblyEvidenceList.Count);
78                         }
79                 }
80
81                 public bool IsReadOnly {
82                         get{ return false; }
83                 }
84                 
85                 // LAMESPEC: Always TRUE (not FALSE)
86                 public bool IsSynchronized {
87                         get { return true; }
88                 }
89
90                 public bool Locked {
91                         get { return _locked; }
92                         set { 
93                                 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
94                                 _locked = value; 
95                         }
96                 }       
97
98                 public object SyncRoot {
99                         get { return this; }
100                 }
101
102                 //
103                 // Public Methods
104                 //
105
106                 public void AddAssembly (object id) 
107                 {
108                         assemblyEvidenceList.Add (id);
109                 }
110
111                 public void AddHost (object id) 
112                 {
113                         if (_locked) {
114                                 new SecurityPermission (SecurityPermissionFlag.ControlEvidence).Demand ();
115                         }
116                         hostEvidenceList.Add (id);
117                 }
118
119                 public void CopyTo (Array array, int index) 
120                 {
121                         if (hostEvidenceList.Count > 0) 
122                                 hostEvidenceList.CopyTo (array, index);
123                         if (assemblyEvidenceList.Count > 0) 
124                                 assemblyEvidenceList.CopyTo (array, index + hostEvidenceList.Count);
125                 }
126
127                 public IEnumerator GetEnumerator () 
128                 {
129                         return new EvidenceEnumerator (hostEvidenceList.GetEnumerator (), 
130                                 assemblyEvidenceList.GetEnumerator ());
131                 }
132
133                 public IEnumerator GetAssemblyEnumerator () 
134                 {
135                         return assemblyEvidenceList.GetEnumerator ();
136                 }
137
138                 public IEnumerator GetHostEnumerator () 
139                 {
140                         return hostEvidenceList.GetEnumerator ();
141                 }
142
143                 public void Merge (Evidence evidence) 
144                 {
145                         IEnumerator hostenum, assemblyenum;
146                         
147                         hostenum = evidence.GetHostEnumerator ();
148                         while( hostenum.MoveNext () ) {
149                                 AddHost (hostenum.Current);
150                         }
151
152                         assemblyenum = evidence.GetAssemblyEnumerator ();
153                         while( assemblyenum.MoveNext () ) {
154                                 AddAssembly (assemblyenum.Current);
155                         }
156                 }
157         
158                 private class EvidenceEnumerator : IEnumerator {
159                         
160                         private IEnumerator currentEnum, hostEnum, assemblyEnum;                
161         
162                         public EvidenceEnumerator (IEnumerator hostenum, IEnumerator assemblyenum) 
163                         {
164                                 this.hostEnum = hostenum;
165                                 this.assemblyEnum = assemblyenum;
166                                 currentEnum = hostEnum;                 
167                         }
168
169                         public bool MoveNext () 
170                         {
171                                 bool ret = currentEnum.MoveNext ();
172                                 
173                                 if ( !ret && hostEnum == currentEnum ) {
174                                         currentEnum = assemblyEnum;
175                                         ret = assemblyEnum.MoveNext ();
176                                 }
177
178                                 return ret;
179                         }
180
181                         public void Reset () 
182                         {
183                                 hostEnum.Reset ();
184                                 assemblyEnum.Reset ();
185                                 currentEnum = hostEnum;
186                         }
187
188                         public object Current {
189                                 get {
190                                         return currentEnum.Current;
191                                 }
192                         }
193                 }
194         }
195 }
196