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
.Extensions
.Session
19 using System
.Collections
;
20 using System
.Configuration
;
21 using Castle
.Core
.Configuration
;
22 using Castle
.MonoRail
.Framework
.Configuration
;
25 /// This extension allow one to provide a custom
26 /// implementation of the session available on <see cref="IEngineContext"/>
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.
32 /// <monoRail customSession="Type name that implements ICustomSessionFactory">
33 /// <extensions>
34 /// <extension type="Castle.MonoRail.Framework.Extensions.Session.CustomSessionExtension, Castle.MonoRail.Framework" />
35 /// </extensions>
39 public class CustomSessionExtension
: IMonoRailExtension
42 /// Reference to an instance of <see cref="ICustomSessionFactory"/>
43 /// obtained from the configuration
45 private ICustomSessionFactory customSession
;
47 #region IMonoRailExtension implementation
50 /// Gives to the extension implementor a chance to read
51 /// attributes and child nodes of the extension node
53 /// <param name="node">The node that defines the MonoRail extension</param>
54 public void SetExtensionConfigNode(IConfiguration node
)
61 #region IServiceEnabledComponent implementation
64 /// Services the specified provider.
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
);
78 /// Reads the attribute <c>customSession</c>
79 /// from <see cref="MonoRailConfiguration"/> and
80 /// instantiate it based on the type name provided.
82 /// <exception cref="ConfigurationException">
83 /// If the typename was not provided or the type
84 /// could not be instantiated/found
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. " +
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
);
126 /// Overrides the ISession instance on <see cref="IEngineContext"/>.
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
;
139 /// Retrives the ISession instance from <see cref="IEngineContext"/>.
140 /// and invokes <see cref="ICustomSessionFactory.PersistSession"/>
142 private void OnReleaseSessionState(IEngineContext context
)
144 customSession
.PersistSession(context
.Session
, context
);