Drop this one too
[mono.git] / mcs / class / System.ServiceModel.Routing / System.ServiceModel.Routing.Configuration / RoutingSection.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using System.Configuration;
5 using System.Linq;
6 using System.ServiceModel;
7 using System.ServiceModel.Channels;
8 using System.ServiceModel.Configuration;
9 using System.ServiceModel.Description;
10 using System.ServiceModel.Dispatcher;
11
12 namespace System.ServiceModel.Routing.Configuration
13 {
14         static class RoutingConfigurationExtension
15         {
16                 public static ServiceEndpoint CreateEndpoint (this ChannelEndpointElement el)
17                 {
18                         // FIXME: implement
19                         throw new NotImplementedException ();
20                 }
21
22                 public static MessageFilter CreateFilter (this FilterElement el, RoutingSection sec)
23                 {
24                         switch (el.FilterType) {
25                         case FilterType.Action:
26                                 return new ActionMessageFilter (el.FilterData);
27                         case FilterType.EndpointAddress:
28                                 return new EndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
29                         case FilterType.PrefixEndpointAddress:
30                                 return new PrefixEndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
31                         case FilterType.And:
32                                 var fe1 = (FilterElement) sec.Filters [el.Filter1];
33                                 var fe2 = (FilterElement) sec.Filters [el.Filter2];
34                                 return new StrictAndMessageFilter (fe1.CreateFilter (sec), fe2.CreateFilter (sec));
35                         case FilterType.Custom:
36                                 return (MessageFilter) Activator.CreateInstance (Type.GetType (el.CustomType));
37                         case FilterType.EndpointName:
38                                 return new EndpointNameMessageFilter (el.FilterData);
39                         case FilterType.MatchAll:
40                                 return new MatchAllMessageFilter ();
41                         case FilterType.XPath:
42                                 return new XPathMessageFilter (el.FilterData);
43                         default:
44                                 throw new ArgumentOutOfRangeException ("FilterType");
45                         }
46                 }
47         }
48
49         public class RoutingSection : ConfigurationSection
50         {
51                 static ServiceModelSectionGroup wcfRootSection = ConfigurationManager.GetSection ("system.serviceModel") as ServiceModelSectionGroup;
52
53                 static ServiceEndpoint CreateServiceEndpoint (string name)
54                 {
55                         // FIXME: might be service endpoints.
56                         var endpoints = wcfRootSection.Client.Endpoints;
57                         return ((ChannelEndpointElement) endpoints [name]).CreateEndpoint ();
58                 }
59
60                 [MonoTODO]
61                 public static MessageFilterTable<IEnumerable<ServiceEndpoint>> CreateFilterTable (string name)
62                 {
63                         var sec = (RoutingSection) ConfigurationManager.GetSection ("system.serviceModel/routing");
64
65                         var filters = new Dictionary<string,MessageFilter> ();
66                         var table = new MessageFilterTable<IEnumerable<ServiceEndpoint>> ();
67                         var ftec = (FilterTableEntryCollection) sec.FilterTables [name];
68                         foreach (FilterTableEntryElement fte in ftec) {
69                                 MessageFilter filter;
70                                 if (filters.TryGetValue (fte.FilterName, out filter)) {
71                                         var filterElement = (FilterElement) sec.Filters [fte.FilterName];
72                                         filter = filterElement.CreateFilter (sec);
73                                 }
74                                 table.Add (filter, new List<ServiceEndpoint> (), fte.Priority);
75                                 var list = (List<ServiceEndpoint>) table [filter];
76                                 list.Add (CreateServiceEndpoint (fte.EndpointName));
77                                 var bec = (BackupEndpointCollection) sec.BackupLists [fte.BackupList];
78                                 foreach (BackupEndpointElement bee in bec)
79                                         list.Add (CreateServiceEndpoint (bee.EndpointName));
80                         }
81                         return table;
82                 }
83
84                 public RoutingSection ()
85                 {
86                         BackupLists = new BackupListCollection ();
87                         Filters = new FilterElementCollection ();
88                         FilterTables = new FilterTableCollection ();
89                         NamespaceTable = new NamespaceElementCollection ();
90                 }
91
92                 [ConfigurationProperty ("backupLists", Options = ConfigurationPropertyOptions.None)]
93                 public BackupListCollection BackupLists { get; private set; }
94
95                 [ConfigurationProperty ("filters", Options = ConfigurationPropertyOptions.None)]
96                 public FilterElementCollection Filters { get; private set; }
97
98                 [ConfigurationProperty ("filterTables", Options = ConfigurationPropertyOptions.None)]
99                 public FilterTableCollection FilterTables { get; private set; }
100
101                 [ConfigurationProperty ("namespaceTable", Options = ConfigurationPropertyOptions.None)]
102                 public NamespaceElementCollection NamespaceTable { get; private set; }
103         }
104 }