1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Configuration
18 using System
.Configuration
;
22 /// Represents the MonoRail external configuration
24 public class MonoRailConfiguration
: ISerializedConfig
26 private static readonly String SectionName
= "monorail";
27 private static readonly String AlternativeSectionName
= "monoRail";
29 private bool checkClientIsConnected
, useWindsorIntegration
, matchHostNameAndPath
, excludeAppPath
;
30 private Type customFilterFactory
;
31 private XmlNode configurationSection
;
33 private SmtpConfig smtpConfig
;
34 private ViewEngineConfig viewEngineConfig
;
35 private ControllersConfig controllersConfig
;
36 private ViewComponentsConfig viewComponentsConfig
;
37 private ScaffoldConfig scaffoldConfig
;
39 private RoutingRuleCollection routingRules
;
40 private ExtensionEntryCollection extensions
;
41 private ServiceEntryCollection services
;
42 private DefaultUrlCollection defaultUrls
;
45 /// Initializes a new instance of the <see cref="MonoRailConfiguration"/> class.
47 public MonoRailConfiguration()
49 smtpConfig
= new SmtpConfig();
50 viewEngineConfig
= new ViewEngineConfig();
51 controllersConfig
= new ControllersConfig();
52 viewComponentsConfig
= new ViewComponentsConfig();
53 scaffoldConfig
= new ScaffoldConfig();
54 routingRules
= new RoutingRuleCollection();
55 extensions
= new ExtensionEntryCollection();
56 services
= new ServiceEntryCollection();
57 defaultUrls
= new DefaultUrlCollection();
59 checkClientIsConnected
= false;
60 matchHostNameAndPath
= false;
61 excludeAppPath
= false;
67 /// <param name="section"></param>
68 public MonoRailConfiguration(XmlNode section
) : this()
70 configurationSection
= section
;
76 /// <returns></returns>
77 public static MonoRailConfiguration
GetConfig()
79 MonoRailConfiguration config
=
80 ConfigurationManager
.GetSection(SectionName
) as MonoRailConfiguration
;
85 ConfigurationManager
.GetSection(AlternativeSectionName
) as MonoRailConfiguration
;
90 throw new ApplicationException("You have to provide a small configuration to use " +
91 "MonoRail. Check the samples or the documentation");
97 #region ISerializedConfig implementation
100 /// Deserializes the specified node.
102 /// <param name="node">The node.</param>
103 public void Deserialize(XmlNode node
)
105 viewEngineConfig
.Deserialize(node
);
106 smtpConfig
.Deserialize(node
);
107 controllersConfig
.Deserialize(node
);
108 viewComponentsConfig
.Deserialize(node
);
109 scaffoldConfig
.Deserialize(node
);
111 services
.Deserialize(node
);
112 extensions
.Deserialize(node
);
113 routingRules
.Deserialize(node
);
114 defaultUrls
.Deserialize(node
);
116 ProcessFilterFactoryNode(node
.SelectSingleNode("customFilterFactory"));
117 ProcessMatchHostNameAndPath(node
.SelectSingleNode("routing"));
118 ProcessExcludeAppPath(node
.SelectSingleNode("routing"));
120 XmlAttribute checkClientIsConnectedAtt
= node
.Attributes
["checkClientIsConnected"];
122 if (checkClientIsConnectedAtt
!= null && checkClientIsConnectedAtt
.Value
!= String
.Empty
)
124 checkClientIsConnected
= String
.Compare(checkClientIsConnectedAtt
.Value
, "true", true) == 0;
127 XmlAttribute useWindsorAtt
= node
.Attributes
["useWindsorIntegration"];
129 if (useWindsorAtt
!= null && useWindsorAtt
.Value
!= String
.Empty
)
131 useWindsorIntegration
= String
.Compare(useWindsorAtt
.Value
, "true", true) == 0;
133 if (useWindsorIntegration
)
135 ConfigureWindsorIntegration();
143 /// Gets the SMTP config.
145 /// <value>The SMTP config.</value>
146 public SmtpConfig SmtpConfig
148 get { return smtpConfig; }
152 /// Gets the view engine config.
154 /// <value>The view engine config.</value>
155 public ViewEngineConfig ViewEngineConfig
157 get { return viewEngineConfig; }
161 /// Gets the controllers config.
163 /// <value>The controllers config.</value>
164 public ControllersConfig ControllersConfig
166 get { return controllersConfig; }
170 /// Gets the view components config.
172 /// <value>The view components config.</value>
173 public ViewComponentsConfig ViewComponentsConfig
175 get { return viewComponentsConfig; }
179 /// Gets the routing rules.
181 /// <value>The routing rules.</value>
182 public RoutingRuleCollection RoutingRules
184 get { return routingRules; }
188 /// Gets the extension entries.
190 /// <value>The extension entries.</value>
191 public ExtensionEntryCollection ExtensionEntries
193 get { return extensions; }
197 /// Gets the service entries.
199 /// <value>The service entries.</value>
200 public ServiceEntryCollection ServiceEntries
202 get { return services; }
206 /// Gets the custom filter factory.
208 /// <value>The custom filter factory.</value>
209 public Type CustomFilterFactory
211 get { return customFilterFactory; }
215 /// Gets the scaffold config.
217 /// <value>The scaffold config.</value>
218 public ScaffoldConfig ScaffoldConfig
220 get { return scaffoldConfig; }
224 /// Gets a value indicating whether MR should check for client connection.
227 /// <c>true</c> if it should check client is connected; otherwise, <c>false</c>.
229 public bool CheckClientIsConnected
231 get { return checkClientIsConnected; }
235 /// Gets a value indicating whether to use windsor integration.
238 /// <c>true</c> if it should use windsor integration; otherwise, <c>false</c>.
240 public bool UseWindsorIntegration
242 get { return useWindsorIntegration; }
246 /// Gets a value indicating whether match host name and path should be used on
247 /// MonoRail routing.
250 /// <c>true</c> if it should match host name and path; otherwise, <c>false</c>.
252 public bool MatchHostNameAndPath
254 get { return matchHostNameAndPath; }
258 /// Gets a value indicating whether routing should exclude app path.
260 /// <value><c>true</c> if exclude app path; otherwise, <c>false</c>.</value>
261 public bool ExcludeAppPath
263 get { return excludeAppPath; }
267 /// Gets the configuration section.
269 /// <value>The configuration section.</value>
270 public XmlNode ConfigurationSection
272 get { return configurationSection; }
276 /// Gets the default urls.
278 /// <value>The default urls.</value>
279 public DefaultUrlCollection DefaultUrls
281 get { return defaultUrls; }
284 private void ProcessFilterFactoryNode(XmlNode node
)
286 if (node
== null) return;
288 XmlAttribute type
= node
.Attributes
["type"];
292 String message
= "The custom filter factory node must specify a 'type' attribute";
293 throw new ConfigurationErrorsException(message
);
296 customFilterFactory
= TypeLoadUtil
.GetType(type
.Value
);
299 private void ProcessMatchHostNameAndPath(XmlNode node
)
301 if (node
== null) return;
303 XmlAttribute matchHostNameAndPathAtt
= node
.Attributes
["matchHostNameAndPath"];
305 if (matchHostNameAndPathAtt
!= null && matchHostNameAndPathAtt
.Value
!= String
.Empty
)
307 matchHostNameAndPath
= String
.Compare(matchHostNameAndPathAtt
.Value
, "true", true) == 0;
311 private void ProcessExcludeAppPath(XmlNode node
)
313 if (node
== null) return;
315 // maybe a check to make sure both matchHostNameAndPathAtt & includeAppPath
316 // are not both set as that wouldn't make sense?
317 XmlAttribute excludeAppPathAtt
= node
.Attributes
["excludeAppPath"];
319 if (excludeAppPathAtt
!= null && excludeAppPathAtt
.Value
!= String
.Empty
)
321 excludeAppPath
= String
.Compare(excludeAppPathAtt
.Value
, "true", true) == 0;
325 private void ConfigureWindsorIntegration()
327 const string windsorExtensionAssemblyName
= "Castle.MonoRail.WindsorExtension";
329 services
.RegisterService(ServiceIdentification
.ControllerTree
, TypeLoadUtil
.GetType(
330 TypeLoadUtil
.GetEffectiveTypeName(
331 "Castle.MonoRail.WindsorExtension.ControllerTreeAccessor, " +
332 windsorExtensionAssemblyName
)));
334 controllersConfig
.CustomControllerFactory
= TypeLoadUtil
.GetType(
335 TypeLoadUtil
.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorControllerFactory, " +
336 windsorExtensionAssemblyName
));
338 viewComponentsConfig
.CustomViewComponentFactory
= TypeLoadUtil
.GetType(
339 TypeLoadUtil
.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorViewComponentFactory, " +
340 windsorExtensionAssemblyName
));
342 customFilterFactory
= TypeLoadUtil
.GetType(
343 TypeLoadUtil
.GetEffectiveTypeName("Castle.MonoRail.WindsorExtension.WindsorFilterFactory, " +
344 windsorExtensionAssemblyName
));