Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Objects/Factory/Support
In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv17021/src/Spring/Spring.Web/Objects/Factory/Support
Modified Files:
WebObjectFactory.cs
Log Message:
SPRNET-953
SPRNET-952
SPRNET-949
SPRNET-948
SPRNET-947
Index: WebObjectFactory.cs
===================================================================
RCS file: /cvsroot/springnet/Spring.Net/src/Spring/Spring.Web/Objects/Factory/Support/WebObjectFactory.cs,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** WebObjectFactory.cs 16 Dec 2007 12:43:48 -0000 1.9
--- WebObjectFactory.cs 29 May 2008 12:13:27 -0000 1.10
***************
*** 52,61 ****
private readonly static ILog log = LogManager.GetLogger(typeof(WebObjectFactory));
! private const string OBJECTTABLEKEY = "spring.objects";
/// <summary>
/// Holds the virtual path this factory has been created from.
/// </summary>
! private string contextPath;
#region Constructor (s) / Destructor
--- 52,68 ----
private readonly static ILog log = LogManager.GetLogger(typeof(WebObjectFactory));
! private readonly static string OBJECTTABLEKEY = "spring.objects";
!
! private delegate IDictionary ObjectDictionaryCreationHandler();
/// <summary>
/// Holds the virtual path this factory has been created from.
/// </summary>
! private readonly string contextPath;
! /// <summary>
! /// Holds the handler reference for creating an object dictionary
! /// matching this factory's case-sensitivity
! /// </summary>
! private readonly ObjectDictionaryCreationHandler createObjectDictionary;
#region Constructor (s) / Destructor
***************
*** 101,104 ****
--- 108,115 ----
{
this.contextPath = contextPath;
+ this.createObjectDictionary = (caseSensitive)
+ ? new ObjectDictionaryCreationHandler(CreateCaseSensitiveDictionary)
+ : new ObjectDictionaryCreationHandler(CreateCaseInsensitiveDictionary);
+
InstantiationStrategy = new WebInstantiationStrategy();
}
***************
*** 106,110 ****
#endregion
! #region Convinience accessors for Http* objects
/// <summary>
--- 117,121 ----
#endregion
! #region Convenience accessors for Http* objects
/// <summary>
***************
*** 119,123 ****
/// Get the table of 'request'-scoped objects.
/// </summary>
! private IDictionary Request
{
get
--- 130,134 ----
/// Get the table of 'request'-scoped objects.
/// </summary>
! protected virtual IDictionary Request
{
get
***************
*** 129,134 ****
if (objecttable == null)
{
! objecttable = new Hashtable();
! this.Context.Items[OBJECTTABLEKEY] = objecttable;
}
}
--- 140,144 ----
if (objecttable == null)
{
! this.Context.Items[OBJECTTABLEKEY] = CreateObjectDictionary();
}
}
***************
*** 140,144 ****
/// Get the table of 'session'-scoped objects. Returns null, if Session is disabled.
/// </summary>
! private IDictionary Session
{
get
--- 150,154 ----
/// Get the table of 'session'-scoped objects. Returns null, if Session is disabled.
/// </summary>
! protected virtual IDictionary Session
{
get
***************
*** 150,155 ****
if (objecttable == null)
{
! objecttable = new Hashtable();
! Context.Session[OBJECTTABLEKEY] = objecttable;
}
}
--- 160,164 ----
if (objecttable == null)
{
! Context.Session[OBJECTTABLEKEY] = CreateObjectDictionary();
}
}
***************
*** 161,164 ****
--- 170,181 ----
/// <summary>
+ /// Creates a dictionary matching this factory's case sensitivity behaviour.
+ /// </summary>
+ protected IDictionary CreateObjectDictionary()
+ {
+ return createObjectDictionary();
+ }
+
+ /// <summary>
/// Creates the root object definition.
/// </summary>
***************
*** 181,202 ****
protected override object GetSingleton(string objectName)
{
! object instance = base.GetSingleton(objectName);
! if (instance == null)
! {
! string key = GetObjectKey(objectName);
! if (this.Context != null)
{
! if (this.Request.Contains(key))
! {
! instance = this.Request[key];
! }
! else if (this.Session != null && this.Session.Contains(key))
! {
! instance = this.Session[key];
! }
}
}
- return instance;
}
--- 198,233 ----
protected override object GetSingleton(string objectName)
{
! object instance = null;
! instance = GetScopedSingleton(objectName, this.Request);
! if (instance != null) return instance;
!
! instance = GetScopedSingleton(objectName, this.Session);
! if (instance != null) return instance;
!
! instance = base.GetSingleton(objectName);
! return instance;
! }
!
! /// <summary>
! /// Looks up an <paramref name="objectName"/> in the specified cache dictionary.
! /// </summary>
! /// <param name="objectName">the name to lookup.</param>
! /// <param name="scopedSingletonCache">the cache dictionary to search</param>
! /// <returns>the found instance. null otherwise</returns>
! protected object GetScopedSingleton(string objectName, IDictionary scopedSingletonCache)
! {
! if (scopedSingletonCache == null) return null;
!
! lock (scopedSingletonCache.SyncRoot)
! {
! object instance = scopedSingletonCache[objectName];
! if (instance == TemporarySingletonPlaceHolder)
{
! throw new ObjectCurrentlyInCreationException(objectName);
}
+
+ return instance;
}
}
***************
*** 217,277 ****
string objectName, RootObjectDefinition objectDefinition, object[] arguments)
{
! if (objectDefinition is IWebObjectDefinition)
{
- object instance;
ObjectScope scope = ((IWebObjectDefinition)objectDefinition).Scope;
! if (scope == ObjectScope.Application)
{
! return base.CreateAndCacheSingletonInstance(objectName, objectDefinition, arguments);
! // base.AddSingleton(objectName, TemporarySingletonPlaceHolder);
! // // eager caching (last parameter) should be allowed in order to resolve circular references
! // instance = CreateObject(objectName, objectDefinition, arguments, true);
! // base.AddSingleton(objectName, instance);
}
! else if (scope == ObjectScope.Request)
{
! if (this.Context == null)
{
! throw new ObjectCreationException(string.Format("Can't create 'request' scoped web singleton object '{0}' without a valid HttpContext.Current instance.", objectName));
}
! string key = GetObjectKey(objectName);
! Request[key] = TemporarySingletonPlaceHolder;
! instance = CreateObject(objectName, objectDefinition, arguments, false);
! Request[key] = instance;
}
! else if (scope == ObjectScope.Session)
{
! if (this.Context == null)
{
! throw new ObjectCreationException(string.Format("Can't create 'session' scoped web singleton object '{0}' without a valid HttpContext.Current instance.", objectName));
}
! IDictionary sessionCache = this.Session;
! if (sessionCache == null)
{
! throw new ObjectCreationException(string.Format("'session' scoped web singleton object '{0}' require a valid Session.", objectName));
}
! string key = GetObjectKey(objectName);
! sessionCache[key] = TemporarySingletonPlaceHolder;
! instance = CreateObject(objectName, objectDefinition, arguments, false);
! sessionCache[key] = instance;
}
else
{
! throw new ObjectDefinitionException("Web singleton objects must be either request, session or application scoped.");
}
- return instance;
}
! return base.CreateAndCacheSingletonInstance(objectName, objectDefinition, arguments);
}
/// <summary>
! /// Creates hopefully unique key for the scope based on object name.
/// </summary>
! /// <param name="name">Object name.</param>
! /// <returns>Generated key.</returns>
! private string GetObjectKey(string name)
{
! return name + ".spring";
}
--- 248,399 ----
string objectName, RootObjectDefinition objectDefinition, object[] arguments)
{
! if (objectDefinition is IWebObjectDefinition
! && ((IWebObjectDefinition)objectDefinition).Scope != ObjectScope.Application)
{
ObjectScope scope = ((IWebObjectDefinition)objectDefinition).Scope;
! if (scope == ObjectScope.Request)
{
! IDictionary requestCache = this.Request;
! if (requestCache == null)
! {
! throw new ObjectCreationException(string.Format("'request' scoped web singleton object '{0}' requires a valid Request.", objectName));
! }
!
! object instance = CreateAndCacheScopedSingletonInstance(objectName, objectDefinition, arguments, requestCache);
! return instance;
}
! else if (scope == ObjectScope.Session)
{
! IDictionary sessionCache = this.Session;
! if (sessionCache == null)
{
! throw new ObjectCreationException(string.Format("'session' scoped web singleton object '{0}' requires a valid Session.", objectName));
}
! object instance = CreateAndCacheScopedSingletonInstance(objectName, objectDefinition, arguments, sessionCache);
! return instance;
}
!
! throw new ObjectDefinitionException("Web singleton objects must be either request, session or application scoped.");
! }
!
! return base.CreateAndCacheSingletonInstance(objectName, objectDefinition, arguments);
! }
!
! /// <summary>
! /// Creates a singleton instance for the specified object name and definition
! /// and caches the instance in the specified dictionary
! /// </summary>
! /// <param name="objectName">
! /// The object name (will be used as the key in the singleton cache key).
! /// </param>
! /// <param name="objectDefinition">The object definition.</param>
! /// <param name="arguments">
! /// The arguments to use if creating a prototype using explicit arguments to
! /// a static factory method. It is invalid to use a non-null arguments value
! /// in any other case.
! /// </param>
! /// <param name="scopedSingletonCache">the dictionary to be used for caching singleton instances</param>
! /// <returns>The created object instance.</returns>
! /// <remarks>
! /// If the object is successfully created, <paramref name="scopedSingletonCache"/>
! /// contains the cached instance with the key <paramref name="objectName"/>.
! /// </remarks>
! protected virtual object CreateAndCacheScopedSingletonInstance(string objectName, RootObjectDefinition objectDefinition, object[] arguments, IDictionary scopedSingletonCache)
! {
! object instance;
! lock (scopedSingletonCache.SyncRoot)
! {
! instance = scopedSingletonCache[objectName];
! if (instance == TemporarySingletonPlaceHolder)
{
! throw new ObjectCurrentlyInCreationException(objectName);
! }
! else if (instance == null)
! {
! scopedSingletonCache.Add(objectName, TemporarySingletonPlaceHolder);
! try
{
! instance = CreateObject(objectName, objectDefinition, arguments, true);
! AssertUtils.ArgumentNotNull(instance, "instance");
! scopedSingletonCache[objectName] = instance;
}
! catch
{
! scopedSingletonCache.Remove(objectName);
! throw;
}
! }
! }
! return instance;
! }
!
! /// <summary>
! /// Add the created, but yet unpopulated singleton to the singleton cache
! /// to be able to resolve circular references
! /// </summary>
! /// <param name="objectName">the name of the object to add to the cache.</param>
! /// <param name="objectDefinition">the definition used to create and populated the object.</param>
! /// <param name="rawSingletonInstance">the raw object instance.</param>
! /// <remarks>
! /// Derived classes may override this method to select the right cache based on the object definition.
! /// </remarks>
! protected override void AddEagerlyCachedSingleton(string objectName, IObjectDefinition objectDefinition, object rawSingletonInstance)
! {
! if (objectDefinition is IWebObjectDefinition
! && ((IWebObjectDefinition)objectDefinition).Scope != ObjectScope.Application)
! {
! ObjectScope scope = ((IWebObjectDefinition) objectDefinition).Scope;
! if (scope == ObjectScope.Request)
! {
! this.Request[objectName] = rawSingletonInstance;
! }
! else if (scope == ObjectScope.Session)
! {
! this.Session[objectName] = rawSingletonInstance;
}
else
{
! throw new ObjectDefinitionException("Web singleton objects must be either request, session or application scoped.");
}
}
! else
! {
! base.AddEagerlyCachedSingleton(objectName, objectDefinition, rawSingletonInstance);
! }
}
/// <summary>
! /// Remove the specified singleton from the singleton cache that has
! /// been added before by a call to <see cref="AddEagerlyCachedSingleton"/>
/// </summary>
! /// <param name="objectName">the name of the object to remove from the cache.</param>
! /// <param name="objectDefinition">the definition used to create and populated the object.</param>
! /// <remarks>
! /// Derived classes may override this method to select the right cache based on the object definition.
! /// </remarks>
! protected override void RemoveEagerlyCachedSingleton(string objectName, IObjectDefinition objectDefinition)
{
! if (objectDefinition is IWebObjectDefinition
! && ((IWebObjectDefinition)objectDefinition).Scope != ObjectScope.Application)
! {
! ObjectScope scope = ((IWebObjectDefinition) objectDefinition).Scope;
! if (scope == ObjectScope.Request)
! {
! this.Request.Remove(objectName);
! }
! else if (scope == ObjectScope.Session)
! {
! this.Session.Remove(objectName);
! }
! else
! {
! throw new ObjectDefinitionException("Web singleton objects must be either request, session or application scoped.");
! }
! }
! else
! {
! base.RemoveEagerlyCachedSingleton(objectName, objectDefinition);
! }
}
***************
*** 356,359 ****
--- 478,497 ----
}
}
+
+ /// <summary>
+ /// Creates a case insensitive hashtable instance
+ /// </summary>
+ private static IDictionary CreateCaseInsensitiveDictionary()
+ {
+ return CollectionsUtil.CreateCaseInsensitiveHashtable();
+ }
+
+ /// <summary>
+ /// Creates a case sensitive hashtable instance
+ /// </summary>
+ private static IDictionary CreateCaseSensitiveDictionary()
+ {
+ return new Hashtable();
+ }
}
}
\ No newline at end of file
|