Update of /cvsroot/springnet/Spring.Net/src/Spring/Spring.Core/Objects/Factory/Xml
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17342
Added Files:
XmlConfigObjectFactoryHandler.cs
Log Message:
Convenience class to instantiate & populate an IObjectFactory from config file.
--- NEW FILE: XmlConfigObjectFactoryHandler.cs ---
#region Licence
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#region Imports
using System;
using System.Xml;
using System.Configuration;
using Spring.Util;
using Spring.Core.IO;
using Spring.Objects;
using Spring.Objects.Factory.Support;
#endregion
namespace Spring.Objects.Factory.Xml
{
/// <summary>
/// Creates a <see cref="Spring.Objects.Factory.IObjectFactory"/> instance
/// populated with the object definitions supplied in the configuration
/// section.
/// </summary>
/// <author>Rick Evans (.NET)</author>
/// <version>$Id: XmlConfigObjectFactoryHandler.cs,v 1.1 2004/07/26 07:40:37 springboy Exp $</version>
public class XmlConfigObjectFactoryHandler : IConfigurationSectionHandler
{
#region Constants
private static readonly string DefaultObjectFactoryType
= typeof (DefaultListableObjectFactory).AssemblyQualifiedName;
#endregion
#region Constructor (s) / Destructor
/// <summary>
/// Creates a new instance of the XmlConfigObjectFactoryHandler class.
/// </summary>
public XmlConfigObjectFactoryHandler () {}
#endregion
#region Properties
/// <summary>
/// The full, assembly qualified name of the
/// <see cref="Spring.Objects.Factory.IObjectFactory"/> instance that is to be
/// populated with object definitions pulled from a configuration section.
/// </summary>
/// <remarks>
/// <p>
/// The name must refer to an <see cref="Spring.Objects.Factory.IObjectFactory"/>
/// implementation that also implements the
/// <see cref="Spring.Objects.Factory.Support.IObjectDefinitionRegistry"/>
/// interface.
/// </p>
/// </remarks>
protected string ObjectFactoryTypeName
{
get {
return _factoryTypeName;
}
set {
if (StringUtils.HasText (value))
{
_factoryTypeName = value;
}
}
}
#endregion
#region Methods
/// <summary>
/// Creates a <see cref="Spring.Objects.Factory.IObjectFactory"/> instance
/// populated with the object definitions supplied in the configuration
/// section.
/// </summary>
/// <param name="parent">
/// The configuration settings in a corresponding parent
/// configuration section.
/// </param>
/// <param name="configContext">
/// The configuration context when called from the ASP.NET
/// configuration system. Otherwise, this parameter is reserved and
/// is a null reference.
/// </param>
/// <param name="section">
/// The <see cref="System.Xml.XmlNode"/> for the section.
/// </param>
/// <returns>
/// A <see cref="Spring.Objects.Factory.IObjectFactory"/> instance
/// populated with the object definitions supplied in the configuration
/// section.
/// </returns>
public object Create (object parent, object configContext, XmlNode section)
{
IObjectFactory factory = InstantiateFactory ();
XmlObjectDefinitionReader reader
= new XmlObjectDefinitionReader (factory as IObjectDefinitionRegistry);
reader.RegisterObjectDefinitions (
section as XmlElement, new ObjectsDefinitionConfigResource (section.Name));
return factory;
}
/// <summary>
/// Instantiate the <see cref="Spring.Objects.Factory.IObjectFactory"/> instance
/// that is to be populated with object definitions pulled from a configuration
/// section.
/// </summary>
/// <returns>
/// A <see cref="Spring.Objects.Factory.IObjectFactory"/> instance.
/// </returns>
protected virtual IObjectFactory InstantiateFactory ()
{
return ObjectUtils.InstantiateType (
ObjectUtils.ResolveType (ObjectFactoryTypeName)) as IObjectFactory;
}
#endregion
#region Fields
private string _factoryTypeName = DefaultObjectFactoryType;
#endregion
private class ObjectsDefinitionConfigResource : IResource
{
#region Constructor (s) / Destructor
/// <summary>
/// Creates a new instance of the ObjectsDefinitionConfigResource class.
/// </summary>
/// <param name="configurationSection">
/// The name of the configuration section that this resource is serving up.
/// </param>
public ObjectsDefinitionConfigResource (string configurationSection)
{
SetDescription (configurationSection);
}
#endregion
#region IResource Members
public bool IsOpen
{
get
{
return false;
}
}
public Uri URL
{
get
{
return null;
}
}
public System.IO.FileInfo File
{
get
{
return null;
}
}
public string Description
{
get
{
return _description;
}
}
public bool Exists
{
get
{
return false;
}
}
#endregion
#region IInputStreamSource Members
public System.IO.Stream InputStream
{
get
{
return null;
}
}
#endregion
#region Methods
/// <summary>
/// Sets the description of this configuration section resource.
/// </summary>
/// <param name="configurationSection">
/// The name of the configuration section.
/// </param>
private void SetDescription (string configurationSection)
{
_description = "[Configuration File Section] : " + configurationSection;
}
#endregion
#region Fields
private string _description;
#endregion
}
}
}
|