Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / Extensions / Session / CustomSessionExtension.cs
blob62e1a597a8bfe5af4c3d08ac0aab49cdad30f7cf
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.Extensions.Session
17 using System;
18 using System.Xml;
19 using System.Collections;
20 using System.Configuration;
21 using Castle.Core.Configuration;
22 using Castle.MonoRail.Framework.Configuration;
24 /// <summary>
25 /// This extension allow one to provide a custom
26 /// implementation of the session available on <see cref="IEngineContext"/>
27 /// </summary>
28 /// <remarks>
29 /// To successfully install this extension you must add the attribute <c>customSession</c>
30 /// to the <c>monoRail</c> configuration node and register the extension on the extensions node.
31 /// <code>
32 /// &lt;monoRail customSession="Type name that implements ICustomSessionFactory"&gt;
33 /// &lt;extensions&gt;
34 /// &lt;extension type="Castle.MonoRail.Framework.Extensions.Session.CustomSessionExtension, Castle.MonoRail.Framework" /&gt;
35 /// &lt;/extensions&gt;
36 /// &lt;/monoRail&gt;
37 /// </code>
38 /// </remarks>
39 public class CustomSessionExtension : IMonoRailExtension
41 /// <summary>
42 /// Reference to an instance of <see cref="ICustomSessionFactory"/>
43 /// obtained from the configuration
44 /// </summary>
45 private ICustomSessionFactory customSession;
47 #region IMonoRailExtension implementation
49 /// <summary>
50 /// Gives to the extension implementor a chance to read
51 /// attributes and child nodes of the extension node
52 /// </summary>
53 /// <param name="node">The node that defines the MonoRail extension</param>
54 public void SetExtensionConfigNode(IConfiguration node)
56 // Ignored
59 #endregion
61 #region IServiceEnabledComponent implementation
63 /// <summary>
64 /// Services the specified provider.
65 /// </summary>
66 /// <param name="provider">The provider.</param>
67 public void Service(IMonoRailServices provider)
69 ExtensionManager manager = (ExtensionManager) provider.GetService(typeof(ExtensionManager));
70 IMonoRailConfiguration config = (IMonoRailConfiguration) provider.GetService(typeof(IMonoRailConfiguration));
72 Init(manager, config);
75 #endregion
77 /// <summary>
78 /// Reads the attribute <c>customSession</c>
79 /// from <see cref="MonoRailConfiguration"/> and
80 /// instantiate it based on the type name provided.
81 /// </summary>
82 /// <exception cref="ConfigurationException">
83 /// If the typename was not provided or the type
84 /// could not be instantiated/found
85 /// </exception>
86 /// <param name="manager">The Extension Manager</param>
87 /// <param name="configuration">The configuration</param>
88 private void Init(ExtensionManager manager, IMonoRailConfiguration configuration)
90 manager.AcquireSessionState += OnAdquireSessionState;
91 manager.ReleaseSessionState += OnReleaseSessionState;
93 string customSessionAtt =
94 configuration.ConfigurationSection.Attributes["customSession"];
96 if (customSessionAtt == null)
98 String message = "The CustomSessionExtension requires that " +
99 "the type that implements ICustomSessionFactory be specified through the " +
100 "'customSession' attribute on 'monorail' configuration node";
101 throw new ConfigurationErrorsException(message);
104 Type customSessType = TypeLoadUtil.GetType(customSessionAtt);
106 if (customSessType == null)
108 String message = "The Type for the custom session could not be loaded. " +
109 customSessionAtt;
110 throw new ConfigurationErrorsException(message);
115 customSession = (ICustomSessionFactory) Activator.CreateInstance(customSessType);
117 catch(InvalidCastException)
119 String message = "The Type for the custom session must " +
120 "implement ICustomSessionFactory. " + customSessionAtt;
121 throw new ConfigurationErrorsException(message);
125 /// <summary>
126 /// Overrides the ISession instance on <see cref="IEngineContext"/>.
127 /// </summary>
128 /// <remarks>
129 /// Note that the session available through IHttpContext is left untouched</remarks>
130 /// <param name="context"></param>
131 private void OnAdquireSessionState(IEngineContext context)
133 IDictionary session = customSession.ObtainSession(context);
135 context.Session = session;
138 /// <summary>
139 /// Retrives the ISession instance from <see cref="IEngineContext"/>.
140 /// and invokes <see cref="ICustomSessionFactory.PersistSession"/>
141 /// </summary>
142 private void OnReleaseSessionState(IEngineContext context)
144 customSession.PersistSession(context.Session, context);