imported everything from my branch (which is slightly harmless).
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / SoapExtension.cs
1 // \r
2 // System.Web.Services.Protocols.SoapExtension.cs\r
3 //\r
4 // Author:\r
5 //   Tim Coleman (tim@timcoleman.com)\r
6 //\r
7 // Copyright (C) Tim Coleman, 2002\r
8 //\r
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 \r
31 using System.IO;\r
32 using System.Collections;\r
33 using System.Web.Services.Configuration;\r
34 \r
35 namespace System.Web.Services.Protocols {\r
36         public abstract class SoapExtension {\r
37 \r
38                 #region Fields\r
39 \r
40                 Stream stream;\r
41 \r
42                 #endregion\r
43 \r
44                 #region Constructors\r
45 \r
46                 protected SoapExtension ()\r
47                 {\r
48                 }\r
49 \r
50                 #endregion // Constructors\r
51 \r
52                 #region Methods\r
53 \r
54                 public virtual Stream ChainStream (Stream stream)\r
55                 {\r
56                         return stream;\r
57                 }\r
58 \r
59                 public abstract object GetInitializer (Type serviceType);\r
60                 public abstract object GetInitializer (LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute);\r
61                 public abstract void Initialize (object initializer);\r
62                 public abstract void ProcessMessage (SoapMessage message);\r
63 \r
64 \r
65 #if !TARGET_JVM
66                 static ArrayList[] globalExtensions;\r
67 #else
68                 static ArrayList[] globalExtensions {
69                         get {
70                                 return (ArrayList[])AppDomain.CurrentDomain.GetData("SoapExtension.globalExtensions");
71                         }
72                         set {
73                                 AppDomain.CurrentDomain.SetData("SoapExtension.globalExtensions", value);
74                         }
75                 }
76 #endif
77 \r
78                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs)\r
79                 {\r
80                         if (extensionConfigs == null) return null;\r
81                         SoapExtension[] res = new SoapExtension [extensionConfigs.Length];\r
82                         CreateExtensionChain (extensionConfigs, res, 0);\r
83                         return res;\r
84                 }\r
85 \r
86                 internal static SoapExtension[] CreateExtensionChain (SoapExtensionRuntimeConfig[] hiPrioExts, SoapExtensionRuntimeConfig[] medPrioExts, SoapExtensionRuntimeConfig[] lowPrioExts)\r
87                 {\r
88                         int len = 0;\r
89                         if (hiPrioExts != null) len += hiPrioExts.Length;\r
90                         if (medPrioExts != null) len += medPrioExts.Length;\r
91                         if (lowPrioExts != null) len += lowPrioExts.Length;\r
92                         if (len == 0) return null;\r
93 \r
94                         SoapExtension[] res = new SoapExtension [len];\r
95                         int pos = 0;\r
96                         if (hiPrioExts != null) pos = CreateExtensionChain (hiPrioExts, res, pos);\r
97                         if (medPrioExts != null) pos = CreateExtensionChain (medPrioExts, res, pos);\r
98                         if (lowPrioExts != null) pos = CreateExtensionChain (lowPrioExts, res, pos);\r
99                         return res;\r
100                 }\r
101 \r
102                 static int CreateExtensionChain (SoapExtensionRuntimeConfig[] extensionConfigs, SoapExtension[] destArray, int pos)\r
103                 {\r
104                         for (int n=0; n<extensionConfigs.Length; n++)\r
105                         {\r
106                                 SoapExtensionRuntimeConfig econf = extensionConfigs [n];\r
107                                 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);\r
108                                 ext.Initialize (econf.InitializationInfo);\r
109                                 destArray [pos++] = ext;\r
110                         }\r
111                         return pos;\r
112                 }\r
113 \r
114                 static void InitializeGlobalExtensions ()\r
115                 {\r
116                         globalExtensions = new ArrayList[2];\r
117                         \r
118                         ArrayList exts = WSConfig.Instance.ExtensionTypes;\r
119                         if (exts == null) return;\r
120 \r
121                         foreach (WSExtensionConfig econf in exts)\r
122                         {\r
123                                 if (globalExtensions [(int)econf.Group] == null) globalExtensions [(int)econf.Group] = new ArrayList ();\r
124                                 ArrayList destList = globalExtensions [(int) econf.Group];\r
125                                 bool added = false;\r
126                                 for (int n=0; n<destList.Count && !added; n++)\r
127                                         if (((WSExtensionConfig)destList [n]).Priority > econf.Priority) {\r
128                                                 destList.Insert (n, econf);\r
129                                                 added = true;\r
130                                         }\r
131                                 if (!added) destList.Add (econf);\r
132                         }\r
133                 }\r
134 \r
135                 internal static SoapExtensionRuntimeConfig[][] GetTypeExtensions (Type serviceType)\r
136                 {\r
137                         if (globalExtensions == null) InitializeGlobalExtensions();\r
138                         \r
139                         SoapExtensionRuntimeConfig[][] exts = new SoapExtensionRuntimeConfig[2][];\r
140 \r
141                         for (int group = 0; group < 2; group++)\r
142                         {\r
143                                 ArrayList globList = globalExtensions[group];\r
144                                 if (globList == null) continue;\r
145                                 exts [group] = new SoapExtensionRuntimeConfig [globList.Count];\r
146                                 for (int n=0; n<globList.Count; n++)\r
147                                 {\r
148                                         WSExtensionConfig econf = (WSExtensionConfig) globList [n];\r
149                                         SoapExtensionRuntimeConfig typeconf = new SoapExtensionRuntimeConfig ();\r
150                                         typeconf.Type = econf.Type;\r
151                                         SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);\r
152                                         typeconf.InitializationInfo = ext.GetInitializer (serviceType);\r
153                                         exts [group][n] = typeconf;\r
154                                 }\r
155                         }\r
156                         return exts;\r
157                 }\r
158 \r
159                 internal static SoapExtensionRuntimeConfig[] GetMethodExtensions (LogicalMethodInfo method)\r
160                 {\r
161                         object[] ats = method.GetCustomAttributes (typeof (SoapExtensionAttribute));\r
162                         SoapExtensionRuntimeConfig[] exts = new SoapExtensionRuntimeConfig [ats.Length];\r
163                         int[] priorities = new int[ats.Length];\r
164 \r
165                         for (int n=0; n<ats.Length; n++)\r
166                         {\r
167                                 SoapExtensionAttribute at = (SoapExtensionAttribute) ats[n];\r
168                                 SoapExtensionRuntimeConfig econf = new SoapExtensionRuntimeConfig ();\r
169                                 econf.Type = at.ExtensionType;\r
170                                 priorities [n] = at.Priority;\r
171                                 SoapExtension ext = (SoapExtension) Activator.CreateInstance (econf.Type);\r
172                                 econf.InitializationInfo = ext.GetInitializer (method, at);\r
173                                 exts [n] = econf;\r
174                         }\r
175                         Array.Sort (priorities, exts);\r
176                         return exts;\r
177                 }\r
178 \r
179                 internal static Stream ExecuteChainStream (SoapExtension[] extensions, Stream stream)\r
180                 {\r
181                         if (extensions == null) return stream;\r
182 \r
183                         Stream newStream = stream;\r
184                         foreach (SoapExtension ext in extensions)\r
185                                 newStream = ext.ChainStream (newStream);\r
186                         return newStream;\r
187                 }\r
188 \r
189                 internal static void ExecuteProcessMessage(SoapExtension[] extensions, SoapMessage message, bool inverseOrder)\r
190                 {\r
191                         if (extensions == null) return;\r
192 \r
193                         if (inverseOrder)\r
194                         {\r
195                                 for (int n = extensions.Length-1; n >= 0; n--)\r
196                                         extensions[n].ProcessMessage (message);\r
197                         }\r
198                         else\r
199                         {\r
200                                 for (int n = 0; n < extensions.Length; n++)\r
201                                         extensions[n].ProcessMessage (message);\r
202                         }\r
203                 }
204 \r
205                 #endregion // Methods\r
206         }\r
207 \r
208         internal class SoapExtensionRuntimeConfig\r
209         {\r
210                 public Type Type;\r
211                 public int Priority;\r
212                 public object InitializationInfo;\r
213         }\r
214 }