Fixed.
[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                         // depends on System.ServiceModel internals (by InternalVisibleTo).
19                         // FIXME: is IRequestReplyRouter okay for every case?
20                         return new ServiceEndpoint (ContractDescription.GetContract (typeof (IRequestReplyRouter)), ConfigUtil.CreateBinding (el.Binding, el.BindingConfiguration), new EndpointAddress (el.Address));
21                 }
22
23                 public static MessageFilter CreateFilter (this FilterElement el, RoutingSection sec)
24                 {
25                         switch (el.FilterType) {
26                         case FilterType.Action:
27                                 return new ActionMessageFilter (el.FilterData);
28                         case FilterType.EndpointAddress:
29                                 return new EndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
30                         case FilterType.PrefixEndpointAddress:
31                                 return new PrefixEndpointAddressMessageFilter (new EndpointAddress (el.FilterData), false);
32                         case FilterType.And:
33                                 var fe1 = (FilterElement) sec.Filters [el.Filter1];
34                                 var fe2 = (FilterElement) sec.Filters [el.Filter2];
35                                 return new StrictAndMessageFilter (fe1.CreateFilter (sec), fe2.CreateFilter (sec));
36                         case FilterType.Custom:
37                                 return (MessageFilter) Activator.CreateInstance (Type.GetType (el.CustomType));
38                         case FilterType.EndpointName:
39                                 return new EndpointNameMessageFilter (el.FilterData);
40                         case FilterType.MatchAll:
41                                 return new MatchAllMessageFilter ();
42                         case FilterType.XPath:
43                                 return new XPathMessageFilter (el.FilterData);
44                         default:
45                                 throw new ArgumentOutOfRangeException ("FilterType");
46                         }
47                 }
48         }
49
50         public class RoutingSection : ConfigurationSection
51         {
52                 static ServiceEndpoint CreateServiceEndpoint (string name)
53                 {
54                         // FIXME: might be service endpoints.
55                         var endpoints = ConfigUtil.ClientSection.Endpoints;
56                         foreach (ChannelEndpointElement ep in endpoints)
57                                 if (ep.Name == name)
58                                         return ep.CreateEndpoint ();
59                         throw new KeyNotFoundException (String.Format ("client endpoint '{0}' not found", name));
60                 }
61
62                 [MonoTODO]
63                 public static MessageFilterTable<IEnumerable<ServiceEndpoint>> CreateFilterTable (string name)
64                 {
65                         var sec = (RoutingSection) ConfigurationManager.GetSection ("system.serviceModel/routing");
66
67                         var table = new MessageFilterTable<IEnumerable<ServiceEndpoint>> ();
68                         var ftec = (FilterTableEntryCollection) sec.FilterTables [name];
69                         foreach (FilterTableEntryElement fte in ftec) {
70                                 var filterElement = (FilterElement) sec.Filters [fte.FilterName];
71                                 MessageFilter filter = filterElement.CreateFilter (sec);
72                                 table.Add (filter, new List<ServiceEndpoint> (), fte.Priority);
73                                 var list = (List<ServiceEndpoint>) table [filter];
74                                 list.Add (CreateServiceEndpoint (fte.EndpointName));
75                                 var bec = (BackupEndpointCollection) sec.BackupLists [fte.BackupList];
76                                 if (bec != null)
77                                         foreach (BackupEndpointElement bee in bec)
78                                                 list.Add (CreateServiceEndpoint (bee.EndpointName));
79                         }
80                         return table;
81                 }
82
83                 public RoutingSection ()
84                 {
85                         //BackupLists = new BackupListCollection ();
86                         //Filters = new FilterElementCollection ();
87                         //FilterTables = new FilterTableCollection ();
88                         //NamespaceTable = new NamespaceElementCollection ();
89                 }
90
91                 [ConfigurationProperty ("backupLists", Options = ConfigurationPropertyOptions.None)]
92                 public BackupListCollection BackupLists {
93                         get { return (BackupListCollection) base ["backupLists"]; }
94                         private set { base ["backupLists"] = value; }
95                 }
96
97                 [ConfigurationProperty ("filters", Options = ConfigurationPropertyOptions.None)]
98                 public FilterElementCollection Filters {
99                         get { return (FilterElementCollection) base ["filters"]; }
100                         private set { base ["filters"] = value; }
101                 }
102
103                 [ConfigurationProperty ("filterTables", Options = ConfigurationPropertyOptions.None)]
104                 public FilterTableCollection FilterTables {
105                         get { return (FilterTableCollection) base ["filterTables"]; }
106                         private set { base ["filterTables"] = value; }
107                 }
108
109                 [ConfigurationProperty ("namespaceTable", Options = ConfigurationPropertyOptions.None)]
110                 public NamespaceElementCollection NamespaceTable {
111                         get { return (NamespaceElementCollection) base ["namespaceTable"]; }
112                         private set { base ["namespaceTable"] = value; }
113                 }
114         }
115 }